I’m trying to understand how cyclomatic complexity is computed at the file level. I have a couple files in a Java project where issues are raised on methods because of high complexity, but the complexity of the entire class is less than the complexity of the method. So just out of curiosity, how is the complexity computed at the class level? I’ve read the Cyclomatic Complexity Guide but didn’t find an explanation for this specific point.
Gilles
SonarQube Server 2025.2 (Developer Edition)
Hello Gilles,
As far as I know, SonarQube only looks at the complexity of methods, and I don’t know of a rule that would raise an issue for the complexity of the entire class.
The complexity rules I’m aware of are: S3776 which looks at cognitive complexity, and S1541 for cyclomatic complexity. This last one is not activated in the default quality profile, as cognitive complexity is the preferred metric.
If you encountered a different rule, I’m happy to look into it.
By setting the threshold of S3776 rule to 0, I’ve been able to collect the complexity of each method. And it appears that the complexity at the class level is just the sum of the complexity of all methods. My initial attempt last week gave me inconsistent results, probably because I read the results before everything was written to disk.
I guess I have my answer now, as everything seems to be consistent.