Cyclomatic复杂性 - 绘制此java语句的控制流图

时间:2016-03-11 21:42:27

标签: testing metrics control-flow-graph

任何人都可以帮忙吗?

while (x > level)
    x = x – 1;
x = 0

1 个答案:

答案 0 :(得分:1)

可以使用提供的公式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非常清楚地指出如何简化JavaC#C++等语言的计算。但是,必须仔细执行标识条件,如图所示{{ 3}}:

- 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.