1. What is PMD ?
PMD is a source code analyzer.
It finds unused variables, empty catch blocks,
unnecessary object creation, and so forth.
2. PMD works by scanning Java code and checks for violations
in three major areas:
– Compliance with coding standards
– Coding antipatterns
– Cut and Paste Detector (CPD)
3. Compliance with coding standards
● Naming conventions
– class, method, parameter and variable names
● Class and method length
● Existence and formatting of comments and JavaDocs
4. Coding antipatterns
● Possible bugs - Empty try/catch/finally/switch blocks
● Dead code - Unused local variables, parameters and private methods
● Empty (if/while) statements
● Suboptimal code - wasteful String/StringBuffer usage
5. Coding antipatterns (2)
Overcomplicated expressions
– unnecessary if statements, for loops that could be while loops
Duplicate code
– copied/pasted code means copied/pasted bugs
Classes with high Cyclomatic Complexity measurements
6. Cut and Paste Detector (CPD)
a tool that scans files and looks for suspect code replication.
CPD can be parametrized by the minimum size of the code block.
7. Stakeholders
● 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.
● Team members can do peer-to-peer discussion
PMD has the potential to transform a mechanical and syntax check
oriented code review into a to dynamic peer-to-peer discussion.
8. Rules
PMD comes with many predefined rules; we can add more or
customize existing one.
EmptyFinallyBlock
– Avoid empty finally blocks - these can be deleted
UnnecessaryReturn
– Avoid unnecessary return statements
LongVariable
– Detects when a field, local variable is declared with a long name