Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.
titleTable of Contents

Table of Contents

Some will argue that Duplication is the worst quality flaw as it includes coding sin because it perpetuates all the others. Indeed copy/For instance, copying and pasting a block of code duplicates : the disregard of coding standards, any potential bugsits Bugs and Potential Bugs, its Coding Standards Breaches, and so on. It's a meta-sin, if you will.

To help you fight against detect and eliminate duplication, SonarQube embeds uses its own Copy Paste Detection engine. It can detect duplication between blockscopy/paste detection engine, which can detect duplications:


  • Within a source fileWithin
  • Across multiple files in a project
  • Scattered in different projects

When a change is needed on a piece of code, first check for duplications through the web interface. If there are any:


  • Across modules of a project
  • Across multiple projects

SonarQube allows to detect "Type 2" duplications which means :  structurally/syntactically identical fragments except for variations in literals and comments. A piece of code is considered as duplicated as soon as there are at least 100 successive and duplicated tokens (can be overridden with property sonar.cpd.${language}.minimumTokens) spread on at least 10 lines of code (can be overridden with property sonar.cpd.${language}.minimumLines). For Java projects, the duplication detection mechanism behaves slightly differently as a piece of code is considered as duplicated as soon as there is the same sequence of 10 successive statements whatever is the number of tokens and lines.

Looking For Duplications

Add the Duplications widget (was Comments & Duplications prior to version 3.7) to widget to your dashboard:

From there, you can drill down to the Duplications tab in end up in the component viewer:

Image Removed


Image Removed

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.

Image Added

To display the source codeof a particular instance of a block, click on the file name.

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

Activating Cross Module and Cross Project Duplication Detection

Log By default, the scope of duplication detection is the project for standard projects and the module for multi-module projects. To detect duplications between the modules of your project and between the different projects analyzed on your SonarQube platform, log in as a a System administrator, go to Settings > General Settings > General > Duplications and set the Cross project duplication detection property to true.Run a new  Then run another analysis on your projects to detect duplications with other projects.




. You also have the option of setting this at the project level: Configuration > General > Duplications.


  • For technical reasons, cross module duplications will be be detected only after the second analysis of your project.
  • Cross project duplication detection is automatically disabled when analyzing a project with the sonar.branch property set.

Best Practices

  • Always check for duplications in a file before modifying it. If there are any, either:
    • Analyze those duplications and eliminate them by using inheritance or other suitable patterns (do this only if you have unit tests on the blocks to change)
    • Do not forget to replicate your changes wherever it is necessary on the duplicated blocks
  • Use the Issues and Technical Debt mechanisms to help you monitor the cost and and track the remediation of this sin by editing your Quality Profiles to include the Duplicated Blocks rule from the Common Sonar repository.

Related Topics

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