圈复杂度 - 绘制此 java 语句的控制流图
Cyclomatic complexity - draw control flow graph for this java statement
有人可以帮忙吗?
while (x > level)
x = x – 1;
x = 0
可以使用提供的公式计算圈复杂度 here。
Cyclomatic complexity = E - N + P
where,
E = number of edges in the flow graph.
N = number of nodes in the flow graph.
P = number of nodes that have exit points
对于您的情况,图表应如下所示:
--------------- ----------
| x > level |----- NO ------>| x = x-1|
|-------------| ----|-----
| |---------------------
|
Yes
|
-------|----------
| End while (if) |
-------|----------
|
|
---------
| x = 0 |
----------
(非ASCII艺术人)
所以,cyclomatic complexity
应该是:
E = 4, N = 4, P = 2 => Complexity = 4 - 4 + 2 = 2
[编辑]
Ira Baxter
很好地指出了如何为 Java
、C#
、C++
等语言简化此计算。但是,必须仔细执行识别条件,如图所示 here:
- Start with a count of one for the method.
- Add one for each of the following flow-related elements that are found in the method.
Returns - Each return that isn't the last statement of a method.
Selection - if, else, case, default.
Loops - for, while, do-while, break, and continue.
Operators - &&, ||, ?, and :
Exceptions - catch, finally, throw, or throws clause.
有人可以帮忙吗?
while (x > level)
x = x – 1;
x = 0
可以使用提供的公式计算圈复杂度 here。
Cyclomatic complexity = E - N + P
where,
E = number of edges in the flow graph.
N = number of nodes in the flow graph.
P = number of nodes that have exit points
对于您的情况,图表应如下所示:
--------------- ----------
| x > level |----- NO ------>| x = x-1|
|-------------| ----|-----
| |---------------------
|
Yes
|
-------|----------
| End while (if) |
-------|----------
|
|
---------
| x = 0 |
----------
(非ASCII艺术人)
所以,cyclomatic complexity
应该是:
E = 4, N = 4, P = 2 => Complexity = 4 - 4 + 2 = 2
[编辑]
Ira Baxter
很好地指出了如何为 Java
、C#
、C++
等语言简化此计算。但是,必须仔细执行识别条件,如图所示 here:
- Start with a count of one for the method.
- Add one for each of the following flow-related elements that are found in the method.
Returns - Each return that isn't the last statement of a method.
Selection - if, else, case, default.
Loops - for, while, do-while, break, and continue.
Operators - &&, ||, ?, and :
Exceptions - catch, finally, throw, or throws clause.