Reputation: 1442
Is there a way to set complexity limits for sonar?
I prefer very simple codes (at most one if or loop), but I cannot find a setting like sonar.cpd.java.minimumtokens
or sonar.cpd.java.minimumLines
exist for code duplications (and does not seem to work, but that will be another question).
I would like to reduce the cyclomatic complexity limit for methods, and also the maximum number of methods in one class. For the latter, a line limit would be okay. How can I set those complexity limits?
Upvotes: 0
Views: 5377
Reputation: 19664
If you're using VS Code, you can enable the SonarQube tab in the Activity Bar context menu. Then you can search via its Rules tab's more ("...") menu for java:S3776
(code complexity). Toggling that rule will add it to your settings.json, which you can then modify with the documented threshold. The default is 15 but many flat ifs don't bother me as much as searching through 5 layers of lasagna code:
"sonarlint.rules": {
"java:S3776": {
"level": "on",
"parameters": {
"threshold": 18
}
},
},
Upvotes: 0
Reputation: 3121
There is a few Java rules targeting cyclomatic complexity provided with SonarQube. The only way to configure them is to use a dedicated Quality Profile. By default, the SonarWay quality profile uses fixed thresholds. However, these rules are parameterizable, and you can configure them as soon as you create a quality profile other than the default SonarQube's SonarWay profile. You will then have to configure your project to rely on this new Quality Profile, rather than SonarWay.
For reference, have a look at the following rules:
I would also recommend to have a look at another complexity-related rule:
This rule is relying on the concept of Cognitive Complexity, designed in order to remedy Cyclomatic Complexity’s shortcomings and produce a measurement that more accurately reflects the relative difficulty of understanding, and therefore of maintaining methods, classes, and applications.
Upvotes: 1