Showing posts with label eclipse. Show all posts
Showing posts with label eclipse. Show all posts

Saturday, May 23, 2009

Using PMD for reviewing Java code

Introduction
In this blog I would like to discuss about some of the code review tools that can be used in RAD (Rational Application Developer) and RSA (Rational Software Architect) or any eclipse based IDE.

Usually the code written by the developers (including me :)- ) are generally not easy to maintain and it is a herculean task to understand the code. So it is always better to use some of the open source code review tools while development, which atleast ensures code return by the developers are readable.

Here we can discuss about how to integrate PMD with some of the existing IDE's. In this case I have taken RAD.

PMD
PMD is an open source code review tool provided by sourceforge.net.

PMD is integrated with JDeveloper, Eclipse, JEdit, JBuilder, BlueJ, CodeGuide, NetBeans/Sun Java Studio Enterprise/Creator, IntelliJ IDEA, TextPad, Maven, Ant, Gel, JCreator, and Emacs.

It is an open source tool which can be used to scan the Java source code for some of the problems like:
  • Possible Bugs - like empty try/catch/finally block/if/else/switch statements

  • Dead code - unused local variables,parameters and private methods

  • Suboptimal Code - StringBuffer usage and wasteful String

  • Complicated expressions - unneccessary if statements, for loops

  • Duplicate code - copied/pasted code that repeats in the Java code

Installing

PMD provides some basic rules which are applied over the Java code, the rules which are used by the PMD are described in detail, here. Alternatively we can also create a new rules if needed or edit the existing rules by using PMD prefernces option.

Window -> Preferences -> PMD -> Rules Configuraion

The above option will be available only after installing PMD on RAD. The following steps describes about how to install PMD for RAD/any eclipse based IDE:

1) Launch RAD/any eclipse based IDE

2) Select the menu “Window -> Preferences -> General -> Network Connections”

3) Select the radio button “Manual proxy configuration:” and enter the proxy server name and port in Http Proxy option. Click Ok.

4) To install the plug-ins, navigate to “Help -> Software updates”

5) Select “Available Software” tab and click the button “Add Site”

6) Enter the value http://pmd.sourceforge.net/eclipse and click “Ok”

7) Expand the node against http://pmd.sourceforge.net/eclipse. If it throws error check the proxy settings which was done in step 3. Make sure that the proxy server and proxy port are entered properly.

8) Select the check-box against “PMD for Eclipse 3” and un-select the check-box against “PMD for Eclipse 2” and click “Install”




9) After installation it will prompt for restart.

Using PMD
After installing PMD just right click on any Java file and select "PMD -> Check Code with PMD" which will show all the code review comments in "Violations Overview" view. If not shown the choose the view from "Windows -> Show View" and search for Violations Overview view. Click Ok.

Turn off some of the irrelevant rules used by PMD using "Windows -> Preferences -> PMD -> Rules Configuration" option. The same option can be used to edit or add a new rule to PMD.

In the next post we will see how to integrate "Check Style" plugin and configure it in RAD. Bye till then.

Regards,
Elango