Thursday, May 05, 2005

Static Code Analysis

If you would like to improve the quality of your code you can use a set of open source tools to achieve that. For Java there are many different tools, covering different aspects of static code analysis.

Please keep in mind, that automated static code analysis, never gives you a 100% answer about the quality of your code. From my point of view, it's more about getting some indications if your code has more or less options for improvements. Believe me, most of the time, there are more options for improvements then you would think of...

Code Conventions - CheckStyle

Let's start with the basic need of code conventions. If you start a new project you would like to have some conventions how the code has to be written. There are some Java code conventions provided by Sun, but you don't want to check them manually, do you?

To ensure, that your code is written according to your conventions, you can use CheckStyle. Run your checks with Ant or a command line tool.

It's highly recommended to use Ant or Maven to do the checks, since you would like to have a complete build cycle with different tools.

Code Reviews - Hammurapi, PMD

Usually there are some development guidelines, best practices or just how-to's while your working on a project, a framework or an application. These guidelines try to improve the quality of your software. But how do you check, if people follow these guidelines? Usually you do code reviews and try to find obvious mistakes. But unfortunately code reviews are very time consuming and expensive.

You can automatize certain aspects of the review process, by checking your code with tools like Hammurapi or PMD. Both tools provide rules which are organized in different groups, like: database connection pool, EJB, exception handling, and many more. Have a look at the project sites to get an overview of all the available rules.

If you have some special guidelines, which are important for your project, but not available in Hammurapi or PMD, you can write your own rules. Both tools are providing a simple interface to add new, project specific rules.

Common Bugs - FindBugs

There are many different type of bugs, you can write :-) and many times they follow a certain pattern. With FindBugs you can analyze your code and it'll check it according to a huge set of possible bugs, based on patterns.

Once again, there is no guarantee, you'll find all the bugs or it'll even point you to some code fragments which don't have a bug at all.

Testing - JUnit, TestNG

Testing is one of the most important aspects of quality. If you don't write test cases for your software, you don't know, what you are developing. Testing is a necessary precondition for refactoring. If you refactor your code, without test cases, you can't tell, if you changed something in the functionality or not.

A quasi standard testing tool is JUnit. It's a simple testing framework for unit testing. There is a huge set of tools, using JUnit as a core to test different aspcets, like HTTPUnit, Cactus or StrutsTestCase.

In the last few months an other testing framework got a lot of attention, it's called TestNG. Since JUnit has many limitations, there is a need for a more flexible testing framework.

Coverage - Corbertura, JCoverage, EMMA

While writing unit tests for your software, you would like to know, how much of your code is covered by your tests. There are quite many tools, which try to address this need. Have a look at Corbertura, JCoverage or EMMA for a start.

Many times the coverage tools are used for analyzing the test coverage of your software. An other nice aspect would be, to analyze the code which has been used during the execution of your application. These could show you, which code has been "touched" during the exectution and if you have to do some bug fixing, you would see, if a certain class is used quite often or seldom.

Test Quality - Jester

To find out, how robust your test classes and methods are, you can use Jester to test your test code.

Build Environment - Ant, Maven

As mentioned above, you should atomize your build environment. I think, the quasi standard to do this is Ant. Another very nice tool, is Maven. Both are providing the possibility to atomize a complex set of task.

Contiunous Integration - CruiseControl

Finally, you would like to analyze your code as often as possible. Now we are talking about continuous integration. Please take the time and read the article of Martin Fowler about continuous integration, I belive it's a must for every developer!

A great tool, to set up an continuous integration environment is CruiseControl. There are some other tools, but I won't mention them here.

No comments: