Cleaner Code with the PMD Eclipse Plug-In, Page 3
If one reason for messy code is the differing opinions of what constitutes clean code, the expectation that the default rules will be perfect for every project is a bit ambitious. The PMD project takes this into account by making it very easy to customize which rules to enforce and what level of attention they should be given.
The basic view generally won't need to be changed:
Figure 7: Basic PMD Option
The predefined rules can be edited and removed easily in the preferences view. Changing descriptions to match the text of your coding standards can make the standards themselves easier to remember. As running the code check becomes more of a habit, most developers will tend to have fewer violations because correcting them reinforces remembering how to code to the standards.
Figure 8: Customize Rules with Configuration
One key option is the violation level. In the code check results view, there are color-coded toggles to set what level of violations to show (see Figure 6). When time for code review is limited, selecting the higher priority level (lower numbered) violations can help developers and reviewers focus on the most urgent violations.
When determining what level a violation should be, it is a good idea to avoid the temptation to go too high because the top level violations will prevent compilation of code.
Figure 9: Error High Prevents Compilation
If you are adopting PMD mid project, setting too many violations at the highest level can bring project progress to a screeching halt or drive developers to remove the plug in, both results defeating the purposes of improving quality while saving time. However, if PMD is part of your environment from the first line written, high violation settings can lead to improved code quality throughout the project.
PMD also allows for creating your own rules, a task that is far beyond the scope of this article. Full documentation is available at http://pmd.sourceforge.net/howtowritearule.html. Most teams will find that customizing the descriptions and priorities of the large selection of pre-defined rules will be more than adequate for their needs.
Once the rules have been customized to match your standards, they can be exported for sharing across the enterprise or team.
Even though the tool itself is simple to install, customize, and use, creating practices and policies to get the most of its use may take a bit more work. My personal preference is to make sure all violations at all levels have been addressed prior to the completion of the QA phase. Even with the best-defined rule sets, there will be some exceptions to the rules, and the project allows for this by providing a "Mark as reviewed" option. Using this option adds an annotation at the end of the line of code that will allow the code check to skip that violation in future checks.
PMD is a great tool for improving code quality, developing good habits, and speeding up code reviews. It is not a panacea that can completely replace manually reviewing code. Code is an art as well as a science and automated tools have a long way to go before a level of heuristics that can be 100% reliable will be reached.
About the Author
Scott Nelson provides optimization services designing, developing, and maintaining web-based applications for manufacturing, pharmaceutical, financial services, non-profits, and real estate agencies for use by employees, customers, vendors, franchisees, executive management, and others who use a browser. For information on how he can help with your web applications, please visit http://www.fywservices.com/. He also blogs all of the humorous emails forwarded to him at Frequently Unasked Questions.