Book Review: PMD Applied

A few weeks ago, Tom Copeland author of PMD Applied was nice enough to give me some time for an interview on CodeSnipers. I figured the least I could do is give him and the community some feedback on his book. Read on for details.

Once upon a time, I was involved in a rather large Java web project. This codebase was huge, under active development, and in need of a serious overhaul. As far as I was able to discover, there were no less than eight people's hands and development styles thoroughly embedded throughout the code. It was a mess, but it was mostly functional, so it was hard to complain. Until I realized that the system was still deployed and jar'd by hand.

My first step was to start building Unit Tests and an Ant build script around it. After quite a bit of hacking and learning, I was able to close a few bugs and simplify the build/deploy cycle into a one-button process (+1 on the Joel Test). But there were still few coding standards in place and there were quite a few things that “just didn't look right”. Fast forward a few more months and I caught a presentation from Tom Copeland who also brought along some copies of his book. I bought one immediately and dove in.

First of all, you must realize: PMD is NOT a substitute for any type of testing. Second, it's not a source code formatter like jalopy. It does a bit of that, but it is a code analysis tool to check through your logic and makes suggestions to encourage clarity.

Next – and the book states this pretty clearly – if you run all the default rules, you're going to drive yourself nuts because some rules are contradictory. It recommends starting with the CPD (more below) and the “unused variable” checks and grow from there as you need. I ignored the book and did my own thing when I began… just trust the book.

It then dives into how to get PMD involved in your development and build process. It goes into detail on building the proper Ant and Maven tasks in addition to using the PMD plugins for the various IDE's. Using this information, I was able to add it to an entire series of Ant builds and my IDE in about 15 minutes. And the best thing is that now I can get feedback on the fly.

The next thing the book discusses is the Copy Paste Detector (CPD). As a devotee of refactoring, unit testing, etc, I found this to be of HUGE assistance and immediately implemented it. I was quite happy to find that it only found eleven instances of duplicated code and most of them were known and existed to support a legacy system. The book goes into detail on how to tweak the CPD and even customize it for other languages, so this may find itself into some of my other non-Java projects…

Next, Tom goes into detail about the different rulesets and writing your own. This is the area where he starts pointing out how PMD is used as a guide as opposed to a hard and fast set of requirements. He goes into great detail here about how to create rules for your team and implement them into the build process. Since PMD and PMD Applied focus on improving clarity of code by cleaning up logic, it makes quite a bit of sense to customize your rules to your needs. The ability to draft them via XPath and then implement them in Java is a huge plus in terms of rapid prototyping and should simplify the development process. I haven't started digging into this yet, but I have some ideas.

Custom report generation is covered early in the book, but is more useful once you've read about the different rulesets. The default reports are plain html, xml, csv, and are nothing special. Of course, if you wanted to add more information or color code the different types of rule-breakages, this would be useful to provide a quick summary of things that need particular attention. That is on my personal agenda.

Finally, the book gets into the guts of the different rules. It gives the XPath implementation of many of these rules and where – if anywhere – these rules are broken within Java itself. I found some of those remarks funny because it's good to know that Sun makes mistakes too, but if you're just looking to use PMD as opposed to extend it, skip it. This is overkill for just starting off and may be a bit scary.

Overall, I say the book and PMD itself are solid resources to have in any Java developer's library and lib directory. Face it, you're not a perfect developer and neither am I, but by implementing it in our project, I was able to catch a multitude of unused variables, illogical constructs, and nasty nested loops which did things like open and close the same file repeatedly. PMD will not serve as a replacement for Unit Testing, but can highlight areas where particular attention should be focused.

For Java developers, I'd give it a 9/10. Unfortunately, other than the Copy Paste Detector, it's simply not applicable for others.

If you're interested in his book, it can be found at http://pmdapplied.com/ and you can read about PMD's ongoing development on Tom's personal blog. I get no benefit from the sales or success besides the interview he already gave.