Use PMD - Programming Mistake Detector?
How do you ensure that your code follows standard programming principles? For most Java development projects going on these days the answer would be to use “PMD”. It is aiming towards becoming a de-facto tool for analyzing the source code and is being used by more and more Java applications everyday.
Note: There are a lot many tools that PMD competes with i.e Checkstyle, FindBugs, Hammurapi, Soot, Squale etc. However exploring capabilities of these tools(other than PMD) are out of scope of this article.
PMD is a static rule set based Java source code analyzer that identifies potential problems in the code like:
- Possible bugs - empty try/catch/finally/switch statements
- Dead code - unused local variables, parameters and private methods
- Suboptimal code - wasteful String/StringBuffer usage
- Overcomplicated expressions - unnecessary if statements, for loops that could be while loops
- Duplicate code - copied/pasted code means copied/pasted bugs
You can download everything from here, and you can get an overview of all the rules at the rulesets index page.
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.
Instructions for configuring PMD for Eclipse
Let me walk you through the steps required to quickly configure it with your Eclipse installation. It works like a breeze.
- Start Eclipse.
- Start the installation procedure : select the Help>Software Updates>Find and Install… menu item.
- Select “Search for new features to install” option and click Next.
- Click New Remote Site…
- Give a name (ie PMD Eclipse Site), enter the URL http://pmd.sourceforge.net/eclipse
- Select this new site in the Sites to include in search list and click Next.
- Select PMD for Eclipse 3 and Apache Xerces in the “Select the features to install” list and click Next.
- Accept the terms of the license agreements and click Next.
- Verify that the install location is your Eclipse installation directory, otherwise select the correct one, click Finish.
- A warning appear telling the feature is not signed. Ignore and click Install to continue.
- Accept to restart the workbench to load PMD into the workbench. Eclipse is restarted and a PMD welcome page is displayed : the plugin is correctly installed.
Writing custom rules
It is interesting and important to note that you can write your custom rules. Writing PMD rules is cool because you don’t have to wait for PMD team to get around to implementing feature requests. Following are the two approaches to write custom rules.
- Write a rule using Java
- Write an XPath expression More details about each of these approaches can be found at sourceforge.
|Linked data based integration|
|New Date and Time API in Java8|
|Re-platformig this blog to Wintersmith|
|Improving code quality in agile teams|