Cyclomatic complexity is a measure of linearly independent paths in a code. Cyclomatic complexity is used to measure the complexity of a code. Lower cyclomatic complexity means a lower complexity of code.
Note: The cyclomatic complexity of a code can be greater than or equal to 1.
The cyclomatic complexity of a code can be calculated as:
CC = E - N + 2P
E
: The number of edges in the control flow graph of the code.N
: The number of nodes in the control flow graph of the code.P
: The number of connected components in the control flow graph of the code.CC = D + 1
D
: The number of decisions in the control flow graph of the code.The value of cyclomatic complexity indicates how readable and testable a code is:
Value range | Code information |
1 - 10 |
|
10 - 20 |
|
> 20 |
|
> 50 |
|
Consider the code snippet below, which is a simple program to add two numbers together.
int a = 1;int b = 2;cout<<"a + b = "<<a+b;
The control flow graph of the code snippet above will be as follows:
The cyclomatic complexity of the control flow graph will be calculated as:
E
: the number of edges in the control graph = 2N
: the number of nodes in the control graph = 3P
: the number of connected components in the control graph = 1D
: the number of decisions in the control graph = 0CC = E - N + 2P
CC = 2 - 3 + 2(1)
CC = 1
CC = D + 1
CC = 0 + 1
CC = 1
Consider the code snippet below, which is a program to get the maximum of two numbers.
int a = 1;int b = 2;if (a>b){cout<<"a is greater";}else{cout<<"b is greater";}
The control flow graph of the code snippet above will be as follows:
The cyclomatic complexity of the control flow graph will be calculated as:
E
: the number of edges in the control graph = 6N
: the number of nodes in the control graph = 6P
: the number of connected components in the control graph = 1D
: the number of decisions in the control graph = 1CC = E - N + 2P
CC = 6 - 6 + 2(1)
CC = 2
CC = D + 1
CC = 1 + 1
CC = 2
Consider the code snippet below, which is a program to print numbers from 1 to 5 using a for loop.
int i;for (i = 1; i <= 5; i++){cout<<i<<endl;}
The control flow graph of the code snippet above will be as follows:
The cyclomatic complexity of the control flow graph will be calculated as:
E
: the number of edges in the control graph = 6N
: the number of nodes in the control graph = 6P
: the number of connected components in the control graph = 1D
: the number of decisions in the control graph = 1CC = E - N + 2P
CC = 6 - 6 + 2(1)
CC = 2
CC = D + 1
CC = 1 + 1
CC = 2
Consider the code snippet below, which is a program to print numbers from 1 to 5 using a while loop.
int i = 1;while (i <= 5){cout<<i<<endl;i++;}
The control flow graph of the code snippet above will be as follows:
The cyclomatic complexity of the control flow graph will be calculated as:
E
: the number of edges in the control graph = 5N
: the number of nodes in the control graph = 5P
: the number of connected components in the control graph = 1D
: the number of decisions in the control graph = 1CC = E - N + 2P
CC = 5 - 5 + 2(1)
CC = 2
CC = D + 1
CC = 1 + 1
CC = 2
Consider the code snippet below, which is a program to get the minimum element of an array:
int arr[] = {1, 7, 4, 9, 12, 7, -1, 13};int arrSize = 8;int min = arr[0];for(int i = 0; i < arrSize; i++){if( arr[i] < min){min = arr[i];}}cout<<"Array minimum = "<<min;
The control flow graph of the code snippet above will be as follows:
The cyclomatic complexity of the control flow graph will be calculated as:
E
: the number of edges in the control graph = 11N
: the number of nodes in the control graph = 10P
: the number of connected components in the control graph = 1D
: the number of decisions in the control graph = 2CC = E - N + 2P
CC = 11 - 10 + 2(1)
CC = 3
CC = D + 1
CC = 2 + 1
CC = 3
Free Resources