## Introduction to Cyclomatic Complexity

Cyclomatic complexity is developed by Thomas J. McCabe in 1976. He recommended that programmers should count the complexity of the modules on which they are working, and split them into smaller modules so that whenever the cyclomatic complexity of the module exceeded 10. 10 is the maximum cyclomatic complexity for one program. It is a measurement of the source code complexity that is being related to a number of coding errors. It determines the independent paths, which would be very helpful for the developers and testers.

This helps in testing the software and gives us more confidence that every aspect of our program has been tested. This improves the code quality and also helps in concentrate more on invisible paths if any. It generally calculated by developing a control flow graph of the code that measures the number of linearly independent paths through a program to measure the complexity of a piece of code accurately. This helps to improve software quality.

### Definition

The cyclomatic complexity of the code is the number of linearly independent paths within it is calculated.

### How to Calculate Cyclomatic Complexity?

Calculating the cyclomatic complexity of the program is very easy by the following formula.

**Cyclomatic Complexity = E – N + 2P**

**E => The no. of edges of the graph****N => The No. of nodes of the graph****P => The no of connected components**

There is an alternate formula if we consider the exit point which backs to your entry point. And you will create it like a cycle.

**Cyclomatic Complexity = E – N + P**

**Cyclomatic Complexity = E – N + 1**

We have one more way of calculating this. This is a more easy way.

4.6 (3,144 ratings)

View Course

- Draw graph
- Then connect exit point to the entry point
- And then count holes in the graph

### Look at the following figure:

Following are some Flow Graph Notations:

**If-then-else: **

**While:**

**Do-While:**

**For: **

** **

If the program is not having any loop then its complexity is lower. When the program encounters any loop then complexity gets increased.

Suppose we have one if the condition defined then we got complexity as 2. Because of the condition is having two conditions True and False.

This technique mostly used in basic testing of the white box. Cyclomatic complexity represents a minimum no. of tests required to execute every path in the code.

Different languages have different tools to measure the cyclomatic complexity of the program.

### Steps to Calculate the Cyclomatic Complexity are as Follows

- Draw the flowchart or a graph diagram from the code.
- Now, In the second step check and identify how many independent paths it has.
- Then calculate the cyclomatic complexity by formula mentioned below:

**M = E –N +2P**

- According to the measure design the test cases.

Now, you may get a question that, how it may get actually calculated. Let’s go ahead and understand how we are actually going to calculate it.

Consider the following Java code example:

This program calculates the fibonacii series like:

0+1=1

1+1=2

2+1=3

3+2=5

5+3=8

8+5=13

//Following program is to just print the fibonacii series

`class Printno {`

Public static void main(String[] args){

int max = 20 ;

int pre = 0;

int next = 1;

System.out.println(“The Fibonacii series is : ” +prev);

While(next<= max){

System.out.println(next);

Sum = prev + next;

Prev = next;

Next =sum;

}

}

}

>javac Printno.java

>java Printno

O/p:

The fibonacii series is: 0

1

1

2

3

5

8

13

Give a closer look at the above program. You will find one while loop. This program consists of only one while loop.

Now the time is to draw a graph for it.

**Control flow graph as below:**

** Flowchart **

Now, TO calculate the complexity of the above program, first we need to calculate the total no. of edges:

Total no. of edges: 6

Now, calculate the total no.of nodes.

Total no.of Nodes: 5

**Formula: M = E-N +2p**

**M = 6 -5 + 2**

**M = 1+2**

**M=3**

So, the cyclomatic complexity for this program is 3.

Complex codes are difficult to maintain and update or modify. As we all know that cyclomatic complexity should not exceed 10.

### Types of Complexity

There are Two Types of Complexity :

#### Essential Complexity**: **

This complexity is a type of code that we cannot ignore.

Ex. The flight management system is a more complex one.

#### Accidental Complexity:

As the name suggests it because something happened like bug fixing, Patching, Modification, etc in the system. Mostly we are only working on Accidental complexity.

### Benefits of Cyclomatic Complexity:

- As simple logic, if complexity reduces, we are more convenient to understand.
- If the more complex program is there then the programmer needs to cha=eck no of possibilities get increased.
- Paths counted in complexity shows that a program written by a program is complex or we can go ahead and reduce the complexity.
- It reduces the coupling of code.
- Suppose a program has cyclomatic complexity of 5.that means there are 5 different independent paths through the method.
- This means 5 test cases have to be made to implement this code for testing.
- Hence, it always good to get a lesser number for cyclomatic complexity.
- This process is required because the highly coupled code is very difficult to modify.
- The higher the complexity no of the code that means code is also more complex.

### Tools used for Calculating Cyclomatic Complexity are

- Cyclo
- CCCC
- McCabe IQ
- GCov
- Bullseye Coverage
- PMD
- LC2
- Findbugs
- Jarchitect

### Conclusion

Cyclomatic complexity is the measure of the program complexity. This measure helps us to understand the required work to be done and how complex is the software going to be. Cyclomatic complexity is part of White Box Testing.

### Recommended Articles

This has been a guide to Cyclomatic Complexity. Here we discuss How to Calculate Cyclomatic Complexity? along with Benefits and types of complexity. You may also look at the following articles to learn more –