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

问题描述 投票:1回答:1

有人能帮忙吗?

while (x > level)
    x = x – 1;
x = 0
testing metrics control-flow-graph
1个回答
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++等语言的计算。但是,必须仔细执行识别条件,如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.
© www.soinside.com 2019 - 2024. All rights reserved.