async vs parallel processing :👈 👉:What are PDBs

Cyclomatic Complexity

What is Cyclomatic Complexity?

Cyclomatic Complexity is a software metric that measures the number of independent execution paths through a program's source code.

It was introduced by Thomas McCabe and helps quantify how complex a piece of code is.

Formula:

Cyclomatic Complexity = Number of Decision Points + 1

Decision points include:

  • if
  • else if
  • for
  • while
  • do while
  • case statements
  • catch
  • Conditional operators (?:)
  • Logical branches (&&, ||) in some analysis tools

Example 1: Simple Method

public void Process()
{
    Console.WriteLine("Hello");
}

No decision points:

Complexity = 1

There is only one execution path.


Example 2: One If Statement

public void Process(int age)
{
    if (age >= 18)
    {
        Console.WriteLine("Adult");
    }

    Console.WriteLine("Done");
}

One decision point:

Complexity = 2

Paths:

  1. Condition is true
  2. Condition is false

Example 3: Multiple Conditions

public string GetGrade(int marks)
{
    if (marks >= 90)
        return "A";
    else if (marks >= 75)
        return "B";
    else
        return "C";
}

Decision points:

  • if
  • else if
Complexity = 3

Independent paths:

  1. Grade A
  2. Grade B
  3. Grade C

Why is Cyclomatic Complexity Important?

1. Indicates Code Maintainability

Higher complexity generally means:

  • Harder to understand
  • Harder to modify
  • Greater chance of introducing bugs

Example:

if (...)
{
   ...
}
else if (...)
{
   ...
}
else if (...)
{
   ...
}
else if (...)
{
   ...
}

The more branches, the more difficult the code becomes to maintain.


2. Helps Estimate Testing Effort

Cyclomatic Complexity represents the minimum number of test cases needed to achieve full branch coverage.

Example:

Complexity = 5

At least 5 independent test cases are needed to exercise every path.


3. Identifies Refactoring Candidates

Very high complexity may indicate that a method should be split into smaller methods.

Example:

Complexity = 25

This is often considered a warning sign and may suggest the method violates the Single Responsibility Principle.


4. Improves Code Quality

Static analysis tools such as:

  • SonarQube
  • ReSharper
  • Visual Studio Code Analysis

often flag methods with excessive complexity because they are more error-prone.


Typical Complexity Ratings

Complexity Risk Level
1-10 Low risk, easy to maintain
11-20 Moderate complexity
21-50 High complexity, harder to test
>50 Very high risk, should be refactored

Interview Answer

Cyclomatic Complexity is a software metric that measures the number of independent execution paths through a program. It is calculated based on the number of decision points such as if, switch, and loops. It is important because it indicates code complexity, helps estimate the number of test cases required for full path coverage, identifies code that may need refactoring, and serves as a predictor of maintainability and defect risk.

Back to Index
async vs parallel processing :👈 👉:What are PDBs