4

I have done so much analysis on how sonar cpd detects duplicate blocks.But I am not able to trigger out exactly what the process it takes to detect blocks or lines of code.Do that have any minimum number of lines.

For example if I am writing as below it is not detecting any code duplications even I repeat more that 20 times.

        System.out.println("this is good");
        System.out.println("this is good");

        System.out.println("this is good");
        System.out.println("this is good");

        System.out.println("this is good");

        System.out.println("this is good");

        System.out.println("this is good");

        System.out.println("this is good");

        System.out.println("this is good");

        System.out.println("this is good");

Later on I tried giving blocks duplications

     try
    {
    connection = null;
    }
    catch(Exception e){
        e.printStackTrace();
    }
    try
    {
    connection = null;
    }
    catch(Exception e){
        e.printStackTrace();
    }
    try{
    connection = null;
    }
    catch(Exception e){
        e.printStackTrace();
    }
    try{
    connection = null;
    }
    catch(Exception e){
        e.printStackTrace();
    }

Here it is considering as two block even though it has many blocks.

Please let me know the exact process followed in this duplication detection by sonar 3.4.1

In this http://docs.sonarsource.org/3.1/apidocs/src-html/org/sonar/plugins/cpd/SonarEngine.html

I found a constant block size as 10. But I am able to relate this in my observation.

satheesh
  • 1,443
  • 7
  • 28
  • 41
  • 1
    This is a sign of a so-called "token based" clone detector, which match sequences of tokens. These tend to have high false positive detection rates; **} catch ( Exception** in your example is considered to be a small clone by such detector. To counter that these detectors tend to be configured to only accept pretty big clones with some possible additional restrictions, see Andras' answer. AST based-clone detectors only match on language structures; they would not consider **} catch ( Exception** to be a clone, ever. Consequently, they can find clones with considerably smaller granularity. – Ira Baxter Feb 13 '13 at 09:20

1 Answers1

0

A block is the lines of code between balanced braces. The constant block size means that in the block there must be 10 Lines of code to match. So to have a duplication try this.

public void foo(){
//... 10 lines of code
}

private void bar(){
//.... the same 10 lines of code
}
András Tóth
  • 605
  • 4
  • 12
  • i gave two functions and inside i gave 15 print statements duplicated in both functions.But not observed any duplications in sonar analysis – satheesh Feb 13 '13 at 07:09
  • was the two function body was the same? – András Tóth Feb 13 '13 at 07:14
  • Yes both functions are with same body – satheesh Feb 13 '13 at 07:16
  • Sorry, in that case I have no idea not counting the obvious bugs (e.g your code is not analyzed). – András Tóth Feb 13 '13 at 07:24
  • I am getting only in some cases duplications but not with this case.And my code is analyzed successfully – satheesh Feb 13 '13 at 08:34
  • ... If you want to see this work, feed it 10,000 copies of the same line and see what it does. Once that works, cut down the size to see where it stops detecting duplicates. Better, give it something real of scale to gnaw on; this almost always produces interesting clones. (Read the clone detector tool docs to understand the parameters of detection, rather than trying to guess by making examples; I suspect "10" is just such a parameter and that's the wrong value). – Ira Baxter Aug 01 '14 at 17:03