Thursday, October 18, 2007

Improving Code Quality with PMD and Eclipse

PMD is a static code analyzer for Java. Developers use PMD to comply with coding standards and deliver quality code. Team leaders and Quality Assurance folks use it to change the nature of code reviews. PMD has the potential to transform a mechanical and syntax check oriented code review into a to dynamic peer-to-peer discussion.

What is PMD?
PMD works by scanning Java code and checks for violations in three major areas:

Compliance with coding standards such as:


Naming conventions - class, method, parameter and variable names
Class and method length
Existence and formatting of comments and JavaDocs
Coding antipatterns such as:

Empty try/catch/finally/switch blocks
Unused local variables, parameters and private methods
Empty if/while statements
Overcomplicated expressions - unnecessary if statements, for loops that could be while loops
Classes with high Cyclomatic Complexity measurements
Cut and Paste Detector (CPD) - a tool that scans files and looks for suspect code replication. CPD can be parameterized by the minimum size of the code block.
In its current version, PMD comes packaged with 149 rules in 19 rulesets. Most of these rules can be parameterized at runtime by supplying properties or parameters. The standard package offers many well-thought rules. In addition users also have the ability to add their own rules for particular coding convention or quality metrics.

Here are some of the rules distributed with PMD:

EmptyFinalizer - If the finalize() method is empty, then it does not need to exist.
EmptyFinallyBlock - Avoid empty finally blocks - these can be deleted.
UnnecessaryReturn - Avoid unnecessary return statements
OnlyOneReturn - A method should have only one exit point, and that should be the last statement in the method.
CyclomaticComplexity - Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.
TooManyFields - Classes that have too many fields could be redesigned to have fewer fields, possibly through some nested object grouping of some of the information. For example, a class with city/state/zip fields could instead have one Address field.
LongVariable - Detects when a field, formal or local variable is declared with a long name.
NoPackage - Detects when a class or interface does not have a package definition

No comments: