Cyclomatic complexity measures how simple or complex your code is.
October 22, 2016

What Is Cyclomatic Complexity?

Coding Best Practices
Static Analysis

There are several metrics for software quality. And cyclomatic complexity is an important one. 

What Is Cyclomatic Complexity?

Cyclomatic complexity (CYC) is a metric for software quality. It was developed by Thomas J. McCabe Sr. in 1976. 

In its simplest form, CYC is a count of the number of decisions in the source code. The higher the count, the more complex the code. 

Why Is It Important?

It can be used in two ways, to:

  • Limit code complexity.
  • Determine the number of test cases required.

Cyclomatic complexity can be one of the most difficult code metrics to understand. And that makes it difficult to calculate.

How to Calculate Cyclomatic Complexity (McCabe)

Here's the formula for calculating cyclomatic complexity (CYC):

CYC = E – N + 2P

In this equation:

  • P = number of disconnected parts of the flow graph (e.g. a calling program and a subroutine)
  • E = number of edges (transfers of control)
  • N = number of nodes (sequential group of statements containing only one transfer of control)

This translates to the number of decisions + one.

Binary decisions — such as “if” and “while” statements — add one to complexity.

Boolean operators can add either one or nothing to complexity. For instance, one may be added if a Boolean operator is found within a conditional statement.

Two Cyclomatic Complexity Examples

Example 1

Here's an example of cyclomatic complexity.

void foo(void)
{
    if (a && b)
        x=1;
    else
        x=2;
}

At first, it looks like there is one decision in this example. So, CYC = 2.

However, when you consider the side effect of the Boolean operator, there are actually two decisions.

void foo(void)
{
    if (a)
        if (b) 
            x=1;
    else
        x=2;
 }

So, CYC = 3 in this example. 

Example 2

There are other variations of CYC, too.

Myers' Interval is an extension to CYC. It accounts for complexity caused by compound predicates. It uses CYC as its lower bound. The upper bound is defined as the total number of conditions in the code +1. It's presented as two values separated by:

CYCLOMATIC COMPLEXITY : NUMBER OF LOGICAL OPERATORS

Here's an example using Myers' Interval.

int divide (int x, int y)
{
    if (y != 0 /* Condition 1 */
    {
    return x / y;
    }
    else if (x == 0 && y > 2) /* Condition 2; Conditional expression 1 */
    {
        return 1;
    }
    else
    {
        printf (“div by zero\n”);
        return 0;
    } 

The example above has a STMCC value of 3:4. Its CYC is 3. And there is one connective (&&) used in the conditions. 

Lower Complexity = 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. 

Learn how to reduce the complexity of your code — and maintain a readable codebase.

More Software Quality Metrics