miércoles 13 de agosto de 2008

Technical Reading

August is a good month to practice some reading, even more so if, as is my case, you have to stay at work. Between novels and Terry Pratchet I always try to drop in a technical book or two. This summer I finally had the time for two that had been sitting on my TODO list for ages: Java Puzzlers and Effective Java (2nd ed). Both from Joshua Bloch, one of the fathers of Java.

Java Puzzlers is a book that deals (as the name implies) with traps, pitfalls and corner cases. And good practices! An example can clarify things. Look at the following code snippet (taken from puzzle #1):

public boolean isOdd(int i) {
   return i % 2 == 1;
}

A quick glance reveals a standard use of the modulus to determine if a number is even or odd. Can you spot the problem? I didn't really. In fact, the code does not work for negative odd numbers or rephrased it the method fails in a 25% of the situations. You can test it (I recommend using the Groovy console for testing quick scripts like this). A correct version would look like:

public static boolean isOdd(int i) {
   return i % 2 != 0;
}

The author explains concisely why it fails even proposes a better (faster) version (using the logical AND operator). The book is made of nearly one hundred puzzles grouped in ten chapters covering operators, casting, exceptions, loops, classes et al. The cases are sometimes very convoluted and well beyond the scope of day to day coding but are also important to know. If anything, the book will just make the reader aware of the importance of unit testing and code coverage (although it doesn't mention them explicitly). Even one liners like the above must be matched by a test.

Effective Java is a book on advanced Java programming. You may think that I (or you) don't need a book on the topic any longer. In the end, we are expert coders! Great! Read the book and tell me :-) IMHO this is the definitive Java guide. I was impressed (and probably you will be also) by the depth of the knowledge showed in every chapter. Even those that deal with commodity topics (and this is subjective) like exceptions or some Java 5 features. There's a sample chapter (generics) available for free reading here. You get the idea. Now imagine equal dissertations over Serializable, Concurrency, Composition or Performance. I bet most people don't know the subtleties of the static reserved word and how it interacts with several other language features like, say, private inner classes. If you want to go to that level of depth this is your book.

All in all, I liked both books, though the second is way more useful. Probably they're not as easy to read as something like The Mythical Man-Month or The Pragmatic Programmer but they also offer an understanding of the Java language that you won't get in any other place. So...highly recommended lecture!!