Clarifying the manual count of Cyclomatic Complexi

2020-04-16 17:39发布

问题:

Let's assume that we have a code like this:

switch(y)
{
case 1: case 2: case 3:
    function();
    break;
case 4: case 5: case 6:
    function_2();
    break;
}

Can we get the CC value as 6+1 here? Why a value of 1 is added? If the CC value is considered as 7, is that the number of independent paths?

What if a fall through scenario is considered above? As only possible two unique paths are there, 2 +1 =3

Which of the above are correct or are the both of them correct?

回答1:

As we know, CC = P+1.

Here, P = number of predicate nodes (conditions) = 2

Number of conditions will be 2 because:

Case branch can cover several alternative values or ranges, such as Case 1, 2, 5 To 10. As they introduce no additional branches to decide on, they do not increase cyclomatic complexity either.

source: here

So, CC = 2+1 = 3