Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Book Review: Java by Comparison

Book Review: Java by Comparison

This item in japanese


"Java by Comparison; Become a Java Craftsman in 70 Examples" is an excellent book. To describe it in one sentence: it teaches you how to write better and clearer Java code.

Who is this book for?
I like that the authors set expectations for the book by saying you should be able to code Fizz Buzz in less than 15 minutes. It’s a lot more specific than just saying one should know how to program.

The authors struck a good balance in covering multiple audiences. The book manages to be useful for:

  • New developers - Most examples are understandable by a programmer who just knows the basics. Even when using an API that a new programmer might not know, it is still clear why the improved version is better.
  • New Java developers transitioning from another language - The examples focus on common idioms. This helps map knowledge from the other language and also helps the developer realize when they are carrying idioms from the other language.
  • Mid-level Java developers - Most of the examples are still useful to a developer who would like to write better code. Since each example is independent, it is easy to skim past the ones that the developer knows and go straight to the other parts. It’s also good for learning JUnit 5 and Java 8 best practices in a clear/concise way.
  • Experienced developers - It's easy to forget that not everyone knows as much as you do. This book is an excellent way to remember to write code that is easily understandable to everyone on the team. It’s also helpful to keep handy during code reviews to show teammates. After all, you didn’t make up these ideas on clean code; they are well recognized!

What is the format?

The majority of the book uses a two page format which is convenient in a physical book or two page view in an e-book reader. Just make sure your screen is large enough to view two pages at once. Trying to read this on a Kindle seems like it would be a frustrating experience!

The two page format is not the one you might have seen from another publisher. This one is designed for comparing code. The top left page has the "before" code. The top right page has the improved code. This is an easy format for comparing. It’s also helpful in seeing that the improved code is often shorter/cleaner.

The important differences in the code snippets are supposed to be highlighted. However, in the print copy, the highlighting is medium gray (on a light gray background). By contrast, the Java keywords are in black bolded and the rest of the code is in black.

This had the effect of me needing to train myself to "tune out" the keywords and focus on the gray text. It took me a few pages to get there so wasn’t a big problem. There was a reference to green so I imagine the e-book uses actual colors. At least the printed book isn’t a problem for the colorblind as there is only one color involved!

The bottom/remainder both pages describe the differences, why they are important and various other pointers. Which brings us to another formatting oddity. When code is used within the text sections, it was in a narrower font than the text. I’m used to the opposite. Some of the examples even contain bonus tips like how to avoid null pointers.

What version of Java is covered?
The book primarily targets Java 8.  There were great examples with lambdas, streams and optionals. I even learned that the Collections class has a frequency method. That’s definitely something that could make my code shorter!

There’s a reference to a number of APIs in Java 9, which is fun. One of the tips had a suggestion for documenting modules for Java 9. Overall, Java 8 is a logical choice that will keep the book current for a long time. The APIs introduced in Java 9/10/11 aren't used in the space of examples used in the book.

How are the examples?
The examples cover a variety of topics. Many were refactorings for writing shorter and easier to read code. Some were functional like not using BigDecimal for monetary amounts. (Thank you!) I also really like seeing the JUnit 5 parameterized test in there. I liked the tip that gave a template for writing good comments.

Is the book all examples?
Mostly. The first eight chapters cover the 70 examples. The ninth and final chapter covers other good development practices. I’m glad Jenkins and SonarQube got a shout out in there. I also enjoyed seeing libraries referenced like jGiven (a BDD library for Java) and junit-quickcheck (for property-based testing).

Finally, it was great to see the caveat that it is important for an actual person to look at static analysis rules to see if they apply.

Overall impressions
I enjoyed reading this book and definitely recommend it. It's written in an easy-to-read style with both short and fun examples. I was definitely skeptical when reading the back cover and seeing the broad range of skills that this book was targeting. However, it is true: this book really is useful for more than just novices. Happy reading!

Rate this Article


Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p