ŷ

Jump to ratings and reviews
Rate this book

Java by Comparison: Become a Java Craftsman in 70 Examples

Rate this book
Write code that's clean, concise, and to the code that others will read with pleasure and reuse. Comparing your code to that of expert programmers is a great way to improve your coding skills. Get hands-on advice to level up your coding style through small and understandable examples that compare flawed code to an improved solution. Discover handy tips and tricks, as well as common bugs an experienced Java programmer needs to know. Make your way from a Java novice to a master craftsman. This book is a useful companion for anyone learning to write clean Java code. The authors introduce you to the fundamentals of becoming a software craftsman, by comparing pieces of problematic code with an improved version, to help you to develop a sense for clean code. This unique before-and-after approach teaches you to create clean Java code. Learn to keep your booleans in check, dodge formatting bugs, get rid of magic numbers, and use the right style of iteration. Write informative comments when needed, but avoid them when they are not. Improve the understandability of your code for others by following conventions and naming your objects accurately. Make your programs more robust with intelligent exception handling and learn to assert that everything works as expected using JUnit5 as your testing framework. Impress your peers with an elegant functional programming style and clear-cut object-oriented class design. Writing excellent code isn't just about implementing the functionality. It's about the small important details that make your code more readable, maintainable, flexible, robust, and faster. Java by Comparison teaches you to spot these details and trains you to become a better programmer. What You You need a Java 8 compiler, a text editor, and a fresh mind. That's it.

206 pages, Paperback

Published March 21, 2018

22 people are currently reading
195 people want to read

About the author

Simon Harrer

6books22followers
Simon is a consultant, developer, and teacher who's always looking out for new technologies, frameworks, and ideas. His friends call him a clean code evangelist, but in a good way.

Ratings & Reviews

What do you think?
Rate this book

Friends & Following

Create a free account to discover what your friends think of this book!

Community Reviews

5 stars
55 (44%)
4 stars
51 (40%)
3 stars
13 (10%)
2 stars
6 (4%)
1 star
0 (0%)
Displaying 1 - 29 of 29 reviews
Profile Image for Cristian.
41 reviews
August 14, 2021
This short book is great for starting developers, and intermediate developers will probably still find a couple of things worth learning. The first few examples are extremely basic, but I can see how they make sense for someone without much experience. They were also confusing/misleading (until the authors explain why a couple of pages later) because the improvements are still suboptimal (specially at the start); the idea being that they want to improve one thing at a time, and not overwhelm with changes for the comparisions.

Though I agree with almost all of it, I have a couple of disagreements, or at least remarks, e.g.:

1. They never mention the ternary operator. I think the first comparisons would have benefited further by use of it, leading to more concise and less repetitive code.
2. On page 9, they recommend extracting conditions into boolean variables to give them a name and improve readability. The suggestion is well-intentioned, but has the downside of computing more than is needed. One of the upsides of the boolean operators && and || is their lazy evaluation. A better solution would have been extracting the conditions to their own methods. You still give the condition a name, but can also reuse them in other places if need be, and you still get lazy evaluation. Note that this is something that affects languages with eager evaluation (which is the majority, including of course, Java), but extracting conditions into a variable would work perfectly fine in a language such as Haskell, since lazy evaluation ensures it won't be computed unless actually needed.
3. On page 10, they use string.trim().equals(""), as well as string.trim().isEmpty() later in the book. These are quite common, but they should mention that commonly used libraries (such as Apache Commons with StringUtils.isBlank()) have preferable (more readable and more performant) alternatives.
4. On page 13, they argue that switch statements should always have a default branch to fallback in case some new case is added and one forgets to update the code. They fail to mention that IDEs (at least Eclipse, maybe also the java compiler) have exhaustiveness checks for enums, and one can even fail the build in case there is a branch not covered by a switch. I think doing that is better than adding a redundant default case, which if ever useful, will still fail in runtime instead of compile time. (There are also switch expressions which must be exhaustive, but the book predates these).
5. The "Favor Format Over Concatenation" advice is also questionable. It's true that it's sometimes more readable--at the expense of being considerably slower, as well as sacrificing some safety: if you are mising an argument or have one too many (conversely, if you are missing a format specifier or have more than needed), the code will compile fine but cause an exception at runtime.

These things aside, and their not-so-favorable talk about static analysis at the beginning (although they do recommend using it by the end of the book), I'd definitely recommend this.
1 review
March 17, 2019
This book plays the role of a mentor that helps a Java beginner to pass the labyrinth of different solutions, coding styles, best practices, tools and libraries and to be ready to develop production ready software.

Java is not the first and not the last language I have learned. When I started coding Java I brought with me a lot of practices and habits I had acquired coding other languages. Fortunately, I had a couple of more experienced colleagues that helped me to become a Java programmer. This book can do the same. A reader who is familiar with the Java syntax and its main concepts but does not have enough professional development skills in Java can significantly improve these skills.

Experience is not always measured in years. It also depends on the working environment, colleagues, tasks. This book may be useful for experienced programmers as well. It refers to a lot of third party tools everyone should know. It leads the reader step-by-step from simple to tricky mistakes and traps and teaches how to avoid them. Several hours which are required to read the book will be compensated very soon by dozens of hours that will not be wasted on debugging.

I think that everyone will find a lot of new information here. I highly recommend this book for any developer who wants to improve the coding skills in Java or other languages.

Profile Image for Gregor Riegler.
7 reviews2 followers
November 29, 2020
This Book focuses on real world code examples and how to make them better. The Style makes it easy to read and understand, and it has lots of very good advice.

I'd recommend it for beginners and even advanced Java Programmers.

Profile Image for Peter Götz.
12 reviews2 followers
March 14, 2020
Java by Comparison is the book I would have needed 20 years ago. Its main idea is simple yet powerful: show a piece of source code that can be improved. Improve it. Then talk about why it is better now.
This book is well suited for people that are just starting to develop software in Java. It is also useful for developers with a few years of experience. And I believe also very experienced developers can take away something from it and have fun. I did and I had.
It is a pity that I am not teaching Java courses any more. This would be in my selection of books to offer students as a take-away from the course.
Now we need more books like this: Python/Javascript/Ruby/... by comparison, HTML/CSS by comparison, XML/YAML by comparison (ok, just kidding).
Profile Image for Jesus Javier.
1 review
March 14, 2019
Great book for the people who already know Java and for the people is learnig it. If you have experience programming Java, you find this book very interesting with a lot of examples with many examples of what you should not do and, above all, how to improve your programs, making them more efficient, cleaner and easier to read.
If you want your programs to be more readable and more maintainable, you should read this book.
Profile Image for Tri.
3 reviews
May 13, 2019
Pretty good book with clear examples. I would recommend for beginners to learn about Clean Code (the simple way) with Java. Experienced developer might find many common patterns that they were already fluent, in that case Effective Java would be another option.
590 reviews11 followers
May 14, 2019

Dieses Buch wagt sich an eine grosse Herausforderung: Wie kann man das über Jahre angeeignete Expertenwissen in einfacher Form Programmier-Anfängern zugänglich machen?

Die Autoren nutzen dazu 70 Beispiele, in denen ein funktionierender erster Wurf einer wartbaren und durchdachten Lösung gegenübergestellt wird. Die Unterschiede lassen sich ohne grossen Aufwand direkt erkennen. In der Druckversion ist die Ausgangslage jeweils auf der linken Seite beschrieben und das Resultat sowie die Verbesserungen befinden sich auf der rechten Seite � dadurch hat man immer beides im Blick. Dies macht es nicht nur einfach die Veränderungen zu sehen, sondern man bekommt genügend Kontext mitgeliefert, um den Unterschied zwischen beiden Lösungen auch zu verstehen.



Besonders gefallen hat mir der Teil aufräumen. Was als langweilige Pflichtaufgabe aussieht entpuppt sich als äusserst wertvolle Aktivität um den eigenen Code zu verbessern. Vereinfachte boolesche Ausdrücke, die Verhinderung von Negierung und der Verdeutlichung der eigentlichen Aufgabe sind Grundvoraussetzungen für wartbaren Code.



Die durchwegs aus der Praxis stammenden Beispiele werden gut und verständlich vermittelt. Daher ist dieses Buch aus meiner Sicht für alle Entwickler geeignet, die an wartbarem Code interessiert sind und sich verbessern wollen. Obwohl für Java geschrieben funktionieren fast alle Beispiele auch für .Net.


1 review
January 31, 2019
A good book for beginners with simple explanations that are easy to grasp. Examples are kept simple and technical jargon is kept to a minimum so it doesn’t distract from the essential and keeps our understanding high. Perfect for new developers looking to improve their code quality and make it more maintainable. Also, it provides references to authoritative books and online resources for the ones seeking further information on more advanced topics. I would recommend reading "Java by Comparison� for a new Java developer before moving onto "Clean Code" and "Effective Java.� However, I would not recommend it if you have already read these two books, as some advice (even though they are good for novice developer) seems overly simplified and can feel a bit too simplistic.
Profile Image for Daniel Di Giovanni.
7 reviews2 followers
August 3, 2022
is a nice little book as a supplement to programming experience. The early chapters are like a collection of some things I've learned through countless hours of searching through questions on StackOverflow, all in one place. The later chapters have great tips on JavaDoc, testing, streams, and functional programming. Overall a helpful resource.

I also really appreciated that the authors made the problem and the solution pages face each other for each comparison, so that you don't have to keep flipping back and forth. That was very thoughtful.

The only thing I didn't like was the way the chapters (and sometimes pages) transition to each other. It's like they're marketing the next chapter to me, saying things like "You'll learn about [topic] next, so read on!" I think the book would read better if it was more of a flat-out resource, rather than trying to maintain this weird connection with the reader.
Profile Image for Vojtěch Růžička.
6 reviews3 followers
April 12, 2019
I really enjoyed this book and I wish I had a book like this when I was starting with Java. It can be a great resource early in your career. It is easy to read, short, easy to understand and can be a good book to read before more heavy-weight titles such as Effective Java or Clean Code, which can be intimidating and harder to grasp when you are just starting.

In your early days, it can also be useful as an introduction (although very brief) to some related real-world topics such as testing, continuous integration or static code analysis. Overall I do recommend this book especially for junior developers, who will benefit from it the most.

More detailed review here:
Profile Image for Tom Hombergs.
Author6 books28 followers
January 28, 2019
Java by Comparison is a very good read for when you're starting with Java. It's very well structured, each chapter providing a "bad" code example followed by an improved version and a discussion of why it is better.

The book covers best practices on basic language features over usage of comments, naming things, exception handling and test assertions to working with streams, among other things.

Be sure to read the PDF or print version, though, because the e-Book version does not support the "side-by-side" view of the bad and the good code example!
Profile Image for Oskar Westmeijer.
26 reviews1 follower
August 12, 2022
I recommend this book for Junior Java Developers. It was an excellent read. It illustrates the fundamental programming best practices in a before and after comparison. This schema works really well!

Usually people recommend reading Effective Java or Clean Code first. In my opinion these books should be read after Java by Comparison. This book is just great for people starting out in the business.

Furthermore it is quite a compact read and I have used it already a couple of times as a coding reference.
Profile Image for Martin.
25 reviews
July 6, 2018
70 examples with java code smells on one side and their refactored clean version on the other. I think this book could be really useful to junior developers. If you've read clean code and effective java already, you don't need this book, despite giving it to your younger colleagues.
I keep it at work in the team space for the junior developers.
Profile Image for Andrew.
692 reviews8 followers
August 19, 2021
Going to recommend this one to peers. There’s a great variety of examples and ideas to use day to day. The print format is perfect for readability. I’d like to see this format for other programming languages I use.
Profile Image for Miroslav.
Author4 books12 followers
June 29, 2018
Pretty good book - definitely something that I would recommend to junior developers. However, I think that more experienced people can read it as well - there are some pretty good examples here.
Profile Image for James Stanier.
2 reviews
May 9, 2022
A compendium of useful and pragmatic advice to have by your side as you perfect your craft day in, day out. Extremely pertinent and you'll find yourself reaching for it again and again.
Profile Image for Nikos Voulgaris.
1 review3 followers
May 26, 2019
Exactly the kind of book that I would like to have read when I was making my first steps as a software engineer.

Overview
The book mainly focuses on the - very important - area of software quality, touching a broad spectrum of topics, from readability and level of abstraction to exception handling, unit testing, software design and functional programming.

Pros
The information is presented in a brilliantly systematic way, using a comparison for every topic. This is essentially a before and after version of a piece of code, with the former illustrating some problems and the latter focusing on amending these. This results in the content being very digestible and the book being almost like a catalog, which can be used again and again while writing code.

The content of the book is excellent, focusing on topics that truly hurt code bases and teams and typically constitute pitfalls for novice software engineers.

Emphasis is put on simplicity (both in language and code), which helps the reader focus on the meaning.

Despite the fact that it mainly refers to novices, I believe that every software engineer, regardless of her experience, can benefit from reading it.

Despite using Java as a medium to illustrate the principles, I felt that most of the content of the book refers to general software engineering principles (for instance, readability, code design, functional programming, unit testing and more). There are certain points that refer specifically to java, but I feel this is the exception rather than the rule.

Cons
I would love to have seen some more advanced topics covered (perhaps in extra chapters) that would mainly refer to more experienced software engineers. However, I understand that the intention behind the book was that it would refer to novice software engineers. Perhaps this would be an interesting idea for a follow-up with more advanced content.

To sum up, I truly feel that we need more books like this. Books that focus on software quality and on novice people in need of guidance and advice when making their first steps as professional software engineers.
Profile Image for Robert.
90 reviews1 follower
July 23, 2018
I am hopelessly stuck with a legacy programming language, but I have been able to make some incursions into Java in my current legacy ERP environment only because I took some time to self teach Java and earn a basic certification many years ago now. That said, my Java is still very basic even when I made a decent number of programs still running in our current production system. This book really brought me to speed in current Java developments, not in detail, of course, but in giving good pointers and hints where to go to really learn current Java skills. Now I am more hopeful to start a real Java career in the same way when I grabbed a book, studied, and became the Java "expert" in a legacy ERP environment.
3 reviews1 follower
February 17, 2019
A good book to start with Java.

I was positively surprised by this book. I think it is a good, comprehensive view on how to start writing code which is easy to understand. It is well-organised and each chapter keeping the structure which makes the content easy to understand and apply.

I definitely recommend this book to people who just started their journey with Java. This book will help to establish good habits.

It can be also a good compendium for more advanced users to remind them how important is writing code not just for yourself and how to do that.
1 review4 followers
September 20, 2018
A must-read for anyone joining our profession or seeking to grow his code crafting skills. Through suggestive examples, the book not only teaches key techniques of writing clean code, but also seeds passion for simple, elegant code in the reader by discussing each example in depth. This book is exactly what I needed to recommend my trainees asking me “for more�. Thank you! You’ve done an amazing job!
1 review
January 13, 2019
"Java by Comparison" is a wonderful book to improve your professional coding style. It is kind of a manual Sonarqube with a build-in coding mentor. It continous where "Clean code" set the basics but fully embraces modern Java by e.g. favouring functional style where feasible.

I also like the recipe format. Even if you've just have 2 minutes to spare - this is enough time for 1-2 recipes.

Well done folks!
Profile Image for Eduards Sizovs.
118 reviews168 followers
October 7, 2019
This is a good book for inexperienced Java developers and developers that are not familiar with clean code and good design principles. The book covers some basic clean coding techniques � nothing groundbreaking, pretty shallow, not as deep as the "Clean Code" book. Yet, unlike the "Clean Code", "Java by Comparison" covers the latest Java goodies such as streams.

If you are seasoned clean code adept � skip. If you are beginning your clean coding journey � read it.
52 reviews1 follower
October 6, 2020
This book presents several dozen examples of bad vs good code. All of them are valid ones (but I would throw out the chapter about comments) however they are really simplistic. Programmers with years of experience still make poor code design decisions, but this book won't teach you much if you're not an absolute beginner. They could have make the effort to prepare more interesting and challenging examples.
This entire review has been hidden because of spoilers.
1 review
May 30, 2018
The book gives several examples how to improve bad code by showing the original code and an improved version of the code. The examples are easy to understand which makes it very useful for beginners. But I would also recommend this book for advanced and expert programmers since you get a quick overview of how to write good clean code.
Profile Image for Geff Chang.
3 reviews
November 4, 2018
"Java by Comparison" is a quick read (and you don't need a computer or laptop) on various best practices in Java that both junior and senior developers can learn from. I look forward to reading "Effective Java 3rd Edition" next.
Profile Image for Kasra.
18 reviews
March 19, 2020
It's a great book for junior Java developers. It's highly recommend. I have written an in-depth review about this book in my blog,
3 reviews
December 6, 2018
Great book, for beginners to easily learn about good practices and anti patterns.
Author3 books3 followers
January 11, 2023
Targeted at beginners. Great format comparing before and after, well selected code examples.
Displaying 1 - 29 of 29 reviews

Can't find what you're looking for?

Get help and learn more about the design.