Duplication detection in c# recommendations

Wonder if you have any hints on how to handle duplicate detection so it is useful but limits false positives. Some examples we stumbled upon:

  • Command and DTO would have similar/same properties or sections of those, and that would be detected as duplicate
  • unit tests are quite repetitive in their structure as they are DAMP more than DRY

So far we just keep bumping our quality settings but even though we are “just” at 2, we would rather not allow duplication if this was about the actual section of code instead.

For tests we could ignore the whole folder, but I suppose this would disable all other checks not only duplicated.

Any suggestions?

Marcin

Hello @merdacz,

Using the Codacy configuration file you can disable checking for duplication only on your tests directory.

You may also want to have a look at this pull request, where we’re documenting the possibility of tweaking the minimum token length for reporting duplicated code blocks - we’re still reviewing this information, though:

1 Like

Hey,
this is useful. We do not use configuration files yet but will check this out. This should be exactly what we need for the test project case. Just to confirm - would this somehow affect our current setup (via UI) if I throw in this YAML file with ignore paths just for duplication or does this work on top of what is already configured via UI/your defaults?

As of the linked PR I am not sure if I follow fully but it’s not a matter of how long the duplicated block is but that it is about properties (getters/setters). We could potentially use the ignore sub-folder way as for tests as we tend to have dedicated folders for Commands & Dtos, but ideally there would be a way to provide a ignore pattern (which I believe PMD doesn’t support for C#?).

Hi @merdacz

The Codacy Configuration file replaces any configuration set on it, so if you just have an entry with the exclude paths for Duplication, those will be used while the rest of your configurations will remain the same.

This being said, there are some details that will change when you set up the file. For example, you will not be able to ignore files on the UI, so if you are already ignoring any file or wish to ignore files in the future you will have to do it on the Configuration File.

1 Like

Hi,

Just to clarify that in the pull request, we’re documenting a few options that add a bit more flexibility over what PMD CPD considers to be a duplicate block. So I was thinking that, for example, setting the flag --ignore-identifiers to false could be enough for PMD CPD to no longer consider the getter and setter methods clones of each other because of the different attribute names mentioned in those lines of code. This would affect how duplicates are detected throughout the entire repository, though.

Edit: The live documentation already includes these new instructions.

Hello @merdacz,

This is to let you know that we found out that Codacy isn’t reading and applying the Codacy configuration file options that control the PMD CPD duplication detection. Please see my reply on this other thread for more information:

I’m sorry this isn’t a viable solution at the moment, after all.

Hey Paulo
thanks for the heads up on this. Any chance this got fixed meantime? Can you notify me here or can I subscribe elsewhere to get pinged once this happens?
Best regards,
Marcin

Hi @merdacz,

We haven’t made progress on this issue yet since the team is focusing on an upcoming product release at the moment. But I’ll notify you here on this thread when there is news about this topic.

Thank you!

1 Like