EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Cyclomatic Complexity

By Pooja GuptaPooja Gupta

Home » Software Development » Software Development Tutorials » Software Testing Tutorial » Cyclomatic Complexity

Cyclomatic-Complexity

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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.

  1. Draw graph
  2. Then connect exit point to the entry point
  3. And then count holes in the graph

Look at the following figure

Flow Graph Notations

Following are some Flow Graph Notations:

If-then-else:  

Cyclomatic Complexity-2

While:

Cyclomatic Complexity-3

Do-While:

Cyclomatic Complexity-4

For: 

Cyclomatic Complexity-5  

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.

Popular Course in this category
All in One Software Development Bundle (600+ Courses, 50+ projects)600+ Online Courses | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (3,144 ratings)
Course Price

View Course

Related Courses
Software Testing Training (9 Courses, 2 Projects)Penetration Testing Training Program (2 Courses)TestNG Training (4 Courses, 2 Project)

This technique mostly used in basic testing of the white box. It 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

The steps to calculate cyclomatic complexity are as follows.

  1. Draw the flowchart or a graph diagram from the code.
  2. Now, In the second step check and identify how many independent paths it has.
  3. Then calculate the cyclomatic complexity by formula mentioned below:

M = E –N +2P

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

Control flow graph

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

The two major types of complexity are given.

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

Following are the benefits explained.

  • 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

Below are the tools used for calculating.

  • Cyclo
  • CCCC
  • McCabe IQ
  • GCov
  • Bullseye Coverage
  • PMD
  • LC2
  • Findbugs
  • Jarchitect

Conclusion

It 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. It is a part of White Box Testing.

Recommended Articles

This has been a guide to Cyclomatic Complexity. Here we discuss How to Calculate Cyclomatic Complexity and tools used? along with benefits and types of complexity. You may also look at the following articles to learn more –

  1. Introduction to Linux
  2. Introduction To C++
  3. What is HTML5?
  4. What is Hibernate?

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

1 Shares
Share
Tweet
Share
Primary Sidebar
Software Testing Tutorial
  • Advance
    • Cyclomatic Complexity
    • Decision Table Testing
    • Decision Tree Algorithm
    • What is Continuous Integration
    • Mantis Bug Tracker
    • Equivalence Partitioning
    • Gantt Chart Software
    • Install TestNG
    • Install Unity
    • Defect Management Process
    • Test Plan Template
    • Testing Interview Questions
    • Testing of Mobile application
    • What is Test Automation Frameworks
    • Application of Automation
    • What is Cucumber?
    • 15 Best Popular Bug Reporting Tools
    • What is Automated Testing?
    • Software Maintenance Types
    • Software Reliability
    • Best Gantt Chart Software
    • Code Coverage
    • Branch Coverage
    • Decision Coverage
    • Statement Coverage
    • What is Test Case
    • Types of Test Case
    • What is Test Scenario
    • Formal Review
    • Alpha Beta Pruning
    • What is Cyclomatic Complexity?
    • Test Coverage
    • How to Write Test Case
    • Testing Documentation
    • Performance Testing Life Cycle
    • What is Xpath
    • Test Harness
    • Test Strategy
    • Software Incident Management
    • What is Debugging
    • What is Defect?
    • Listeners in TestNG
  • Basics
    • What is Software Testing
    • Careers in Software Testing
    • Defect Life Cycle in Software Testing
    • Levels of Software Testing
    • Software Testing Life Cycle
    • Software Tester Work
    • Software Testing Principles
    • Testing Methodologies
    • Grey Box Testing
    • Types of Software Testing
    • What is a Bug in Software Testing
    • Benefits of Automation Testing
    • What is Automation Testing?
    • Types of Automation
    • Automation Testing Process
    • Mobile Automation Testing
    • Automation Testing Life Cycle
    • Software Quality Assurance
    • Software Quality Assurance
    • What is Test Environment?
    • Verification and Validation Testing
  • Types of Testing
    • Adhoc Testing
    • Agile Testing
    • What is Agile Testing
    • Alpha and Beta Testing
    • Application Testing
    • Automation Testing
    • Benchmark Testing
    • Black Box Testing
    • Domain Testing
    • Dynamic Testing
    • Ecommerce Testing
    • Fuzz Testing
    • Gray Box Testing
    • GUI Testing
    • Installation Testing
    • Interface Testing
    • Interoperability Testing
    • Mainframe Testing
    • Manual Testing
    • Mutation Testing
    • Monkey Testing
    • Negative Testing
    • Penetration Testing
    • Protocol Testing
    • Recovery Testing
    • Regression Testing
    • Accessibility Testing
    • Sanity Testing
    • Scalability Testing
    • Security Testing
    • Spike Testing
    • Stability Testing
    • State Transition Testing
    • Static Testing
    • Gatling Load Testing
    • System Integration Testing
    • Structural Testing
    • Locust Load Testing
    • System Testing
    • Control Flow Testing
    • Unit Testing
    • Volume Testing
    • Web Testing Application
    • What is Exploratory Testing
    • What is Stress Testing
    • What is Usability Testing
    • White Box Testing
    • Types of White Box Testing
    • Compatibility Testing 
    • Use Case Testing
    • Beta Testing
    • Integration Testing
    • Non Functional Testing
    • What is Functional Testing
    • Cookie Testing
    • Alpha Testing
    • Boundary Value Testing
    • Equivalence Class Testing
    • Glass Box Testing
    • SOA Testing
    • Smoke Testing
    • Visual Testing
    • Visual Paradigm
    • Model-Based Testing
  • Testing techniques
    • Software Testing Methodologies
    • Black Box Testing Techniques
    • Static Testing Techniques
    • Test Case Design Techniques
    • What is Static Analysis
  • Testing tools
    • Automation Testing Tools
    • Functional Testing Tools
    • GUI Testing Tools
    • Penetration Testing Tools
    • Performance Testing Tools
    • SOA Testing Tools
    • Accessibility Testing Tools
    • What is QTP
    • Regression Testing Tools
    • Security Testing Tools
    • Test Management Tools
    • Code Coverage Tools
    • Test Coverage Tools
    • Defect Tracking Tools
    • Continuous Integration Tools
    • Install Bugzilla
    • Test data generation tool
    • Unit Testing Tools
    • Web Testing Tools
    • Stress Testing Tools
    • Performance Monitoring Tools
    • Mobile Testing Tools
    • Responsive Testing Tool
    • Cross Browser Testing Tools
    • Risk Based Testing
    • Database Testing Tools
    • WinRunner
    • What is Squish?
    • CubicTest
    • What is WinRM?
    • Bugzilla Tool
  • Inteview Questions
    • Automation Testing Interview Questions
    • Manual Testing Interview Questions
    • ISTQB Interview Questions
    • Cucumber Interview Questions
    • Software Testing Interview Questions
    • Penetration Testing Interview Questions

Related Courses

Software Testing Course

Penetration Training Course

TestNG Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - All in One Software Development Bundle (600+ Courses, 50+ projects) Learn More