Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 19

Cyclomatic Complexity

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
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
What is Cyclomatic Complexity?
 Cyclomatic complexity is a software metric used to measure the
complexity of a program. These metric, measures independent paths
through program source code.
 Independent path is defined as a path  that has atleast one edge
which has  not been traversed before in any other paths.
 Cyclomatic complexity can be calculated with respect to functions,
modules, methods or classes within a program.
 This metric was developed by Thomas J. McCabe in 1976 and it
based on a control flow representation of the program. Control flow
depicts a program as a graph which consists of Nodes and Edges.
Cyclomatic Complexity
 In the graph, Nodes represent processing
tasks while edges represent control flow
between the nodes.
 
Flow graph notation for a program:
Flow Graph notation for a program is defines . several nodes
connected through the edges. Below  are Flow diagrams for
statements like if-else, While, until and normal sequence of flow.
 
Mathematical representation:
 Mathematically, it is set of independent paths through the graph
diagram. The complexity of the program can be defined as –
 V(G) = E – N + 2
 Where,
 E – Number of edges
 N – Number of Nodes
 V (G) = P + 1
 Where P = Number of predicate nodes (node that contains condition)
 
Example –
 01.i = 0;
 02. 
 03.n=4; //N-Number of nodes present in the graph
 04. 
 05.while (i<n-1) do
 06. 
 07.j = i + 1;
 08. 
 09.while (j<n) do
 10. 
 11.if A[i]<A[j] then
 12. 
 13.swap(A[i], A[j]);
 14. 
 15.end do;
 16. 
 17.i=i+1;
 18. 
 19.end do;
Flow graph for this program will  
Computing mathematically
 V(G) = 9 – 7 + 2 = 4
 V(G) = 3 + 1 = 4 (Condition nodes are 1,2 and 3 nodes)
 Basis Set – A set of possible execution path of a program
 1, 7
 1, 2, 6, 1, 7
 1, 2, 3, 4, 5, 2, 6, 1, 7
 1, 2, 3, 5, 2, 6, 1, 7
Properties of Cyclomatic complexity:

 Following are the properties of Cyclomatic complexity:


 V (G) is the maximum number of independent paths in the graph
 V (G) >=1
 G will have one path if V (G) = 1
 Minimize complexity to 10
Cyclomatic Complexity V(G)

Computing the cyclomatic


complexity:

number of simple decisions + 1

In this case, V(G) = 4


Cyclomatic Complexity V(G)

Convert
To

This is NOT a simple decision!


These are simple decisions!

A simple decision chooses one of two options


Steps 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?
Graph Complexity
(Cyclomatic Complexity)
A number of industry studies have indicated
that the higher V(G), the higher the probability
or errors.

modules

V(G)

modules in this range are


more error prone
Basis Path Testing

Next, we derive the


independent paths:
1

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

3 Path 1: 1,2,3,6,7,8
4
5 6 Path 2: 1,2,3,5,7,8
Path 3: 1,2,4,7,8
Path 4: 1,2,4,7,2,4,...7,8
7
Finally, we derive test
cases to exercise these
8
paths.
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");
}
}
}
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
Output from JavaNCSS
NCSS = Non Commenting Source Statements

CCN = cyclomatic complexity number

You might also like