What Is Cyclomatic Complexity – Learn With An Example
Cyclomatic Complexity
Cyclomatic complexity is a software metric used to indicate the complexity of a program. It is a quantitative measure of the number of linearly independent paths through a program's source code. It was developed by Thomas J. McCabe, Sr. in 1976.
- Cyclomatic Complexity is a very common buzz word in the Development community which is used for measuring development process time line.
- Mainly used to determine the complexity of a piece of code or functionality.
- Developed by MaCabe and helps to identify the below questions for the programs/features
For QA How It can be used

Let me go step by step: first understand
- how is it calculated ? and then we will move on to understand how the level of testing is determined.
The calculation of CC revolves around 3 concepts
- Nodes
- Edges
- Connected components
Cyclomatic Complexity formula
The formula for calculating CC is as:
CC = E – N + 2P
where,
E = the number of edges in the control flow graph
N = the number of nodes in the control flow graph
P = the number of connected components
Calculating Cyclomatic Complexity using these steps -
Method-01:
Method-02:
Method-03:
P = Total number of predicate nodes contained in the control flow graph
Note-
Types of Complexity
- Construction of graph with nodes and edges from code.
- Identification of independent paths.
- Cyclomatic Complexity Calculation
- Design of Test Cases
- Cyclomatic complexity is calculated using the control flow representation of the program code.
- In control flow representation of the program code,
- Nodes represent parts of the code having no branches.
- Edges represent possible control flow transfers during program execution
Method-01:
- Cyclomatic Complexity = Total number of closed regions in the control flow graph + 1
Method-02:
- Cyclomatic Complexity = E – N + 2
- E = Total number of edges in the control flow graph
- N = Total number of nodes in the control flow graph
Method-03:
- Cyclomatic Complexity = P + 1
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.
- Cyclomatic complexity can be one of the most difficult software quality metrics to understand. And that makes it difficult to calculate.
- It is a software metric that measures the logical complexity of the program code.
- It counts the number of decisions in the given program code.
- It measures the number of linearly independent paths through the program code.
- Limit code complexity.
- Determine the number of test cases required.
Disadvantages of Cyclomatic Complexity-
Final Conclusion :
- If we have Lower Cyclomatic Complexity means its Better Code
- Higher numbers of CYC are bad. Lower numbers are good.
- That's because code with high complexity is difficult to test. And it's likely to result in errors.
- So, code with low complexity is easier to test. And it's less likely to produce errors
Comments
Post a Comment