Cyclomatic Complexity与应用
上一篇 / 下一篇 2007-11-05 17:12:19 / 个人分类:软件测试技术研究
(vUVAF9| H9o0Cyclomatic Complexity
w7Dk(SR9[{#q051Testing软件测试网G'An8GQ J_xReference:http://www.sei.cmu.edu/str/descrīptions/cyclomatic.html51Testing软件测试网MR8rPOj(f q
Z2_Un|%`_]0+目的:结构复杂度的测量方法,可以用来度量软件的复杂性,评估软件的内在风险和稳定性。51Testing软件测试网!H%c4]+l%s4u
4LHf,nn
KN0+基本原理51Testing软件测试网:H2a(^R)EU
++相关概念51Testing软件测试网c.ZG&f1Kk?
1. 强连通图(strongly-connected graph,有向图)51Testing软件测试网}DV\d$y7h
图中任意两个节点A和B之间,存在一条从A到B的路径,也存在一条从B到A的路径。
? CyJ
o{.O02. 控制流图(control flow graph)
Q2x8y|yg
{W0G=(N,E),一组节点和边的集合,用来表述程序模块的流程,节点可以是程序语句或基本块(即只有一个入口和一个出口的语句的序列)。51Testing软件测试网H6m6_)S c*QRn!y&u
3. 图的秩数(cyclomatic number,这个词怎么翻译好呢?)51Testing软件测试网r)B ]JqI@"}&m]
v=e-n+1(e是图中边的个数,n是图中节点的个数)
q%K0Uh:~04. 线性独立路径(linearly independent path)
(I+Y$n;v3Q%xJ:\0线性独立路径的集合中的任意一条路径不能用集合中的其他路径构造而成,而集合外的任意一条路径可以用集合内的路径构造而成。51Testing软件测试网7@"E;w)hn9r
5. 定理:强连接图的秩数等于线性独立路径的个数51Testing软件测试网h`6[ nx_*}`
x]]6G2k ~QL0+Cyclomatic Complexity的计算
yD8sfb"J]0The cyclomatic complexity of a software module is calculated from a connected graph of the module (that shows the topology of control flow within the program):
Cyclomatic complexity (CC) = E - N + p51Testing软件测试网P4K lL-}fk
51Testing软件测试网RfB?)T)Pwhere E = the number of edges of the graph
V ]wd$m0MCa I0N = the number of nodes of the graph51Testing软件测试网_"WzE/k
p = the number of connected components
0_7ppGQ0assisted byCyclomatic Complexity Analyzersfor various types of source codes51Testing软件测试网 R?:c(Y [*z*u\i
4o Y#Zp5Rb mj0+Cyclomatic complexity能告诉我们什么?51Testing软件测试网.EP}Xwe{L8E
51Testing软件测试网;i$w,ya xoO CC Risk Evaluation
E!G/Ol2q0 1-10 a simple program, without much risk51Testing软件测试网6W
Oz(^Bj;z
11-20 more complex, moderate risk
4r'@K leP!V0 21-50 complex, high risk program
+LJ;QO%f[0greater than 50 untestable program (very high risk)