Tuesday, July 1, 2008

Book Review: Effective Java, Second Edition

Effective Java, Second Edition by Joshua Bloch is certainly the best Java book I have read in a long time. As a disclaimer, I never read the first edition and I am thus unable to compare the two editions. Effective Java, Second Edition is a mostly easy and fun read providing you with many insights and best practices on how to use Java effectively. It certainly is not a book for the beginner just starting out learning Java. For that purpose you may want to take a look at Thinking in Java by Bruce Eckel instead. Nevertheless, Effective Java would serve as an excellent follow-up.

In Effective Java, Joshua Bloch does a great job describing best practices that you as developer will find useful on a daily basis. For example, I really found his description of the builder pattern (Item 2, page 11) quite interesting. Another Item that fascinated me, was Item 15 (page 73) - "Minimize mutability". Both items are part of a broader theme throughout the book that promotes creating code that is as immutable as possible. In that regard, reading the book will enable you to simply write better and safer code. The book also leads the way towards promoting functional programming techniques which will come in quite handily when developing multithreaded applications. Therefore, as a next book I may recommend reading Java Concurrency in Practice by Brian Goetz.

Even for the experienced Java developer, Effective Java contains quite a few little eye openers. I for example was previously unaware of how static factory methods can simplify the creation of parameterized type instances using "type inference". This example is described on page 9 (Item 1). In the past I had always used something like this:

List users = new ArrayList();

But by using a static factory method you can do:

List users = Helper.newArrayList();

I thought that this was a pretty nifty example that may help making code a bit cleaner. What I also very much liked about Effective Java was that Joshua points out certain short-comings of the Java language itself and its APIs whenever applicable. For example, page 64 describes the inconsistent behavior between BigDecimal's 'equals' method and its 'compareTo' method, and in item 41 (page 194) Joshua details the shortcomings of the List interface when using Autoboxing.

While the vast majority of the book was very easy to read and to understand, I found that the chapter about bounded wildcards using generics (item 28) was a little difficult to grasp and I wished it were a bit more extensive. On the other side, the provided mnemonic is quite helpful: PECS - Producer-extends, Consumer-super.

Overall, I highly recommend Effective Java, Second Edition which will continue to serve me, and likely you too, as an excellent reference resource.

No comments: