December's Code Share: Java Puzzlers


Details
Java Puzzlers
There are more things in heaven and earth, Horatio, Than are dreamt of in your philosophy William Shakespeare's Hamlet http://www.online-literature.com/shakespeare/hamlet/6/
The year is coming to and end and there's a last day of term feel to the last code share of 2011. We're going to solve some puzzles. It's going to make a nice change.
Joshua Bloch and Neal Gafter have gathered together a good collection in their book Java Pluzzlers http://www.javapuzzlers.com/
These behaviors are known as traps, pitfalls, and corner cases. Every platform has them, but Java has far fewer than other platforms of comparable power. The goal of the book is to entertain you with puzzles while teaching you to avoid the underlying traps and pitfalls. By working through the puzzles, you will become less likely to fall prey to these dangers in your code and more likely to spot them in code that you are reviewing or revising. Joshua Bloch's and Neal Gafter's A Java Puzzlers Sampler http://www.javapuzzlers.com/java-puzzlers-sampler.pdf Does every language have them? It would be interesting to see some from languages other than java. The puzzles seem like the black holes of software, the places where the normal laws break down. This isn't true, the laws are behaving exactly as they should but interacting in unexpected ways. That's why studying these puzzlers is such a useful exercise: it forces to look at what is really happening, to see the rules for what they really are. They also remind us of Spolsky's Law of Leaky Abstractions.
All non-trivial abstractions, to some degree, are leaky. Abstractions fail. Sometimes a little, sometimes a lot. There's leakage. Things go wrong. It happens all over the place when you have abstractions. Joel Spolsky's Law of Leaky Abstractions http://www.joelonsoftware.com/articles/LeakyAbstractions.html
Computer programming is all about building abstractions, code is just a lie we tell ourselves because we cannot cope with complex reality of engines we tend. There is nothing wrong with that as long as we don't start believing them.
A lot of computer programming consists of building abstractions. What is a string library? It's a way to pretend that computers can manipulate strings just as easily as they can manipulate numbers. What is a file system? It's a way to pretend that a hard drive isn't really a bunch of spinning magnetic platters that can store bits at certain locations, but rather a hierarchical system of folders-within-folders containing individual files that in turn consist of one or more strings of bytes. Looking at these puzzles reminds us that there is more going on than our simplified philosophical abstractions allow for. In these puzzles the consequences are obvious, they results jump out and confound us. In our day to day code those consequences are hard to see. They may result in code that performs poorly. They may confound only a fraction of ours users under unusual circumstances. Regardless, their are always consequences and it does us good to be reminded of them. Also, it's fun to do puzzles.
Challenge: Language Quirks
Thanks to Peter Lawrey of Vanilla Java (http://vanillajava.blogspot.com/) for the following challenge.
Give all the types and values for which `x` make this an infinite loop. There are 16 answers!
while(x == -x);
Here is another one, this time with 3 answers.
while(x != x + 0);
Please bring along your answers, or any other example of a language behaving strangely.
What's Going To Happen?
On Monday 5th December, a couple of days before the share, we'll send out an email to everybody who has signed up. If you have any code to contribute please send it in a reply to that email.
On Wednesday 7th December, the Code Share itself, we will all have a couple of short presentations. This will be followed by breaking out into groups to try and predict the behaviour of the code. Will we be able to successfully predict the outcome? The puzzles will be printed, so you won't have to bring a laptop. Afterwards we will come back together as a group to discuss what we have learned.
After the event we will be heading to the Half Moon, 213-223 Mile End Road, Mile End, Greater London, E1 4AA - http://www.jdwetherspoon.co.uk/home/pubs/th... (http://www.jdwetherspoon.co.uk/home/pubs/the-half-moon)- for drinks/networking.
ThoughtWorks are delighted to be sponsors of the December Code Share!
This is a joint event with the Graduate Developer Group (link to follow) - providing an opportunity for both experienced developers and newcomers to the profession to share points of view.
Please Note -
At previous events LJC members have found this venue/particular room hard to find.
This event is being held in the Francis Bancroft Building - Room Number - FB 1.13a.
Please see this map for further information (the building is numbered 26 on this map) http://www.qmul.ac.uk/docs/about/26065.pdf
The actual signs at the building will say 'Bancroft' building only rather than Francis Bancroft.
Directions -
From Mile End Station you will need to turn left out of the station and then continue for 5 mins approx. up the Mile End Road and then cross over the road to the QM campus and go into the East Gate. This is the first entrance to QM you come to when coming from the direction of Mile End Tube. Continue straight on up Westfield Way for a couple of minutes and then bear left at the Curve cafe. The Bancroft Building is on your right (number 26 on the map).

December's Code Share: Java Puzzlers