Top five programming errors in Java
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:
The third error is a lack of safety in your code. Let’s take this sample:
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?
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