Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cyclomatic Complexity Philippe CHARMAN Last update: 04-02-2013.

Similar presentations


Presentation on theme: "Cyclomatic Complexity Philippe CHARMAN Last update: 04-02-2013."— Presentation transcript:

1 Cyclomatic Complexity Philippe CHARMAN charman@fr.ibm.com http://users.polytech.unice.fr/~charman/ Last update: 04-02-2013

2 Control Flow Graph Definition: representation, using graph notation, of all paths that might be traversed through a program during its execution Node –Function node: –Predicate node: –Empty nodes: Edge: –connection between 2 nodes, –represents the flow of control between 2 nodes c entryexit f

3 Control Flow Graph c entry exit f f f

4 Cyclomatic Complexity Cyclomatic complexity is a software metric developed by McCabe in 1976 and is used to indicate the complexity of a program The cyclomatic complexity v(G) of a single function/method is equal to: v(G) = E − N + 2 where E = the number of edges N = the number of nodes

5 Example #1 int f1(int x){ return x+1; } E = 2 N = 3 v(G) = 2 – 3 + 2 = 1 entry exit x+1

6 Example #2 int f2(int x, int y) { x = a(); y = b(); return x+y; } E = 4 N = 5 v(G) = 1 entry exit x=a() y=b() x+y

7 Example #3 void f3(int x) { if (x == 0) g(); else h(); } E = 6 N = 6 v(G) = 2 entry exit if true false g() h()

8 Example #4 void f4(int x) { if (x == 0) g(); } E = 5 N = 5 v(G) = 2 entry exit if truefalse g()

9 Example #5 void f5(int x) { if (x == 0) return; else h(); } E = 5 N = 5 v(G) = 2 entry exit if true false h()

10 Example #6 void f6(int x) { if (x > 0) a(); else b(); if (odd (x)) c(); else d(); } E = 11 N = 10 v(G) = 3 if entry exit true false true false a() b() d() c()

11 Example #7 void f7(int x, int y) { if (x > 0) { if (y > 0) a(); else b(); } else { if (y > 0) c(); else d(); } E = 14 N = 12 v(G) = 4 if entry if exit true false true false true false a() b() c() d()

12 Example #8 void f8(int x) { switch (x) { case 0: a(); break; case 1: b(); break(); case 2: c(); break; } E = 10 N = 8 v(G) = 4 switch entry exit a() b()c()

13 Example #9 void f9(int x) { if (x == 0) a(); if (x == 1) b(); if (x == 2) c(); } E = 13 N = 11 v(G) = 4 entry if exit a() b() c()

14 Example #10 void f10(int n) { for (int i = 0 ; i < n ; i++) { a(); } E = 5 N = 5 v(G) = 2 entry exit i < n true false a() i = 0

15 Example #11 E = 13 N = 10 v(G) = 5

16 Cyclomatic Complexity for a single function/method Cyclomatic complexity = #edges - #nodes +2 Cyclomatic complexity = number of linearly independent paths Cyclomatic complexity = number of decision points + 1 Cyclomatic complexity = numbers of regions of the flow graph providing edges are not crossing each other Be careful with simple rules such as add 1 to each if, add 1 to each switch/case, add 1 to each switch/default, et.

17 v(G) = Number of regions entry a() exit b() x+y a()b() if c()d() if entry exit

18 Cyclomatic Complexity Cyclomatic Risk Complexity 1-10A simple, low risk program 11-20Moderate complexity, risk complexity 21-50Complex, high risk > 50High risk, detestable program


Download ppt "Cyclomatic Complexity Philippe CHARMAN Last update: 04-02-2013."

Similar presentations


Ads by Google