The Graduate Developer Community’s next free event is ‘December’s Code Share: Java Puzzlers’ on Wednesday 7th December at 6:30pm.

Please see link for details and to sign up –

Java Puzzlers

There are more things in heaven and earth, Horatio, Than are dreamt of in your philosophy

William Shakespeare’s Hamlet 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

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 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 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.

idid 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 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 –… – for drinks/networking.

ThoughtWorks are delighted to be sponsors of the December Code Share!

This event is hosted in collaboration with the London Java Community. We expect many programmers across the range from junior to experienced to be participating, so it’s a great chance to see how code is viewed by an experienced programmer.