Hello,
We had some discussions in the team this week and the main reason for it are the too distracting (default) rules.
I have always seen sonar as a great tool for reporting smells in my code. But the (default) ruleset of sonar has moved away from this target and is annoying the developer with suspicious “errors”.
For example the rule squid:S1155:
if (params.size() == 0) {
The rule suggest to replace .size() == 0 with .isEmpty(). Ok, this is an other possibility to check it, but that doesn’t mean, that one of the possibilities is wrong or a “smell”, it is just an other style. And if in the next line .get(0) is called then even .size() >= 1 seams more logical to me than .isEmpty().
Other examples are the (new) rules squid:S4529, squid:S4834, squid:S4784, squid:S4823, …
These rules report an error because the developer is using HTTP endpoints, regular expressions, permission checks, command line arguments and so on. WTF? Of course these are security-sensitive and the lines should be implemented correctly but they are not an error or smell. In every single line of code there could be perhaps an error, so sonar should perhaps report every line of code as critical.
The last example (the one the main part of the discussion was about) is the rule squid:S3776 about cognitive complexity of code in one method. The rule says by default: If the method has a cognitive complexity of above 15, then is smells. I have to strictly deny this.
Of course it is no good idea to write to complex methods and most of the methods in a project will below 10. But that doesn’t mean, that a value higher than 15 makes the code badly readable and badly maintainable and makes the code smelly or an “error”. I think a usual programmer is easily able to understand (otherwise good written) methods with a cognitive complexity of up to 50. If you take a look at important open source frameworks you will find lots of methods with this complexity. Only “programming monkeys” have perhaps problems with that. Of course it is a good idea to split complex methods into smaller ones if it is possible and makes the code cleaner, but this is true for methods with a complexity of 5 and also for methods with a complexity of 35.
And additionally it would be a good idea to have a possibility to find methods with a high complexity with sonar, because there will be many cases to refactor the code into smaller parts. But it is NOT a smell or dept just because of the complexity (yes, I know there are possibilities to turn the rule of at some line of code or some profile).
So my suggestion for sonar to handle these cases would be to implement a possibility to search for code associated to the mentioned rules, but not treat them as smells or errors.