0

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?

Vin
  • 515
  • 3
  • 17
Árpád Magosányi
  • 1,394
  • 2
  • 19
  • 35

1 Answers1

1

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:

  • squid:S3776: Cognitive Complexity of methods should not be too high

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.

Wohops
  • 3,071
  • 18
  • 29