SonarQube reporting duplicate code for database entities

Make sure to read this post before raising a thread here:

Then tell us:

  • What language is this for?
    • Java
  • Which rule?
    • Duplicate code
  • Why do you believe it’s a false-positive/false-negative?
    • My problem was a false positive, but the solution I found might be a false negative in legitimate duplications
    • Why it was a false positive:
      • I have two database entity classes (javax.persistence)
      • I use Lombok for creating getters and setters and there are no other methods inside. Only the column mappings.
      • One of these entities represents a table and the other one represents a view.
      • The view has a set of 6 columns with same names as the table
      • This isn’t a duplication of code. They serve different purposes and this sort of block can’t be reused easily like that. It would even become confusing code if trying to do so
    • Why I think the solution might cause a false negative:
      • What I did to fix the issue was to scramble the order of these fields in the entity and it worked. The code is still “technically duplicated”, just in a different order, but SonarQube doesn’t complain about it anymore.
  • Are you using
    • SonarCloud?
      • No
    • SonarQube - which version?
      • Yes, 9.8
    • SonarLint - which IDE/version?
      • Yes v7.3.0.59206, on IntelliJ Ultimate v2022.3.1
      • in connected mode with SonarQube or SonarCloud?
        • Yes, connected to the project through SonarQube, but SonarLint didn’t complain about the duplication. Only SonarQube.
  • How can we reproduce the problem? Give us a self-contained snippet (best) or screenshot (good)
    • I guess, just have two pojo classes with a bunch of duplicate fields in the same order. This should be a true positive. But if you add the @Entity and @Table there and @Table in both have different parameters, then it would be a false positive. Scramble the fields in the pojo without the annotations and it should still call the duplication, though in my point of view. but it seems it doesn’t.

Hi,

This is a tough one. Your solution works, but ideally you shouldn’t have to use it. The problem here is in how duplications are detected for Java. From the docs

Differences in indentation and in string literals are ignored while detecting duplications.

And unfortunately, since individual duplications aren’t raised as issues, you can’t mark them False Positive. As I said, what you’ve done works but isn’t ideal since the table and view are supposed to parallel each other (if I understand correctly) and your change makes that harder to see in the code. Your other option would be to exclude the two files in question from duplication detection.

The overall topic of problematic duplication detection is one that comes up like cicadas: infrequently but reliably. I’ll make sure the Product Managers are aware of this. (But don’t hold your breath for immediate change.)

 
Ann

1 Like

Yes, exactly… Some of the fields in the view mirror some of the ones in the table and that’s getting caught as duplication.

Thanks for your explanation. That’s understandable. Since reordering the attributes fixed it for us, we’ll continue with this strategy whenever it happens again (if at all) for now

Have a nice day!

1 Like