CYCLOMATIC COMPLEXITY
Cyclomatic complexity is a software metric used to measure the complexity of a
program. It is based on the number of linearly independent paths through a
program's source code.
In other words, it measures the number of decision points in a program, which can
indicate how difficult the code is to understand, test, and maintain.
Why is cyclomatic complexity important in software engineering?
Cyclomatic complexity is crucial in software development as it indicates the code’s quality,
maintainability, and bug-proneness. Higher cyclomatic complexity means the code is more
difficult to understand and maintain.
Cyclomatic complexity can be helpful in identifying areas of the code that may be
difficult to understand, maintain, or test. By analyzing the cyclomatic complexity of a
program, developers can make informed decisions about refactoring, testing, and overall
code quality.
How to calculate Cyclomatic Complexity?
Method-01:
Cyclomatic Complexity = E – N + 2
Here-
 E = Total number of edges in the control flow graph
 N = Total number of nodes in the control flow graph
Method-02:
Cyclomatic Complexity = Total number of closed regions in the control flow graph + 1
Method-03:
Cyclomatic Complexity = P + 1
Here,
P = Total number of predicate nodes contained in the control flow graph
Note-
 Predicate nodes are the conditional nodes.
 They give rise to two branches in the control flow graph.
Mathematical representation:
Mathematically, it is set of independent paths through the graph diagram. The Code
complexity of the program can be defined using the formula –
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
i=0;
n=4;//N-number of nodes present in the graph
while(i<n-1)do
j=i+1;
while(j<n)do
if A[i]<A[j]
thenswap(A[i],A[j]);
end do;
j=j+1;
end do;
Flow graph for this program will be:
Method -1
V(G) = 9 – 7 + 2 = 4
Method -2
V(G) = 3 + 1 = 4 (Condition nodes are 1,2 and 3 nodes)
Method-3
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
V(G) = 4
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 can be calculated manually if the program is small. Automated
tools need to be used if the program is very complex as this involves more flow graphs.
Based on complexity number, team can conclude on the actions that need to be taken for
measure.
Complexity Number Meaning
1-10 Structured and well written code
High Testability
Cost and Effort is less
10-20 Complex Code
Medium Testability
Cost and effort is Medium
20-40 Very complex Code
Low Testability
Cost and Effort are high
>40 Not at all testable
Very high Cost and Effort
Following table gives overview on the complexity number and corresponding meaning of v (G):
Advantages of Cyclomatic Complexity
 It can be used as a quality metric, given the relative complexity of various designs.
 It is used to measure the minimum effort and best areas of concentration for testing.
 It is able to guide the testing process.
 It is easy to apply.
Disadvantages of Cyclomatic Complexity
 It is the measure of the program’s control complexity and not the data complexity.
 In this, nested conditional structures are harder to understand than non-nested structures.
 In the case of simple comparisons and decision structures, it may give a misleading figure.
.

How to calculte Cyclomatic Complexity through various methods

  • 1.
    CYCLOMATIC COMPLEXITY Cyclomatic complexityis a software metric used to measure the complexity of a program. It is based on the number of linearly independent paths through a program's source code. In other words, it measures the number of decision points in a program, which can indicate how difficult the code is to understand, test, and maintain.
  • 2.
    Why is cyclomaticcomplexity important in software engineering? Cyclomatic complexity is crucial in software development as it indicates the code’s quality, maintainability, and bug-proneness. Higher cyclomatic complexity means the code is more difficult to understand and maintain. Cyclomatic complexity can be helpful in identifying areas of the code that may be difficult to understand, maintain, or test. By analyzing the cyclomatic complexity of a program, developers can make informed decisions about refactoring, testing, and overall code quality.
  • 3.
    How to calculateCyclomatic Complexity? Method-01: Cyclomatic Complexity = E – N + 2 Here-  E = Total number of edges in the control flow graph  N = Total number of nodes in the control flow graph Method-02: Cyclomatic Complexity = Total number of closed regions in the control flow graph + 1
  • 4.
    Method-03: Cyclomatic Complexity =P + 1 Here, P = Total number of predicate nodes contained in the control flow graph Note-  Predicate nodes are the conditional nodes.  They give rise to two branches in the control flow graph.
  • 5.
    Mathematical representation: Mathematically, itis set of independent paths through the graph diagram. The Code complexity of the program can be defined using the formula – 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)
  • 6.
    EXAMPLE i=0; n=4;//N-number of nodespresent in the graph while(i<n-1)do j=i+1; while(j<n)do if A[i]<A[j] thenswap(A[i],A[j]); end do; j=j+1; end do;
  • 7.
    Flow graph forthis program will be:
  • 8.
    Method -1 V(G) =9 – 7 + 2 = 4 Method -2 V(G) = 3 + 1 = 4 (Condition nodes are 1,2 and 3 nodes) Method-3 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 V(G) = 4
  • 9.
    Properties of Cyclomaticcomplexity: 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 can be calculated manually if the program is small. Automated tools need to be used if the program is very complex as this involves more flow graphs. Based on complexity number, team can conclude on the actions that need to be taken for measure.
  • 10.
    Complexity Number Meaning 1-10Structured and well written code High Testability Cost and Effort is less 10-20 Complex Code Medium Testability Cost and effort is Medium 20-40 Very complex Code Low Testability Cost and Effort are high >40 Not at all testable Very high Cost and Effort Following table gives overview on the complexity number and corresponding meaning of v (G):
  • 11.
    Advantages of CyclomaticComplexity  It can be used as a quality metric, given the relative complexity of various designs.  It is used to measure the minimum effort and best areas of concentration for testing.  It is able to guide the testing process.  It is easy to apply.
  • 12.
    Disadvantages of CyclomaticComplexity  It is the measure of the program’s control complexity and not the data complexity.  In this, nested conditional structures are harder to understand than non-nested structures.  In the case of simple comparisons and decision structures, it may give a misleading figure. .