cyclomatic complexity dan fleck fall 2009 dan fleck fall 2009

12
Cyclomatic Complexity Dan Fleck Fall 2009

Upload: shanon-simpson

Post on 24-Dec-2015

232 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Cyclomatic Complexity Dan Fleck Fall 2009 Dan Fleck Fall 2009

Cyclomatic ComplexityCyclomatic Complexity

Dan Fleck

Fall 2009

Dan Fleck

Fall 2009

Page 2: Cyclomatic Complexity Dan Fleck Fall 2009 Dan Fleck Fall 2009

What is it?What is it?

A software metric used to measure the complexity of software

Developed by Thomas McCabe Described (informally)

as the number of

simple decision

points + 1

A software metric used to measure the complexity of software

Developed by Thomas McCabe Described (informally)

as the number of

simple decision

points + 1

Page 3: Cyclomatic Complexity Dan Fleck Fall 2009 Dan Fleck Fall 2009

What is it?What is it?

Essentially the number of linearly independent paths through the code The code has no decision statements:

complexity = 1 The code has an if statement, there are two

paths through the code: complexity = 2

Essentially the number of linearly independent paths through the code The code has no decision statements:

complexity = 1 The code has an if statement, there are two

paths through the code: complexity = 2

Page 4: Cyclomatic Complexity Dan Fleck Fall 2009 Dan Fleck Fall 2009

Cyclomatic Complexity V(G)

Computing the cyclomatic complexity:

number of simple decisions + 1

In this case, V(G) = 4

From Pressman Slides - Software Engineering a Practical Approach 6,e

Page 5: Cyclomatic Complexity Dan Fleck Fall 2009 Dan Fleck Fall 2009

Cyclomatic Complexity V(G)

This is NOT a simple decision!

These are simple decisions!

Convert To

A simple decision chooses one of two optionsA simple decision chooses one of two options

Page 6: Cyclomatic Complexity Dan Fleck Fall 2009 Dan Fleck Fall 2009

Steps to calculate complexitySteps to calculate complexity

Draw the control flow graph of the code Count the number of edges = E Count the number of nodes = N Count the number of connected

components = P Complexity = M – N + 2P What is the complexity for the graph?

Draw the control flow graph of the code Count the number of edges = E Count the number of nodes = N Count the number of connected

components = P Complexity = M – N + 2P What is the complexity for the graph?

Page 7: Cyclomatic Complexity Dan Fleck Fall 2009 Dan Fleck Fall 2009

Graph Complexity (Cyclomatic Complexity)

A number of industry studies have indicated A number of industry studies have indicated that the higher V(G), the higher the probability that the higher V(G), the higher the probability or errors.or errors.

V(G)V(G)

modulesmodules

modules in this range are modules in this range are more error pronemore error prone

From Pressman Slides - Software Engineering a Practical Approach 6,e

Page 8: Cyclomatic Complexity Dan Fleck Fall 2009 Dan Fleck Fall 2009

Basis Path Testing

Next, we derive the Next, we derive the independent paths:independent paths:

Since V(G) = 4,Since V(G) = 4,there are four pathsthere are four paths

Path 1: 1,2,3,6,7,8Path 1: 1,2,3,6,7,8Path 2: 1,2,3,5,7,8Path 2: 1,2,3,5,7,8Path 3: 1,2,4,7,8Path 3: 1,2,4,7,8Path 4: 1,2,4,7,2,4,...7,8Path 4: 1,2,4,7,2,4,...7,8

Finally, we derive testFinally, we derive testcases to exercise these cases to exercise these paths.paths.

11

22

3344

55 66

77

88

From Pressman Slides - Software Engineering a Practical Approach 6,e

Page 9: Cyclomatic Complexity Dan Fleck Fall 2009 Dan Fleck Fall 2009

What is the complexity?What is the complexity?

public void howComplex(i) { while (i<10) {

i++; System.out.printf("i is %d", i); if (i%2 == 0) { System.out.println("even"); } else { System.out.println("odd"); } } }

Page 10: Cyclomatic Complexity Dan Fleck Fall 2009 Dan Fleck Fall 2009

What is the complexity V(G)?What is the complexity V(G)?

public void howComplex() { int i=20; while (i<10) { System.out.printf("i is %d", i); if (i%2 == 0) { System.out.println("even"); } else { System.out.println("odd"); } } }

V(G) = 2 decisions + 1 = 3

Page 11: Cyclomatic Complexity Dan Fleck Fall 2009 Dan Fleck Fall 2009

Output from JavaNCSSOutput from JavaNCSSNCSS = Non Commenting Source Statements

CCN = cyclomatic complexity number

Page 12: Cyclomatic Complexity Dan Fleck Fall 2009 Dan Fleck Fall 2009

Better Tool Found (Nov 2009)Better Tool Found (Nov 2009) RefactorIt http://sourceforge.net/projects/refactorit/ http://www.aqris.com/display/A/Refactorit

Provides Cyclomatic Complexity many other metrics, and refactoring tools Comment density Coupling Depth of inheritance, …

RefactorIt http://sourceforge.net/projects/refactorit/ http://www.aqris.com/display/A/Refactorit

Provides Cyclomatic Complexity many other metrics, and refactoring tools Comment density Coupling Depth of inheritance, …