<?xml version="1.0"?><rss version="2.0">
<channel>
  <title>Alan&#039;s Ramblings - pmd tag</title>
  <link>http://bleaklow.com:80/tags/pmd/</link>
  <description>My opinions may be incorrect, but they are my own</description>
  <language>en</language>
  <copyright>Alan Burlison</copyright>
  <lastBuildDate>Wed, 29 Feb 2012 20:50:00 GMT</lastBuildDate>
  <generator>Pebble (http://pebble.sourceforge.net)</generator>
  <docs>http://backend.userland.com/rss</docs>
  <image>
    <url>http://bleaklow.com/images/misc/logo.gif</url>
    <title>Alan&#039;s Ramblings</title>
    <link>http://bleaklow.com:80/</link>
  </image>
  <item>
    <title>PMD: mostly good</title>
    <link>http://bleaklow.com:80/2005/06/11/pmd_mostly_good.html</link>
    <description>
          &lt;p&gt;
&lt;a href=&#034;http://blogs.sun.com/roller/page/tor/20050611#netbeans_plugins_i_use_part&#034;&gt;Tor Norbye&lt;/a&gt; has done a writeup on &lt;a href=&#034;http://pmd.sourceforge.net/&#034;&gt;PMD&lt;/a&gt;, a tool that looks for common coding errors in your Java source, for example switch statements without default labels or use of the broken &lt;a href=&#034;http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html&#034;&gt;double checked locking&lt;/a&gt; pattern.    It looked interesting, so I installed the plugin into NetBeans and ran it against the source of my Mobile Bollocks MIDlet.  As Tor points out, the default set of selected rules is a bit naff in areas, so I followed his recommendation and enabled them all, then started removing the ones I didn&#039;t agree with - about 30 in total.  PMD is probably the best tool of its type that I have used, however that&#039;s not to say it doesn&#039;t have a few quirks:
&lt;/p&gt;

&lt;p&gt;
&lt;ul&gt;

&lt;li&gt;
It barfs on perfectly OK code.  For example it reports the following error:
&lt;pre&gt;
com.bleaklow.bollocks.ui.About [1]: Error while processing com.bleaklow.bollocks.ui.About;
net.sourceforge.pmd.ast.ASTAllocationExpression
&lt;/pre&gt;
This is triggered by code of the form:
&lt;pre&gt;
    public class Foo {
        public Foo() {
            new Runnable() { public void run() { Foo.this.back(); } };
        }
        public void back() {
        }
    }
&lt;/pre&gt;
The trigger seems to be &lt;code&gt;Foo.this.back()&lt;/code&gt;, I&#039;ve logged the problem - see this &lt;a href=&#034;http://sourceforge.net/tracker/index.php?func=detail&amp;aid=1218779&amp;group_id=56262&amp;atid=479921&#034;&gt;SourceForge bug&lt;/a&gt;.
&lt;/li&gt;&lt;br /&gt;

&lt;li&gt;
Some of the rules which claim to catch a particular error don&#039;t just catch the error that they claim to, they report errors against perfectly OK code.  For example the &lt;code&gt;SimplifyBooleanExpressions&lt;/code&gt; rule claims it is for problems like this:
&lt;pre&gt;
public class Bar {
    // can be simplified to
    // bar = isFoo();
    private boolean bar = (isFoo() == true);
    public isFoo() { return false;}
}
&lt;/pre&gt;
which I agree is a bad practice, the problem is that it also flags things like this as being errors:
&lt;pre&gt;
    if (isFoo() == false) {
        doSomething();
    }
&lt;/pre&gt;
which I actually think is clearer than the alternative:
&lt;pre&gt;
    if (! isFoo()) {
        doSomething();
    }
&lt;/pre&gt;
That &lt;code&gt;!&lt;/code&gt; is easy to miss, so I think the explicit comparison against &lt;code&gt;false&lt;/code&gt; is actually clearer.
&lt;/li&gt;&lt;br /&gt;

&lt;li&gt;
Some of the rules are just plain stupid - for example insisting that a method contains only a single &lt;code&gt;return&lt;/code&gt; statement or prohibiting single-character variable names for things like array indexes.
&lt;/li&gt;&lt;br /&gt;

&lt;li&gt;
Some of the rules are well-intentioned but the implementation is wrong.  For example the &lt;code&gt;AvoidInstansiatingObjectsInLoops&lt;/code&gt; rule is trying to warn you about the potential performance problems that can occur if you continually re-instansiate objects inside a loop, for example:
&lt;pre&gt;
    for (i = 0; i &lt; someArray.length; i++) {
        String s = &#034;some string&#034;;
        doStuff(someArray[i], s);
    }
&lt;/pre&gt;
the problem is that it complains about common idioms such as this, which are perfectly fine:
&lt;pre&gt;
    for (int i = 0; i &lt; someArray.length; i++) {
        if (matchesCondition(someArray[i]) {
            matchedThing = new SomeObject(someArray[i]);
            break;
    }
&lt;/pre&gt;
I guess what it should &lt;strong&gt;really&lt;/strong&gt; be looking for is the instantiation of an object inside a loop that isn&#039;t predicated by a conditional statement.
&lt;/li&gt;

&lt;/ul&gt;
&lt;/p&gt;

&lt;p&gt;
The other problems I have with PMD are more related to missing functionality, and the degree of integration of PMD into NetBeans:
&lt;/p&gt;

&lt;p&gt;
&lt;ul&gt;

&lt;li&gt;
Although the developer documentation implies the rules can have priorities, they don&#039;t appear to be used, at least not in NetBeans.  This means that it&#039;s difficult to spot rules that have caught real coding errors in amongst a sea of stylistic warnings.
&lt;/li&gt;&lt;br /&gt;

&lt;li&gt;
There is no way to suppress rules once you have manually checked that the code is actually OK.  Ideally you would be able to disable rules on both a file and method basis, but you don&#039;t seem to be able to do this.
&lt;/li&gt;&lt;br /&gt;

&lt;li&gt;
You also need to be able to have different rulesets for different projects.  For example I got gazillions of errors related  to the use of &lt;code&gt;Vector&lt;/code&gt; instead of one of the &lt;code&gt;Collection&lt;/code&gt; classes - this would be a fair warning under normal circumstances, but J2ME doesn&#039;t actually provide anything other than &lt;code&gt;Vector&lt;/code&gt;, so it would be useful to disable those rules for J2ME projects.
&lt;/li&gt;

&lt;/ul&gt;
&lt;/p&gt;

&lt;p&gt;
However, even with these provisos it is a great tool and it did find a couple of genuine bugs in my code which my code reviewer &lt;i&gt;cough Gary cough&lt;/i&gt; missed ;-)
&lt;/p&gt;</description>
      <category>Tech</category>
    <category>Java</category>
    <comments>http://bleaklow.com:80/2005/06/11/pmd_mostly_good.html#comments</comments>
    <guid isPermaLink="true">http://bleaklow.com:80/2005/06/11/pmd_mostly_good.html</guid>
    <pubDate>Sat, 11 Jun 2005 07:09:30 GMT</pubDate>
  </item>
  </channel>
</rss>

