Introduction to Cyclomatic Complexity
Cyclomatic complexity in software testing is used to measure the number of logical paths of a program. Basically, it evaluates the complexity of linearly independent paths in the source code of a program. For example, if the number of paths/points is more, the program will be more complex. This is calculated using Control Flow Graph. Cyclomatic complexity is of two types: Essential and Accidental complexity. In this article, the calculation, working, advantages, and types of cyclomatic complexity are discussed in detail.
How to Calculate Cyclomatic Complexity?
It is calculated by developing a Control Flow Graph of the code that measures the number of linear-independent pass through a program module.
It can be represented using the following formula:
Method 1:
Cyclomatic Complexity = E – N + 2P, where
- E = The number of edges of the graph
- N = The number of nodes in the graph
- P = The number of connected components
Method 2:
This formula is used when the exit point is considered, which returns to the entry point. This will create a cycle.
Cyclomatic Complexity = E – N + P
Cyclomatic Complexity = E – N + 1
Method 3:
This is the easier way of representation in the graph form:
- Draw flow graph
- Connect the exit point to the entry point
- And then count holes in the graph
Consider the following figure:
The following are some of the representations of Flow Graph Notations:
If-then-else:
While:
Do-While:
For:
If the program/ source code does not have any loop or no control flow statement, then the cyclomatic complexity is 1. If the program/ source code encounters any loop, the complexity increases accordingly. For example, If the source code contains one ‘if’ condition then the cyclomatic complexity will be 2. Because the condition is having two paths one for true and the other for false. Continue reading to understand it better.
This technique is mostly used in basic testing of the white box. It represents a minimum no. of tests required to execute each path in the code. Different programming languages have different tools to measure this complexity of the program.
Steps to Calculate Cyclomatic Complexity with McCabe’s Formula
The classical steps to calculate cyclomatic complexity through McCabe’s (M) formula are as follows.
- Draw the flowchart or a graph diagram with nodes and edges from the code.
- Identify how many independent paths are there.
- Then calculate the cyclomatic complexity by the formula mentioned below:
- Measure test cases.
M = E –N +2P
Before calculating through a flowchart/graph, let us consider the following example of Java code for better understanding:
The below program calculates the Fibonacci series:
0+1=1
1+1=2
2+1=3
3+2=5
5+3=8
8+5=13
Code:
//Following program is to just print the Fibonacci series
class Main {
public static void main(String[] args){
int sum;
int max = 20 ;
int pre = 0;
int next = 1;
System.out.println("The Fibonacii series is : " +pre);
while(next<= max){
System.out.println(next);
sum = pre + next;
pre = next;
next = sum;
}
}
}
Output:
This above program consists of only one while loop.
Let us understand this same program through a flowchart:
Flowchart:
To calculate the complexity of the preceding program, we must first calculate the total number of edges i.e lines in the flowchart:
Total no. of edges (E) =6
Now, calculate the total no.of nodes i.e shapes in the flowchart.
Total no.of Nodes (N) =5
Further, calculate the total no. of predicate nodes (node that contains condition)
Total no. of connected components (P) =1
Add the values in the formula: M = E – N +2p
M = 6 – 5 + 2
M = 3
So, the cyclomatic complexity for this program is 3. Note that the complexity should not exceed 10.
Complex codes are difficult to maintain, update and modify.
Types of Complexity
The two major types of complexity are as follows.
1. Essential Complexity: Essential complexity is the measurement of no. of entry points, non-deductible nodes, and termination points to solve a problem. This is a type of code that cannot be ignored. Ex. The flight management system.
2. Accidental Complexity: Accidental complexity refers to challenges like fixing, patching, modification, etc., in the system to solve problems. Ex. Fixing a software bug in a mobile device.
Advantages of Cyclomatic complexity:
- Paths in complexity determine whether a program is complex, or whether there is a need to reduce complexity. The higher no. of complexity means the code is more complex.
- It reduces the coupling of code
- The risk involved with the program can be analyzed.
- The use of these metrics in the first place reduces the problems associated with the program.
- Developers and testers can determine an independent path for executions.
Tools used for Calculating Cyclomatic Complexity
- Cyclo
- CCCC
- McCabe IQ
- GCov
- Bullseye Coverage
- PMD
- LC2
- Findbugs
- Jarchitect
Recommended Articles
This has been a guide to Cyclomatic Complexity. Here we have discussed its calculate and the tools used. Also the types and benefits of complexity. You can also learn more by reading the articles listed below:-