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.

