Top five programming errors in Java

Top five programming errors in Java

Everyone makes errors. Even super-qualified experienced developers make errors. Sometimes it happens due to lack of attention or an unclear API. However, those mistakes are rare. Most common programming errors are evidence of insufficient knowledge or skills.

In this article, I would like to talk about the most frequent errors that I have met during my professional programming career. I hope that my experience will help other reduce the number of bugs in modern projects.

The first error I want to talk about is poor naming of the code elements. Each variable, method, class or package should have an exact precise meaning. How can you check that? You can return to the code that you wrote half a year ago and have not seen after that. Can you remember the meanings of all the code elements?

Good names or code identifiers are easy to do. Use the same words that you would use to describe the code element. For example, look at this method:

errors in Java 1.png

What does it mean for you? Can you use it? How should you implement it? These questions will be unanswered unless you know the author of this code. All the questions will disappear if you rewrite this code:

errors in Java 2.png

The second error is a violation of one of the OOP principles – encapsulation. You must never access attributes of other objects directly or indirectly. The only exclusion of this rule is presenting data for the UI layer or sending to DAO layer. For example:

errors in Java 3.png

Although we call getters and do not access fields of the User class, we violate encapsulation. You must never be aware of the internal data presentation in other classes. Instead, you should know which operations this class provides and how to use it.

The third error is a lack of safety in your code. Let’s take this sample:

errors in Java 4.png

You wrote a very simple percentage formatting routine. Is it safe? Yes, it never throws an exception. However, what if someone passes a negative value, for example, -1. This method produces an invalid result. Can we modify this code in the following way?

errors in Java 5.png


It will never produce an invalid result. However, we just hide an error. The fact that someone passes an invalid parameter is sign of another programming error outside of our class. If we have the latter error it will never become known.

The fourth error is an absence of the test automation. Whenever you write a code that should write a test that verifies that code. If you have projects with millions of line of code or a legacy system without tests at all then you should write tests at least for the most critical or often used parts of your system.

The fifth error is code duplication or violation of the DRY principle. Every time you copy part of the code into a new location, you should remember that you would have to modify both code blocks simultaneously. What if you have 10 or 100 such code blocks? How can you quickly find all this code blocks? How can you find it if you slightly change some of them?

errors in Java 6.png

What does 1 mean here? Does it mean that if delta will be 2 index must be 2 as well? Hard-codes values are sometimes called magic numbers and are sign of the hidden duplication as well.

So these are in my opinion the top five programming errors which I have encountered Java. I hope that my experience can help you improve the way you work.

Sergey Morenets
Software Development and Java Specialist