圈复杂度计算——具体例子
Cyclomatic complexity calculation - concrete example
假设我们有以下代码:
public void testIt(boolean a, boolean b){
if (a){
...
}
if (b){
....
}
}
据我所知,有3种计算方法。我将使用其中两个:区域公式和经验法则。
使用区域公式我们有两个区域 if(a){this is the first one}
和 if (b) {this is the second one}
。所以 CC = 两个区域 +1 = 3.
经验法则来自 Testing for ISTQB Advanced Level Technical Test Analyst vol 3
书:
Now, this is all simple enough for a small, modest method like this.
For larger functions, drawing the graph and doing the calculation from
it can be really painful. So, a simple rule of thumb is this: Count
the branching and looping constructs and add 1. The if statements,
for, while, and do/while constructs, each count as one. For the
switch/case constructs, each case block counts as one. In if and
ladder if constructs, the final else does not count. For switch/case
constructs, the default block does not count. This is a rule of thumb,
but it usually seems to work.
所以根据这个规则CC=两个if条件+1=3.
然而,在Whosebug上与一位用户聊天时,有人说这段代码的CC = 4(他使用了第三种方法),并提供了该用户提供的以下图片:
那么这段代码的圈复杂度是多少? 3 或 4 或?
,此代码的圈复杂度为 3
M = E − N + 2P,
where
E = the number of edges of the graph.
N = the number of nodes of the graph.
P = the number of connected components.
M = 7 - 6 + 1*2 = 3
关于软件测试,它意味着:
M is an upper bound for the number of test cases that are necessary to achieve a complete branch coverage.
M is a lower bound for the number of paths through the control flow graph (CFG).
All three of the above numbers may be equal: branch coverage <= cyclomatic complexity <= number of paths.
因此,您需要 2 个测试来提供分支覆盖。但不代表会覆盖所有路径。
要涵盖此处的所有路径,您需要进行 4 次测试。
要理解它,请考虑对您的代码进行以下更新:
public String testIt(boolean a, boolean b){
String s = "";
if (a) s = s+"a";
if (b) s = s+"b";
return s;
}
涵盖您可以测试 (true,true) 和 (false,false) 的两个分支
但可能路径(结果)的数量是 4:
"", "a", "b", "ab"
上面的公式完全没问题2 <= 3 <= 4
假设我们有以下代码:
public void testIt(boolean a, boolean b){
if (a){
...
}
if (b){
....
}
}
据我所知,有3种计算方法。我将使用其中两个:区域公式和经验法则。
使用区域公式我们有两个区域 if(a){this is the first one}
和 if (b) {this is the second one}
。所以 CC = 两个区域 +1 = 3.
经验法则来自 Testing for ISTQB Advanced Level Technical Test Analyst vol 3
书:
Now, this is all simple enough for a small, modest method like this. For larger functions, drawing the graph and doing the calculation from it can be really painful. So, a simple rule of thumb is this: Count the branching and looping constructs and add 1. The if statements, for, while, and do/while constructs, each count as one. For the switch/case constructs, each case block counts as one. In if and ladder if constructs, the final else does not count. For switch/case constructs, the default block does not count. This is a rule of thumb, but it usually seems to work.
所以根据这个规则CC=两个if条件+1=3.
然而,在Whosebug上与一位用户聊天时,有人说这段代码的CC = 4(他使用了第三种方法),并提供了该用户提供的以下图片:
那么这段代码的圈复杂度是多少? 3 或 4 或?
M = E − N + 2P,
where
E = the number of edges of the graph. N = the number of nodes of the graph. P = the number of connected components.
M = 7 - 6 + 1*2 = 3
关于软件测试,它意味着:
M is an upper bound for the number of test cases that are necessary to achieve a complete branch coverage.
M is a lower bound for the number of paths through the control flow graph (CFG).
All three of the above numbers may be equal: branch coverage <= cyclomatic complexity <= number of paths.
因此,您需要 2 个测试来提供分支覆盖。但不代表会覆盖所有路径。
要涵盖此处的所有路径,您需要进行 4 次测试。
要理解它,请考虑对您的代码进行以下更新:
public String testIt(boolean a, boolean b){
String s = "";
if (a) s = s+"a";
if (b) s = s+"b";
return s;
}
涵盖您可以测试 (true,true) 和 (false,false) 的两个分支
但可能路径(结果)的数量是 4:
"", "a", "b", "ab"
上面的公式完全没问题2 <= 3 <= 4