Duplication is the worst quality flow flaw as it includes all the others. Indeed copy/pasting a block of code duplicates non respects : the disregard of coding standards, any potential bugs, etcand so on.
To help you fight against duplication, Sonar SonarQube embeds its own Copy Paste Detection engine (referred as Sonar CPD)engine. It can detect duplication between blocks:
When a change is needed on a piece of code, first check for duplications with Sonarthrough the web interface. If there are duplicationsany:
- Analyze those duplications Try to remove this duplication and eliminate them by using inheritance or other suitable patterns (perform such action only if you have unit tests on the blocks to change)
- Or do not forget to replicate your changes wherever it is necessary on the duplicated blocks
Add the Duplications widget (was Comments & Duplications widget to prior to version 3.7) to your dashboard:
Image RemovedImage Added
From there, you can drill down to the Duplications tab in perspective in the resource component viewer:
How to read the example above: There is duplication between three blocks of 12 lines. The first block in FindbugsVersion starts at line 43, the second block in CheckstyleVersion starts at line 40 and the third block in PmdVersion starts at line 40.
Activating Cross Project Duplication Detection
Log in as an a a System administrator, go to Configuration > to Settings > General Settings > General > Duplications and set the Cross project duplication detection property to true.
Run a new analysis on your project projects to detect duplications with other projects.
Note that cross project duplication detection is automatically disabled when analyzing a project with the '
See the following blog post: Manage Duplicated Code with Sonar.