Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cyclomatic Complexity of switch case statement

I'm confusing about the CC of switch statement

If I have following code:

if (n >= 0) {
    switch(n) {
        case 0:
        case 1: 
            printf("zero or one\n");
            break;
        case 2: 
            printf("two\n");
            break;
        case 3:
        case 4: 
            printf("three or four\n");
            break;
        }
    }
else {
    printf ("negative\n");
}

what is the CC?

I found a post said that it's 5, with this diagram CC diagram

(the edges are 17, not 16, I think it's a typo)

It says that we only need to count case 0 and case 1 as one

But I think the diagram should be: CC diagram

Edges: 17,
Nodes: 13,
17 - 13 + 2P = 6

I count every cases as 1

My OOSE professor said it's 6, but in different way

He said:

init     => 1  
if       => 1  
switch   => 1  
case 0 1 => 1  
case 2   => 1  
case 3 4 => 1

so it should be 6

What's the correct answer?
I'm really confused, thanks.


edited:
Now I think it's 7. yes, 7
Because if n is more than 5, will just do nothing and exit the switch statement.

then we get this diagram:
enter image description here

now E = 18
18 - 13 + 2 = 7

am I correct..?
really, really, really confused...

like image 706
CodinCat Avatar asked May 14 '15 14:05

CodinCat


People also ask

What is the complexity of a switch statement?

It is at worst O(n). Sometimes (and this is language and compiler dependent), it translates to a jump table lookup (for "nice" switches that don't have too large a case range). Then that is O(1).

How can cyclomatic complexity be reduced in a switch case?

Avoid use of switch/case statements in your code. Use Factory or Strategy design patterns instead. Complexity of 8 (1 for each CASE and 1 for method itself). Refactoring using Factory design pattern and complexity changed to 1.

What is cyclomatic complexity formula?

Method 1: Total number of regions in the flow graph is a Cyclomatic complexity. Method 2: The Cyclomatic complexity, V (G) for a flow graph G can be defined as. V (G) = E - N + 2. Where: E is total number of edges in the flow graph. N is the total number of nodes in the flow graph.

What is cyclomatic complexity explain with example?

Cyclomatic complexity is a source code complexity measurement that is being correlated to a number of coding errors. It is calculated by developing a Control Flow Graph of the code that measures the number of linearly-independent paths through a program module.


2 Answers

Code metric tools I've worked with count each case as a separate branch, even if it's a fall through case.

But this is an arbitrary choice. Code metric tools tend to err on the side of caution by default. The way the switch statement will ultimately be evaluated is an internal implementation detail that will vary based on the type of input and number of cases (at least in C#).

The go-to answer for reducing cyclomatic complexity caused by switch statements is to convert the cases/outputs into a dictionary. In your example, it would be something like the code sample below. Be aware that this is only for readability/maintainability. If your switch statement is long enough the .Net compiler will automatically convert it to a dictionary for you, so there is no performance gain.

var outputs = new Dictionary<int, string>()
            {
                { 0, "zero or one\n" },
                { 1, "zero or one\n" },
                { 2, "two\n" },
                { 3, "three or four\n" },
                { 4, "three or four\n" }
            };

if (n >= 0)
{
    printf(outputs[n]);
}
like image 93
Seth Avatar answered Oct 24 '22 10:10

Seth


Ok, I have found the answer.

from McCabe.com, page 26 and 27

The answer is 5, because the original version of CC by McCabe counts a fall-through case as 1.

like image 41
CodinCat Avatar answered Oct 24 '22 10:10

CodinCat