Cleaner Code with the PMD Eclipse Plug-In
Most developers like to write clean code. So, why is so much code in the world messy? Although there are many opinions about this, there are two contributing causes that very most everyone will agree on.
One of the reasons is that there is rarely enough time to write code as cleanly as we would like. Even when code starts clean, the continual refactoring from changing requirements, shifting dependencies, and the inevitable bug fixes (often a result of the first two factors) leads to messy code just as surely as short deadlines and long hours make an organized person's desk become littered with piles of unfiled papers and unfinished notes.
The other reason that is generally agreed on for code in the real world not being as clean as it starts out in our minds is because not everyone generally agrees with what clean code should look like. Some people are more certain that their version is cleaner than another, and there are people who hold different opinions with equal conviction. For example, in which row does the opening brace of a method belong? This is an example where I'm fairly certain I am not the only person who has had endless email threads and inconclusive meetings. The one, final answer will not be decided in our lifetime.
Even though the coding standards of an enterprise or project team may begin as a democratic process, they will not be useful as a benchmark until their definition evolves to a benevolent dictatorship (remember, I am discussing business, not government here). Once the standards are defined, a third reason for not meeting them comes into play; there are usually more rules than most folks can memorize, or remember when the time pressure is on or when the rules of one project differ from those of another. For these causes of messy code, I have found PMD to be the best solution based on its flexibility and ease of use. The letters themselves do not really stand for anything. The creator(s) just thought they sounded good together. The PMD home page supplies several "backronyms" to explain it.
The PMD home page describes the value of the project simply and concisely as something that scans Java source code for potential problems such as possible bugs, dead code, suboptimal code, overcomplicated expressions, and duplicate code.
PMD is more than just an Eclipse plug-in; in fact, it is available as a plug-in for many IDEs. It can be run from a command line or as an ANT task. This makes it perfect for Agile projects because it can be integrated into the developer's IDE and run as part of an automated build process. Even if you aren't running automated scans at build time, making it part of your IDE will allow you to write cleaner code as a developer and to speed up code reviews as a reviewer.
Eclipse Plug-in Installation
Although Google is a developer's best friend (though I really miss DejaNews), for the more mature Eclipse plug-ins it pays to read the details of your Google search results. In the early days of Eclipse plug-ins, the use of the update manager was less prevalent. Most plug-ins at that time were made available as downloads. Many of the plug-in projects that have survived since those early days have since moved entirely to the Eclipse project's preferred method of using the update manager. Even though this can be annoying to those of us who began using Eclipse in the early versions (especially when maintaining a portable tool kit), it is a much cleaner way for plug-in projects to publish their wares and make it easier for the user to get the correct version for their workspace. I mention this because, while preparing for this article, my Google search found the original download site at http://sourceforge.net/projects/pmd-eclipse. Even though it wasn't the highest ranked, it did come in third and the habit to want a download rather than an update manager URL is hard to break. Just before downloading the zip file, I noticed that site was last updated in 2005. Had I installed that version, I would have then spent part of my afternoon cleaning up the mess of my highly personalized workbench.
The currently maintained site is at http://pmd.sourceforge.net/integrations.html, where you will find the update manager URL of http://pmd.sf.net/eclipse. As a refresher from the "Building the Perfect Portable Eclipse Workbench" article, here are the steps to install PMD through the update manager:
Figure 1: Access the Update Manager from Help\Software Updates\Find and Install
Figure 2: Select Search for new features to install in the Update Manager Options