Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sonar cyclomatic complexity rule issue - discourages multiple return statements

For the following piece of code, sonarqube computes the method cyclomatic complexity as 9

String foo() {
    if (cond1) return a;
    if (cond2) return b;
    if (cond3) return c;
    if (cond4) return d;
    return e;
}

I understand as per the rules for computation http://docs.sonarqube.org/display/SONAR/Metrics+-+Complexity the complexity of 9 is correct. So complexity of the method is = 4 (if) + 4 (return) + 1 (method) = 9

This complexity can be reduced, if I have a single exit point.

String foo() {
    String temp;
    if (cond1) { 
        temp = a;
    } else if (cond2) {
        temp = b;
    } else if (cond3) { 
        temp = c;
    } else if (cond4) {
        temp = d;
    } else {
        temp = e;
    }
    return temp;
}

I believe this code is more cluttered and unreadable than the previous version and I feel having methods with return on guard conditions is a better programming practice. So is there a good reason why return statement is considered for computation of cyclomatic complexity? Can the logic for computation be changed so that it doesn't promote single exit point.

like image 495
gopalsaob Avatar asked May 22 '14 09:05

gopalsaob


People also ask

Can SonarQube measure Cyclomatic Complexity?

Yes, Cyclomatic Complexity is a standard metric in SonarQube.

How is Cyclomatic Complexity calculated in SonarQube?

It is the Cyclomatic Complexity calculated based on the number of paths through the code. Whenever the control flow of a function splits, the complexity counter gets incremented by one. Each function has a minimum complexity of 1. This calculation varies slightly by language because keywords and functionalities do.

What is cognitive complexity SonarQube?

Cognitive Complexity Rule. So we have the response from SonarQube: “Refactor this function to reduce its Cognitive Complexity from 26 to the 15 allowed.” Cognitive Complexity is a measure of how hard the control flow of a function is to understand. Functions with high Cognitive Complexity will be difficult to maintain.


2 Answers

I agree you should use some common sense and go with the code which you believe is simplest.

BTW You can simplify you code and have just one return if you use ? :

String foo() {
    return cond1 ? a :
           cond2 ? b :
           cond3 ? c :
           cond4 ? d : e;
}
like image 91
Peter Lawrey Avatar answered Sep 23 '22 02:09

Peter Lawrey


"So is there a good reason why return statement is considered for computation of cyclomatic complexity? Can the logic for computation be changed so that it doesn't promote single exit point."

In your example having multiple returns doesn't add to the complexity and as @Peter Lawrey says you should employ common sense.

Does this mean that all examples of multiple return statements do not to complexity and it should be removed? I don't think so. If would be very easy to come up with an example of a method which is hard-to-read because of multiple return statements. Just imagine a 100 line method with 4 different return statement sprinkled throughout. That is the kind of issue this rules tries to catch.

like image 35
matt freake Avatar answered Sep 24 '22 02:09

matt freake