PMD is a great and widely-used source code analyzer which finds common programming flaws and bad coding practices in source code projects. This document intends to help users make a better use of this helpful tool, by proposing simple yet effective corrective actions to improve the project quality according to PMD XML results.
The version of PMD used is 5.3.1
and the PMD run was executed on the 2018-11-17 00:31:46
.
You can learn more about this module on its documentation page on the project web site.
PMD raised a total of 181408
violations to checked rules, including:
1152
with priority 1,2017
with priority 2,175733
with priority 3,2506
with priority 4.Rules can be considered as coding practices. They represent what the community believes to be right or wrong, althougth it heavily depends on your own context. In this very case:
87
rules have been checked.71
broken rules, and 16
respected rules.18.4
%.This plot shows the proportion of rules violated (NOK: red) and clean (OK: blue). The lightness decreases with the priority (P1 -> p4).
Once you have selected the right rules, and made sure you understand them, then you should strive to keep the number of violations low, or even null. For each violation, you should either fix it or acknowledge it. As you will see, there will always be cases where the rule is smart, but doesn't apply to a specific instance in your code â because there is simply no bullet-proof generic truth. But once you know the rules, you know when to bend them, and it is ok: PMD allows you to ignore or suppress the incriminated warnings if you know what you are doing.
There are a few things to keep in mind when using PMD to improve your code:
From there, the next practical thing to do is either to:
The table on the right shows the 10 top files with high-priority violations. NCC P1 is the number of violations with priority 1 in the file, and NCC P2 is the number of violation with priority 2 in the file. Fixing them would be a good start. When you are over, re-run the analysis and fix the 10 next, until all high-priority violations are fixed.
You should start with files that have a lot of high-priority violations. The current analysis unveils:
3169
violations with priority 1 or 2, distributed in 1401
files. Those are further decomposed in:1152
violations with priority 1, distributed in 821
files, and2017
violations with priority 2, distributed in 693
files.File | NCC P1 | NCC P2 |
---|---|---|
. . /cdt/internal/core/dom/parser/cpp/semantics/CPPSemantics.java | 0 | 45 |
. . se/cdt/internal/core/dom/parser/cpp/semantics/CPPVisitor.java | 0 | 43 |
. . /core/dom/parser/cpp/semantics/TemplateArgumentDeduction.java | 1 | 40 |
. . org/eclipse/cdt/internal/core/parser/scanner/LocationMap.java | 0 | 36 |
. . t/internal/autotools/ui/editors/automake/GNUMakefileUtil.java | 0 | 31 |
. . ipse/cdt/make/internal/core/makefile/gnu/GNUMakefileUtil.java | 0 | 30 |
. . rser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java | 1 | 23 |
. . /cdt/internal/core/dom/parser/cpp/semantics/CPPTemplates.java | 0 | 19 |
. . ipse/cdt/internal/core/dom/parser/cpp/GNUCPPSourceParser.java | 0 | 19 |
. . se/cdt/internal/autotools/core/AutotoolsNewMakeGenerator.java | 0 | 19 |
Another approache is to work on practices acquisition: select a few rules that you consider as really important, with an actionable number of violations, and fix them wherever they appear. The current analysis has 4
rules with priority 1 or 2 that show less than 50 violations. The graphic below on the left shows the 5 top rules with a low number of violations and a priority with either 1 or 2. The table on the rigth provides the corresponding data.
Mnemo | priority | vol |
---|---|---|
DoubleCheckedLocking | 1 | 2 |
BooleanInstantiation | 2 | 5 |
AbstractClassWithoutAnyMethod | 1 | 5 |
BrokenNullCheck | 2 | 11 |
AvoidBranchingStatementAsLastInLoop | 2 | 50 |
The visualisations on this page can be exported and easily reused on an external web site. You can find more information on iframes and pictures reuse in the project's web site. Remember to change the server name in the code samples provided.
Pie chart of checked and broken rules
<iframe src="http://server/projects/tools.cdt/PmdAnalysis/pmd_analysis_pie.html" frameborder="0" style="width: 100%; height: 320px"></iframe>
Files with high priority violations
<img src="http://server/projects/tools.cdt/PmdAnalysis/pmd_analysis_files_ncc1.svg" frameborder="0" style="width: 100%; height: 600px" />
Top 5 high-priority rules
<img src="http://server/projects/tools.cdt/PmdAnalysis/pmd_analysis_top_5_rules.svg" frameborder="0" style="width: 100%; height: 370px" />
The visualisations used in this document rely on a number of flat CSV and JSON data files, that were extracted from the PMD XML results file. You can download and play with them if you want to thereafter:
Page generated by Alambic 3.3.3-dev on Wed Jan 27 07:56:54 2021.