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.


How to Calculate Cyclomatic Complexity?

The calculation of CC revolves around 3 concepts
  1. Nodes
  2. Edges
  3. Connected components 
Statements in a program are represented as nodes, and control paths from one statement to another are represented by Edges.

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 -
 
  • 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
There are 3 commonly used methods for calculating the cyclomatic complexity-
 
Method-01:
 
  •  Cyclomatic Complexity = Total number of closed regions in the control flow graph + 1
 
Method-02:
  •   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-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.

Types of Complexity



Tools Used for Calculating Cyclomatic Complexity-
Why is Cyclomatic Complexity Important ?
  • 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.

How to Understand what Cyclomatic Complexity Means ?-








Usage of Cyclomatic Complexity-

 

 
Properties of Cyclomatic Complexity-
 
 

Benefits of Cyclomatic Complexity-




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

Popular posts from this blog

Event Driven Architecture - Key Principles

Best Engineering Principles