Wednesday, April 1, 2009

pmd applied or the chain of good things

Because the 'patching period' is close to finish and I have zero bugs in my queue now, I put some time into investigating tools that would reduce my frustration with the code, so I devoted the start of the week to PMD.

There are quite a few angles you can approach software quality from and PMD does this by analyzing the source code for mistakes and even ventures into reporting bad code design. By going after source code quality, you leverage the fact that there is greater consensus about whether code A is better than code B than if you were to decide on more general quality metrics like maintainability: how confident would you be in asserting that project A is more maintainable than project B?

It took me about one day to go over over the set of PMD rules applicable to my java desktop project; some of them I had to try out and I eventually filtered out those that I found controversial, reporting false positives and those that simply didn't feel right. I wish the project maintainers could exercise their veto more often against the rules submitted by various contributors, but I guess the philosophy is to let you use just the rules you like.

The way you can do that is create your custom rules file which will include only the PMD rules you want run - notice some of them can be customized with properties -
<ruleset name="My rules"
xmlns="http://pmd.sf.net/ruleset/1.0.0"
xsi="http://www.w3.org/2001/XMLSchema-instance"
schemalocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
nonamespaceschemalocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
<description>A custom rule set</description>

<rule ref="rulesets/basic.xml/EmptyCatchBlock">
<properties>
<property name="allowCommentedBlocks">
<value>true</value>
</property>
</properties>
<rule ref="rulesets/unusedcode.xml/UnusedPrivateField">
</ruleset>
and include it in the PMD classpath.

Two benefits I am expecting from PMD: a smaller code-base (rules like UselessOverridingMethod and UnusedPrivateMethod) and a slightly better/faster usage of the Java library classes (rules like StringInstantiation and InefficientStringBuffering). A collateral benefit could be that my team would learn to write better code after they'll correct their mistakes. What if they will be so thrilled with the experience that they will at their turn plug in new quality tools ... what if they will blog about this and spread the word to a wider audience? ... I can only imagine ...

In the end I'll just say that an improvement never comes alone: here is my humble shot at improving PMD itself:

2 comments:

Horea said...

You could also try FindBugs (http://findbugs.sourceforge.net/) the javaposse guys talk a lot about it.

Lucian Ciufudean said...

cheers, indeed, I tried it