Settings for code duplications and how does code duplication work?

When having the below 2 classes, Teamscale tells me at least the code fragment ranging from private final int a; until the last } in the second if statement of the equals method are definetly duplicates. In SonarQube it tells me there is no duplication at all. Why is it like that?

I read up on the topic but could not find that much detailed docs, it states a fragment has to have at least 10 duplicated statements to be recognized as duplicate. What does count as a statement? When I count in the Methodheads it is more than 10 otherwise less than 10. Can I adjust this value? Also can I adjust this value for a specific package without changing it for others?

public class A
{
    private final int a;

    public A(final int a)
    {
        this.a = a;
    }


    public int getA()
    {
        return a;
    }

    @Override
    public boolean equals(Object other)
    {
        if (other == null)
        {
            return false;
        }

        if (this.getClass() != other.getClass())
        {
            return false;
        }

        A otherA = (A) other;

        return a == otherA.a;
    }
}

public class B{

    private final int b;

    public B(final int b)
    {
        this.b = b;
    }


    public int getB()
    {
        return b;
    }

    @Override
    public boolean equals(Object other)
    {
        if (other == null)
        {
            return false;
        }

        if (this.getClass() != other.getClass())
        {
            return false;
        }

        B otherB = (B) other;

        return b == otherB.b;
    }
}

Hi,

We crafted our own duplication detection engine, so it’s not surprising that you get a different result in other tools. It may help to take a look at the language-specific details for the Metric definition for duplications.

 
Ann

Hi,
I’m not really sure where I can change those values. Also would it be possible to adjust those values based on in which folder those files are? F.e. Files in com.example.my-classes should have a threshold of 15 statements where com.example.my-abstract-classes should have a threshold of 10.

Hi,

For tuning the values, take a look at the sonar.cpd.* analysis parameters. You can do this on an analysis-wide basis but not at a directory level.

 
HTH,
Ann

Please note that the sonar.cpd.* parameters are not used by the Java plugin.

1 Like