Duplication is the worst quality flow as it includes all the others. Indeed copy/pasting a block of code duplicates non respects of coding standards, potential bugs, etc.
To help you fight against duplication, Sonar embeds its own Copy Paste Detection engine (referred as Sonar CPD). It can detect duplication between blocks:
- within a source file
- within a project
- scattered in different projects
When a change is needed on a piece of code, check for duplication with Sonar. If there are duplications:
- Analyze those duplications
- Try to remove this duplication 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
Looking For Duplication
Add the Comments & Duplications widget to your dashboard:
From there, you can drill down to the resource viewer on the Duplication tab:
See the Duplications Tab section for information on how to read the results.
Activating Cross Project Duplication Detection
Log in as an admin, go to Configuration > Duplications and set the Cross project duplication detection property to true.
Run a new analysis on your project to detect duplications with other projects.
See the following blog post: Manage Duplicated Code with Sonar.