|
From 100 Java Mistakes and How to Avoid Them by Tagir Valeev This book shows you how to improve your Java code by identifying and dodging common programming problems. Inside, you’ll find one hundred errors, from missteps that trip up beginners to mistakes even Java experts don’t know they’re making. |
The aim of this book is to help software developers produce less bugs in Java programs. Fixing bugs right when the code is written is much cheaper than at later stages of quality assurance or after the release. When a bug slips into production, it may be minor and unnoticed, but it may also cause significant money loss and even create life-threatening situations.
The book reveals the pitfalls that you may encounter in the Java language itself and in its most used library methods. For each mistake, I try to formulate ways to avoid or quickly detect it. You’ll be able to train your eye and not only avoid mistakes by yourself but easily spot suspicious patterns in your colleagues’ code. I hope you’ll enjoy reading, learn many interesting things about Java and discover how to make static analyzers your friend.
Every software developer introduces bugs to the code. There’s no way to avoid them completely. Some of the bugs cause very subtle change in program behavior and nobody cares about it. Many of them are in the middle: they don’t have disastrous effects but they annoy users and require hours of debugging and fixing.
Some bugs are caused by misunderstanding of the specification requirements, miscommunication between the software development team and the customer, and so on. Another category of bugs comes from the miscommunication between the developer and the machine. That is: the developer correctly understands the problem but writes the wrong code, so the machine solves the problem incorrectly. This book is devoted to the latter category.
Some bugs are complex and unique. For example, one module may fail to handle a rare edge case, and another one may rely on that edge case. Everything was fine until these modules started talking to each other. In rare cases, three or more independent components are involved, and the problem appears only when all of them are connected to each other. Investigation of such problems can be as exciting as a good detective story.
However, there are also repetitive bugs that are produced by many programmers in many programs. Experienced developers have seen a number of such bugs in their practice, so they know in advance what kind of code is dangerous and requires special care. They can spot the erroneous code patterns just because they already saw something similar in the past. Less experienced developers overlook repetitive bugs more often, so the bug has more chance to slip into production and cause serious consequences.
The purpose of this book is to summarize this experience. I list the common mistakes that appear in Java programs. For every mistake discussed in the book, I show a code example, explain why this problem usually happens and advise on how to avoid the problem.
The structure of this book
The book is organized into chapters. Chapter 1 is a short introduction that lists common methods to avoid bugs. The subsequent chapters are split into sections that cover various individual mistakes. They are mostly independent, so feel free to skip something if you already know the given pattern or feel that it’s not applicable to your daily job.
Chapter 2 shows the mistakes inside individual expressions, such as problems with precedence, mixing one operator with another, or pitfalls with variable-arity method calls. Chapter 3 concentrates on mistakes related to the structural elements of a Java program. This includes problems with statements like for-loops, as well as higher-level structure issues like circular initialization of superclass and subclass.
Chapter 4 covers issues that happen when you work with numbers in Java, including the infamous problem with numeric overflow. Chapter 5 concentrates on several of the most common exceptions that happen in Java like NullPointerException
or ClassCastException
. Chapter 6 tells about the caveats of string processing.
Chapter 7 is devoted to comparing objects and speaks mostly about using and implementing methods like equals()
, hashCode()
and compareTo()
. Chapter 8 concentrates on mistakes that happen when you use collections and maps. Chapter 9 covers some easy-to-misuse library methods not covered in previous chapters. Finally, Chapter 10 describes mistakes that may happen when writing the unit tests.
The appendix briefly describes how to enhance some static analysis tools to catch the problems specific to your project. It’s optional to read, but if you are ready to introduce custom static analysis rules to your project, it may serve as a starter guide.
Who this book is for
I believe that this book is most useful for middle-level software developers who already know the Java language but may have not enough practical programming experience. It is likely that some bug patterns described in the book may be unknown to senior software developers as well. Less experienced developers or even advanced students might also find this book interesting.
What readers will learn
This book takes a dive into common approaches to avoiding bugs, with example scenarios and explanations of how to solve them and why it is important to do so. Common approaches to solving issues with code include code review, pair programming, static analysis, and various kinds of testing including unit-testing, property testing, integration testing, smoke testing, and so on.
Furthermore, readers will learn to:
- Write better Java programs
- Recognize common mistakes during programming
- Make less bugs, saving time for debugging and testing
- Use static analyzers to help during programming
- Configure static analysis tools to reduce amount of false reports
- Extend static analysis tools with custom plugins to get the maximum benefit from them
If you interested in seeing more, check out the book here.