1.12.2018 Book Review: Java by Comparision: Become a Java Craftsman in 70 Examples

Java by Comparison [1] leads the reader through 70 examples into software-craftship.
Each example compares a snipped of typical „unclean“ code with a „clean“ replacement, including a short discussion about the reasoning.

What it is about?

This book is a great practical guide for Java developers comprising many aspects from the classical books Clean Code [2], Refactoring [3] and Effective Java [4]. The examples follow a didactically well done learning curve and the explanations are written in an easy language. The book ends with a chapter about general practices for contemporary software development like static code analysis, build automation and continuous integration.

Who is it for?

I strongly recommend the book for:

  • Everybody, who is learning Java programming and wants to learn it in a professional way,
  • junior Java programmers who want to take their next step and become a software craftsperson,
  • senior Java developers as a guide for code reviews and to lead junior Java developers,
  • Java instructors as a foundation for a great curriculum.

Can I recommend it?

Yes! To begin with, in general I agree with all the presented rules. Whenever there are multiple opinions out in the field or exceptions depending on the context, such is also discussed by the authors. I wished these topics were part of college curricula; unfortunately, at least in Germany, this is usually not the case. Great to learn about this extraordinary exception.

Even for me, with >30 years of programming- and over 20 years of Java-experience, there were still plenty new details to learn.

Some Highlights

One special highlight for me was the chapter Let Your Data Flow. Here the authors offer great rules for using the Java 8 functional programming features Lambdas and great rules about the use cases for Java 8 Optional.

I also liked a lot that ADRs (architecture decision records) were mentioned: ADRs are schema for recording decisions about software architecture and their reasoning. I also like using this format for design decisions right in the code, as depicted in the chapter Document Implementation Decisions.

Excursion: Exception Handling in Java Streams

One example was keeping me busy for a while was the chapter Avoid Exceptions in Streams. I had not realized before how ugly Java streams can become if exceptions have to be handled and resources be closed as in Java’s try with resources. The authors show how such can be handled in a half-way decent way. If Java had an Error Handling Monad (compare this presentation), it could look somehow like this:

class LogBooks {

    static List getAll() throws IOException {

        return Try.with(Files.walk(Paths.get("/var/log")))
                .filter(Files::isRegularFile)
                .filter(LogBook::isLogbook)
                .flatMap(path -> Try.of(new LogBook(path)))
                .onException( IOException.class, (exception, path) -> Try.empty())
                .collect(Collectors.toList());
    }
}

Unfortunately, there is no such standard API for Try/catch in standard Java-Streams, just in libraries like VAVR.

What could be improved or is missing?

In general the code formatting was fine, other than stated in on one of the reviews on Amazon. But yet, on my eBook readers (Android Kindle app on Samsung Galaxy Note 12 tablet and Kindle Oasis reader) the lines were sometimes too long. Therefore such lines continued at the beginning of the next line which makes some code snippets hard to read. Explicit line breaks would have helped.

Does the book contain a complete list of rules for clean code? Of cause, it does not, it can not and I was not expecting such, there are simply too many of such (compare with this website) Yet, I was missing a few principles which I consider very important:

  • a chapter about access rights restriction (private/protected/package/public), encapsulation, coherence and false abstractions,
  • the SOLID principles: Single responsibility, Open/closed, Liskov substitution, Interface segregation, Dependency inversion
  • and even though SLA (level of abstraction) is mentioned in a few examples, I think it deserves a chapter of its own.

I also suggest to apply the stated rules in later chapters, e.g. using `final` more often. Too bad it’s not the default in Java anyway.

Maybe these are some ideas for a 2nd edition?

Some Inspiration

While reading the book, I was wondering how the rules apply to other programming languages, e.g. Kotlin or TypeScript. As both type systems are more advanced, some rules simply become irrelevant, but for sure, other rules could come up. Especially in purely functional programming languages a set of such rules would be quite different.

Summary

In total, this book is a must-read for every Java developer to go beyond the junior status and a great reference for those who have already advanced. With a length of only about 200 pages, there is also no excuse for not having read it.

Want to see more of the book or buy it? Then have a look on their website:
https://java.by-comparison.com!


Disclaimer: I got a free copy for review.

Bibliographic References

[1] Java by Comparison: Become a Java Craftsman in 70 Examples, Simon Harrer / Jörg Lenhard / Linus Dietz, 2018 O’Reilly UK Ltd
[2] Clean Code: A Handbook of Agile Software Craftsmanship, 1st Edition, Robert C. Martin, 2008, Prentice Hall
[3] Refactoring: Improving the Design of Existing Code, 2nd Edition, Martin Fowler, 2018, Addison-Wesley Professional
[4] Effective Java, Third Edition, Joshua Bloch, 2017, Addison-Wesley Professional