McCabe's Cyclomatic Complexity Number - Software Measurement

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

COMPSCI 702: Software Measurement

McCabes Cyclomatic Complexity Number


Ewan Tempero e.tempero@cs.auckland.ac.nz

www.cs.auckland.ac.nz:ewan
Emilia Mendes (supervisor) emilia@cs.auckland.ac.nz

COMPSCI 702 c Ewan Tempero p.1/14

Lecture Overview
Structure complexity McCabes Cyclomatic Complexity Number References Fenton and Peeger, Chapters 7 & 8 Potential Exam Question

Explain the intuition that supports using McCabes Cyclomatic Complexity Number as a metric for complexity.

COMPSCI 702 c Ewan Tempero p.2/14

Structure complexity: Intuition


The more complex the structure of code . . . the harder it is to understand . . . the more likely it will have a defect . . . the harder it will be to change . . . the longer it will take to produce . . . the more difcult it will be to reuse

COMPSCI 702 c Ewan Tempero p.3/14

What is structure?
control-ow the sequence of instructions that are

executed
data-ow the creation and movement of data between

components of the code


data organisation the relationship of data items to each

other

COMPSCI 702 c Ewan Tempero p.4/14

Directed Graphs
a mathematical structure (with an appealing visual

representation) for representing things that are related consists of vertices (or nodes, or points), connected by edges (or line segments, or arcs). Vertices: A, B, C, D, E, F Edges: (A,C), (A,C), (A,E), (B,C), (B,F), (C,D), (D,A), (E,F),
A

D E F B

Also: undirected graphs, rules restricting edges between

vertices, classication of vertices Graph Theory the study of properties of graphs

COMPSCI 702 c Ewan Tempero p.5/14

Flowgraphs
Directed graphs (owgraphs) can be used to model control

ow of a program
vertex = statement, edge = (A, B) if control ows from

statement A to B
properties of owgraphs may provide information about

properties of the code

COMPSCI 702 c Ewan Tempero p.6/14

Example
A B C D E F G public static boolean isPrime(int n) { boolean prime = true; A int i = 2; while (i < n) { B if (n % i == 0) { prime = false; C } i++; } D return prime; } E

F G

COMPSCI 702 c Ewan Tempero p.7/14

McCabes Cyclomatic Complexity Number (CCN)


measures the number of linearly independent paths through

the owgraph
v(F ) = e n + 2, F the owgraph of the code, n the number

of vertices, e the number of edges


Intuition the larger the CCN the more complex the code Various sources recommend a CCN of no more than 10-15 Example: CCN = 8 7 + 2 = 3

COMPSCI 702 c Ewan Tempero p.8/14

Example 2
A B C D public static boolean isPrimeA(int n) { for (int i = 2; i < n; i++) { A if (n % i == 0) { return false; B } } return true; C }

COMPSCI 702 c Ewan Tempero p.9/14

(counter) Example

if (a) if (b) goto d: else goto e: else if (c) goto d: else goto e: d: print "Hi. " e: print "How are you?"

if (u) if (v) if (w) print "Hi" print "How are you?"

CNN= 11 9 + 2 = 4

CNN= 7 5 + 2 = 4

COMPSCI 702 c Ewan Tempero p.10/14

(counter) Example (2)


From graph theory, v(f ) = d + 1, where d is the number of

predicate nodes (tests/conditions)


public static boolean isPrimeA(int n) { for (int i = 2; i < n; i++) { if (n % i == 0) { return false; } } return true; } public static boolean dividableBy4(int n) { if (n % 2 == 0) { if ((n/2) % 2 == 0) { return true; } } return false; }

COMPSCI 702 c Ewan Tempero p.11/14

Switch statements
switch (a) { case one: break; case two: // fall through case three: break; default: } .... if (a == one) { // do case one } else if (a == two) { // do case two // do case three } else if (a == three) { // do case three } else { // default }

COMPSCI 702 c Ewan Tempero p.12/14

Exceptions
public void aMethod(Object obj) { // some code without conditionals obj.toString(); // more code without conditionals }

... try { // some code FileInputStream f = new FileInputStream("foo") // more code } catch (FileNotFoundException e) { e.printStackTrace(); } catch (SecurityException e2) { e2.printStackTrace(); } finally { System.out.println("finally!"); } ...

COMPSCI 702 c Ewan Tempero p.13/14

Validation
Attribute relation model: direct metric Unit Denition Model: number of linearly independent paths Instrumentation Model: construct the owgraph of the code,

determine the number of edges and vertices compute or (much easier) identify condition vertices compute
Measurement protocol model: for loops, exceptions, switch

statements
Entity population Model: does it make sense to talk about

average CCN?
Representation Condition: for complexity dubious for number of linearly independent paths valid

COMPSCI 702 c Ewan Tempero p.14/14

You might also like