McCabe's Cyclomatic Complexity Number - Software Measurement
McCabe's Cyclomatic Complexity Number - Software Measurement
McCabe's Cyclomatic Complexity Number - Software Measurement
www.cs.auckland.ac.nz:ewan
Emilia Mendes (supervisor) emilia@cs.auckland.ac.nz
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.
What is structure?
control-ow the sequence of instructions that are
executed
data-ow the creation and movement of data between
other
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
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
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
the owgraph
v(F ) = e n + 2, F the owgraph of the code, n the number
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 }
(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?"
CNN= 11 9 + 2 = 4
CNN= 7 5 + 2 = 4
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 }
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!"); } ...
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