代码环复杂度的计算公式

286 阅读1分钟

i042416 commented on Feb 17, 2017

来了解一下软件工程中代码环复杂度的定义。

the cyclomatic complexity of a program is defined with reference to the control flow graph of the program, a directed graph containing the basic blocks of the program, with an edge between two basic blocks if control may pass from the first to the second. The complexity M is then defined as:
M = E − N + 2, where
E = the number of edges of the graph.
N = the number of nodes of the graph.
Take this very simple report for example, it has cyclomatic complexity calculated as 3 by code inspector.

IF lv_value = 1.
  WRITE: / 'always happend'.
ELSEIF lv_value = 2.
  WRITE: / 'not possible'.
ELSE.
  WRITE: / 'even not possible'.
ENDIF.

Why calculated as 3? See the directed graph for this simple report below:

[图片]

在这里插入图片描述

要获取更多Jerry的原创文章,请关注公众号"汪子熙":