# What Is Cyclomatic Complexity?

Cyclomatic complexity is an important metric for software quality.

Here, we explain what is cyclomatic complexity, cyclomatic complexity McCabe, and provide cyclomatic complexity examples.

Read along or jump to the section that interests you the most:

**What Is Cyclomatic Complexity?****How to Calculate Cyclomatic Complexity McCabe?****Two Cyclomatic Complexity Examples**

## What Is Cyclomatic Complexity?

### Why Is Cyclomatic Complexity Important?

Cyclomatic complexity can be used in two ways, to:

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

However, it can be one of the most difficult software quality metrics to understand. And, that makes CYC difficult to calculate. That's why it is important to learn about software quality metrics — like cyclomatic complexity — and how to measure them accurately measure.

📕 Related White Paper: Which Software Quality Metrics Matter

## How to Calculate Cyclomatic Complexity McCabe?

Use the following formula to calculate 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 + 1.

Binary decisions — such as “if” and “while” statements — add 1 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

### Cyclomatic Complexity Example 1

Here's an example of CYC.

```
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.

### Cyclomatic Complexity 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 plus 1. It's presented as two values separated by:

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

However, complexity is just one measure of quality.

📕 Related White Paper: Guide to Software Quality and How to Measure It

## Lower Cyclomatic 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 — by using a static code analyzer such as Helix QAC.