Duplicate lines in C# code

We are using 8.4.0.35506 of SonarQube developer edition.
After running SQ analysis for the new feature, SQ started to show a lot of duplicate lines in C# code. Is this expected or a bug? For me, it looks like the lines are not the same.

Hi @Viktors_Telle,
There are no duplicates in the code of your screenshot, but in another class (or maybe further down in this class).
When you click on the thin, grey, vertical bar on the very left, SonarQube will show the file, that contains the same code.

Hi @bugbouncer,

Thanks for your suggestion! I clicked on the grey line, and SQ shows that duplicates are in the same file.
Then I clicked on line 10:

And scrolled down to the suggested duplicate lines (824 - 869):

I can’t understand why these are duplicate lines. I also checked few other places, and could not detect duplicate lines there as well.
This massive class contains the list of all countries with appropriate translations.

I have attached the full file.
CountryList.txt (79.0 KB)

Hi @Viktors_Telle,
I have checked the SonarQube documentation regarding the duplication metric:

For Java it says:

There should be at least 10 successive and duplicated statements whatever the number of tokens and lines. Differences in indentation and in string literals are ignored while detecting duplications.

The difference in you lines of code are “just” the string literals - the actual code is repeated over and over.
That should be the explanation.

1 Like

Hi,

Thanks for finding the explanation in the documentation.
Currently, it seems that there is no way to fix this particular duplicate code so that SonarQube is happy.
Probably, I will add this file to sonar.cpd.exclusions parameter to ignore the duplicate checking during analysis.

Why not move these data into a database table or a classic i18n-resource-bundle?

1 Like

Thanks for your suggestions! The database in this case would be overkill since we don’t use it in this application. We can consider storing this data in .json file, reading it on application startup, and “caching” data in static property of the static class.
Thanks again for your help! :slight_smile:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.