Use PMD - Programming Mistake Detector?

About PMD

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:

  1. Possible bugs - empty try/catch/finally/switch statements
  2. Dead code - unused local variables, parameters and private methods
  3. Suboptimal code - wasteful String/StringBuffer usage
  4. Overcomplicated expressions - unnecessary if statements, for loops that could be while loops
  5. 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.

  1. Start Eclipse.
  2. Start the installation procedure : select the Help>Software Updates>Find and Install… menu item.
  3. Select “Search for new features to install” option and click Next.
  4. Click New Remote Site…
  5. Give a name (ie PMD Eclipse Site), enter the URL
  6. Select this new site in the Sites to include in search list and click Next.
  7. Select PMD for Eclipse 3 and Apache Xerces in the “Select the features to install” list and click Next.
  8. Accept the terms of the license agreements and click Next.
  9. Verify that the install location is your Eclipse installation directory, otherwise select the correct one, click Finish.
  10. A warning appear telling the feature is not signed. Ignore and click Install to continue.
  11. 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.

  1. Write a rule using Java
  2. Write an XPath expression More details about each of these approaches can be found at sourceforge.

Happy coding..

Similar Articles

Linked data based integration
New Date and Time API in Java8
Re-platformig this blog to Wintersmith
Improving code quality in agile teams
Java7 updates

blog comments powered by Disqus