Should SonarQube Allow Accepting Duplicate Code?

Hi there!

One of the most common questions or requests I receive is about duplications in Sonar. In most cases, Sonar does not analyze the contents of literals (whether they appear in methods, annotations, or variables) to determine what is inside them and whether the duplication is actually meaningful.

This often leads to situations in common programming patterns where repeating the structure while changing the literals is intentional and useful. A typical example would be React components.

For instance, in two different questions I received today from two separate clients, we had the following cases: a TypeScript annotation and a JavaScript method.

@ApiResponse({ status: HttpStatus.NO_CONTENT, description: ‘Mapa guardado correctamente.’ })

[…]

@ApiResponse({ status: HttpStatus.NO_CONTENT, description: ‘Funciones ejecutadas correctamente.’ })

Or, in the other case:

We could debate whether these are valid cases or not. Personally, I think Sonar is doing its job correctly, since the code could technically be refactored. However, I also believe that refactoring certain patterns can introduce a significant maintenance overhead, sometimes making the code harder to understand or maintain.

In the same way that we have different states for issues, Sonar’s duplication detections may sometimes not fully align with a team’s needs. And occasionally, they may simply be incorrect.

It could be useful to allow users to select a duplicated block and assign it a status such as Accepted. In other words, we acknowledge this technical debt because the cost of refactoring is higher than the cost of keeping the duplication. Alternatively, we could mark it as a false positive if we believe the engine has made a mistake.

The inability to do this often leads to a situation where teams cannot pass their Quality Gates, and the only workaround is to exclude the entire class from duplication analysis just to keep moving forward. However, excluding a class from the duplication engine may also cause legitimate duplication issues to be ignored.

So I’d like to open this discussion:

What should we do in these cases?

Should we implement a mechanism to accept a duplication, similar to how we handle regular issues?

Regards.

9 Likes

Another case reported by a customer:

Are there any significant progress in this issue?

Hi,

This is flagged for the PMs. Organizationally, we understand there’s a need for this, but implementing it is likely to be a big lift and there have been (lots and lots and lots of) other priorities. (Really, lots. This isn’t even low-man on the totem pole. It’s dangling off the end of the totem pole by its fingernails.) But every user ‘vote’ pushes this fractionally higher on the list, so… :crossed_fingers:

 
Ann

Please add my vote for this.
I’ve been adding exclusion rules for what we consider to be legitimate duplications, but it’s sometimes tricky to write an exclusion that doesn’t also ignore too much.

2 Likes

Add my vote for this, please. Thanks.

1 Like

+1 vote! This matter for me is essential.

1 Like

So important for me! Voted

1 Like

You’ve got my vote, no doubt about it!

1 Like