澄清人工计算的环复杂性 [英] Clarifying the manual count of Cyclomatic Complexity
问题描述
让我们假设我们有这样的代码:
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;
}
我们可以在这里将CC值设为6 + 1吗?为什么要添加值1?如果将CC值视为7,那么这是独立路径的数量吗?
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?
如果上面考虑了失败的情况怎么办?可能只有两条唯一的路径,2 +1 = 3
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?
推荐答案
我们知道,CC = P+1.
在这里,P =谓词节点(条件)的数量= 2
Here, P = number of predicate nodes (conditions) = 2
条件数将为2,因为:
Case分支可以覆盖多个替代值或范围,例如 案例1、2、5至10.由于他们没有引入其他分支来决定 ,它们也不会增加循环复杂度.
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.
来源:此处
所以,CC = 2 + 1 = 3
So, CC = 2+1 = 3
这篇关于澄清人工计算的环复杂性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!