Good code is a result of a large number of factors and attention to details. Things from architecture to your knowledge as a programmer all contribute to the quality of your code. One thing that can help raise the quality of your code is the tools you are using. Recently I had a great experience with a couple of small, simple, tools that had a large quality impact upon my code.
According to its site, checkstyle is a development tool to help programmers write Java code that adheres to a coding standard. Simple enough, but what does coding standard have to do with code quality? Well, several things.
- When code is written according to a standard it makes the code easier to read. This is of a crucial importance in groups of developers, but has its benefits in one man teams as well. Often code is written on different machines, with different editors. Writing the code according to a standard makes it easier to read, which helps the developer focus better.
- Checkstyle uses by default the Sun Coding Convention. While I don't agree completely with the SCC I found its requirement for Javadoc to be rather helpful. Many times I come back to code I haven't dealt with in a long time and having the Javadoc ready to jump and explain things is always a great memory refreshener.
- When going through the code again in order to write Javadoc or alter things in order to adhere to the SCC I started refactoring code that I found ugly, not optimal or just simply not ready for extension. I would argue this kind of sessions help improve the overall quality of the code.
FindBugs uses statical analysis to look for bugs in Java code and it's great at spotting situations where a NullPointerException is possible or places where the code might have security or performance issues. In one project I checked, it raised signals over several situations where the parameters passed to a method were not thoroughly checked and NullPointerExceptions could appear.
In another project FindBugs helped spot a situation where an essential multithreading bug could exist.
How to use them
For Android development I use Eclipse and it turns out both of these tools have Eclipse plugins which are easy to install. Just head over to the Checkstyle and Findbugs websites and instructions for installing in Eclipse should be easily findable.
Checkstyle and Findbugs are great tools for raising the quality of your code and they should be part of your development essential tools along with the interactive debugger, syntax checking and unit testing.