Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 21 Next »

Table of Contents


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 duplications 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 Duplications tab in the resource 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.

To display the source code, click on the file name.

To visualize the entire block of code, click on the Expand link.

Activating Cross Project Duplication Detection

Log in as an administrator, go to Settings > Configuration > General Settings > Duplications and set the Cross project duplication detection property to true.

Run a new analysis on your projects to detect duplications with other projects.



Note that cross project duplication detection is automatically disabled when analyzing a project with the 'sonar.branch' property set.

Related Topics

See the following blog post: Manage Duplicated Code with Sonar.

  • No labels