Sunday, March 16, 2008

3 'fantastic' static code analysis tools for eclipse

Its always a hard task to review one's code to make sure it follows some pre-defined coding standards. The reason its hard is not because its difficult to do, but its because its often boring and repetitive task to do. Adhering to 'Coding standards' while we write a piece of code is undoubtedly a very important responsibility for a programmer. Sometimes its as good as testing our functionality to check if it actually works properly or not.

There're are many advantages that come in as a bonus for writing code as per the 'Coding standards'.
  • Our code looks beautiful and easy to understand
  • Its very easy to introduce new people into the projects as they can understand the code quickly.
  • Its easy to modify the code for further changes.
Writing code as per the coding standards is like 'testing' an application. Both the tasks share the same qualities...
  1. They both have pre-defined set of rules ( test cases ) that we should follow every time we want to perform the task
  2. Both the tasks require execution of a sequence of checks/validations on the piece of code.
Since we always have to execute a sequence of steps and verifications in order to finish the task successfully, this is where automation comes into picture. We have several tools available outside to automate the testing process and similarly we have tools to verify 'coding standards' in our code. These tools are normally termed as 'Static code analysis tools' outside.

In this article i'll discuss about the static source code analysis tools that are available as plug-ins to famous 'Eclipse' IDE.

FindBugs:

Findbugs is a static code analysis tool available for eclipse IDE as a plug-in. We can use this tool against a whole eclipse project or just a simple java program to find out trivial issues like 'Possible null pointer exceptions' etc. This plug-in is available as download from the eclipse update site itself. Its easy to install and it takes no time to test it against any java project.

PMD:

PMD is another plug-in that is available for several IDEs including eclipse. The functionality of PMD is pretty much the same as 'Findbugs' except that when we apply the tool against a project, all the code violations are integrated into the code and will be shown as errors and warnings in the code itself. It'll be very easy to use this tool as we don't need to goto any seperate eclipse 'view' to check the list of errors. They'll be shown directly in the code itself. PMD also allows us to extend or customize the validation functionality as per our requirements. If you want to extend the functionality of a validation you might need to override the API that comes along with this and plug-it into the PMD itself. Most of the times, the default set of validations that come pre-packaged with this plug-in itself would be sufficient to solve most of the commonly occurring problems. You can install the plug-in through its update site here.

Check Style:

Check style plug-in is another static code analysis plug-in available for eclipse. This is so far the most configurable plug-in that I come across lately. It comes pre-packaged with lot of rules with it and still it supports overrides for literally every rule that it defines in its modules. You can optionally include any module you want for running check style in your project. You can also use regular expressions for refining/tuning each rule. You can download this as a standalone program from its site itself. If you want to download it as a plug-in to your eclipse you can find a nice one here.

Conclusion :

Nobody's perfect, each plug-in listed above works very well in very specific situations and hence can't be applied in the generic sense. We can't apply the same rules in all the situation without customizing the tool for our requirement. So, in order to get the best results suggestible to use a combination of these plug-ins in our code.