Clarifying the manual count of Cyclomatic Complexity

淺唱寂寞╮ 提交于 2019-12-20 03:05:23

问题


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



来源:https://stackoverflow.com/questions/39055516/clarifying-the-manual-count-of-cyclomatic-complexity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!