Wednesday, February 22, 2006

Java Puzzlers: Traps, Pitfalls, and Corner Cases By Joshua Bloch and Neal Gafter

I just finished reading Java Puzzlers and thought I might try and collect my thoughts and write them up in some coherent form. First off, I found the book to be an enjoyable read; I don't think I have so thoroughly enjoyed a piece of technical writings since The Mythical Man Month by Fred Brooks, or perhaps Effective Java by Joshua Bloch. Bloch and Gafter's writing style is light, and effective, then don't belabour the obvious, nor glaze over important details. At times they stare down difficult topics with tongue firmly planted in cheek, they go so far as to offer a special appendix on their site that explains some of their puns and pop culture references.

At first glance the book appears to be an exploration of the more esoteric portions of the Java Language Specification; neither interesting nor particularly useful beyond novelty. While it would be practically impossible to use this book to solve a particular peculiar problem you are having, it is not intended to be used in that manner. Instead the book provides a fresh way to present AntiPatterns.

Each of the puzzles the book uses to explore the language comes in the following format:

The problem is either in the form of a piece of code or a request for you to write a piece of code to solve a particular simple problem. You are then left to divine what the program will do.

Naturally, and intuitively you will have deduced this is what the code will do, or this is the piece of code you were to write. Inevitably you are wrong, by mid way through the book you come to expect you are wrong and start second guessing your instincts, your second solution is still wrong. Don't sweat your ego, this is the point of the book, while the Java Language usually behaves, there are areas of the spec that would leave Kernighan & Ritchie sweating.

Real Solution:
Truth is revealed, and enlightenment comes, and we move on. This section explains in detail, and with references what is going on, what trick they trapped you on, and what change to the code would make it behave they way you expect (and likely would desire.)

Lessons for Developers:
This is the real diamond in the rough, if you follow the given advice you will keep your code out of the troubled spots. This isn't to imply that your post Java Puzzlers will be free of bugs, but perhaps those bugs won't fall into the subset of nearly impossible to identify. It is left as an exercise for the reader to create impossible bugs to puzzle themselves with. For those familiar with Effective Java, many of the patterns/antipatterns of this lesson section are reiterations from that book. As evidenced by the existence of this these mistakes are often repeated, so repetition of the solution can't be resented.

Lessons for Language Designers:
While less useful for us lowly developers, these lessons are very much patterns/antipatterns for language designers. Those of us in the coding trenches can only hope that they glean something from this section.

At the end of the book, they concisely summarize the developer lessons into a handy appendix. This appendix could easily be stapled into any developer guideline, or best practices document. Long after you finish the book, this set of lessons is a take away you can apply again and again in any application.

While this book is probably of greatest value to a Java Developer, many of the Antipatterns are language agnostic. The style really engages the reader, hopefully resulting in more knowledge retention then the typical technical read. Additionally the challenge style of the book may aid a developer in preparation for their Java 2 Programmer Certification, although I would recommend this as a study secondary source only. Finally, those with passing interest in the language, or with a history in it, while not actively developing, may get a kick out of it. I hope I have intrigued at least one person into picking up a copy and giving it a read, now that I have returned the library copy, I intend to pick up a copy from the bookstore for my personal shelf.

On a side note...
While clearly neither the core content nor particular educational value the book is filled with a few extras. For formatting reasons the book has many large white spaces, these are left to make sure you don't cheat, and think about a problem before reading ahead to see the solution. Instead of leaving the space blank, or filling it with such witty content like "This space has been intentionally left blank" or "This space has been intentionally left blank, ironically it isn't" the spaced are filled with graphical illusions. In addition to the illusions there is an interesting appendix discussion them.


Jennith said...

Hey Geoff,

Awesome book review - I couldn't get java to add 2 numbers together without significant study, so I'm sure that it is way over my head. However, your review tempted me to read it anyways.



Anonymous said...

Very interesting book I am looking forward to get some free time to read it

Neal Gafter said...

Great review; thanks!

Jennith: Can't get it to add two numbers? Neither can we. The book starts gently. See Puzzle 4, which prints the sum of two small numbers.