## Introduction to Cyclomatic Complexity

The quantitative calculation of the number of linearly independent paths in a code section is the cyclomatic complexity. This is a measure in software used to display how complex a system is and is measured with the system control flow graph. This calculation has used the definition of program graph and the number of paths through a program is calculated and regulated. The structure and the topological complexity of a graph can be compared to the computer program. Basically the Cyclomatic are of two types which are accidental complexity and Essential Complexity.

### 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.

- 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.

4.6 (3,144 ratings)

View Course

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 Fibonacci 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 Fibonacci 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 Fibonacci 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 a 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 –