Code duplication is shown 0.0% when different function names are used with the same codebase

Hi,

We came across an issue when duplicated code has not been detected by SonarCloud as we would initially expect. For example, we have two files to this email that have most of the code the same with the difference in function names only:

File 1 - contents1.ts:
export const PiNearbyTitleContainer = styled.View display: flex; flex-direction: row; align-items: center; justify-content: space-between;;

export const PiNearbySortOptionContainer2 = styled.View background-color: ${colors.WHITE}; padding: 16px;;

File 2 - contents2.ts:
export const PiNearbyTitleContainer = styled.View display: flex; flex-direction: row; align-items: center; justify-content: space-between;;

export const PiNearbySortOptionContainer = styled.View background-color: ${colors.WHITE}; padding: 16px;;

The SonarCloud report shows 0.0% of duplicated code in this case, although the files contain several identical functions. As soon as I make the function names identical, SonarCloud detects duplicated code.

Currently we don’t really have any specific configuration for code duplication in SonarCloud. Is there a specific way to configure SonarCloud for a project to detect duplicated code more precisely and avoid cases when a method name change in a sourcecode file significantly impacts the discovered duplicated code percentage?

Hi Vlad,

You can learn about how to configure duplication detection in the documentation.

Check the following section and search for “Duplications”.

Best.

Hi Olivier,

Thank you for your reply.
I have reviewed the ‘Duplications’ at the provided section and played around with the settings specified there.
If I modify the following keys in our ‘sonar-project.properties’ file:

sonar.cpd.javascript.minimumtokens
sonar.cpd.javascript.minimumLines

it seems it has no effect on the resulting percentage.
I modified the sourcecode files to have 30 lines with the duplicated code and it gave me 40% of detected duplicated code. I also checked with about 9 duplicated lines and SonarCloud did not detect duplicated code.
Each time I changed the number of duplicated lines in the sourcecode, I would then modify ‘sonar.cpd.javascript.minimumtokens’ to values in range from 10 to 10000, as well as ‘sonar.cpd.javascript.minimumLines’ to values in range from 2 to 100 and SonarCloud would always return the same percentage of duplicated code.

Is there anything that I’m missing in the configuration?

Thanks,
Vlad