Tuesday, April 04, 2006

Checkstyle - Coding standards

Quality code in my opinion is an important step towards quality software.

I believe having a well-defined set of coding conventions is necessary. To produce code that is readable, reusable and maintainable, it is needed (among other things like having good programming habits) to follow some code writing conventions. I use Eclipse for my IDE. And with it, Checkstyle - the tool that helps write Java code that adheres to a coding standard.

There exist many coding standars. For personal use, I recommend the easiest form: to follow the Sun coding conventions. Of course, you may personalize it. This will help you in coding and your code, to have a standard format. In case of companies, they tend to have different coding standards: you have probably experienced a few, if you have worked already for a couple of different companies. As companies usually develop their own coding standards, sometimes very different from the Sun coding conventions, other times with minor alterations. This is good in either ways: code within a company needs to be maintained, reused, and improved. So this is a small assurance of better code. When a programmer or programmers have a well defined set of coding standards, they will think less about details such as code layout and variable naming conventions, spending more time on the real coding.

Some coding conventions that I find important:

  • Comments: all classes, methods, and variables should have
  • Naming conventions: constants should be in uppercase; variables, methods should have talkative names and should not be abbreviated
    (e.g.: private Map myHashMap;);
  • Declaration: all variables should be declared with visibility modifiers; each declaration should be on separate line

Checkstyle can check many aspects of your source code. You may want to give it try, as it now supports features like: checks for class design problems, duplicate code, and standards related to: Javadoc comments, naming conventions, import statements, white space, modifiers, blocks, etc.

If you already want to add it to your Eclipse IDE, you may find here some hints. As well, you are able to set various highlighting modes.


Technorati tags: Checkstyle, Eclipse, Java, tool, tutorial.