Qodana / Static Code Analysis Guide / What is Cyclomatic Complexity?

What Is Cyclomatic Complexity?

Cyclomatic complexity (CYC) is like a headcount of the “what if?” moments in your code; every loop, branch, and conditional adds to the tally. The more decision points, the higher the score, and the trickier the code is to understand, test, or modify.

Why care? Because overly complex code isn’t just hard to read, it’s harder to fix, riskier to change, and more likely to hide bugs in plain sight. Tracking CYC lets you spot those high-maintenance, error-prone areas before they derail your project.

The idea isn’t new. Back in 1976, Thomas J. McCabe Sr. came up with CYC as a way to measure code complexity and reliability. Today, it’s a staple in static code analysis, and Qodana calculates it automatically every time you run an inspection.

Why teams track CYC?

If you’re juggling multiple environments, languages, or contributors, simplicity is your best friend. Lower CYC means code is easier to understand, onboard with, and extend, whether you’re touching a fresh feature or deciphering legacy logic from 2014.

Instead of burning hours figuring out what your code was trying to do, you can spend that time shipping features, refactoring with purpose, or optimizing performance. A quick glance at your CYC score tells you which functions could use a little streamlining and which are good to go.

With Qodana running in your CI/CD pipeline, those complexity checks are continuous, so you’ll not end up finding out your code’s a labyrinth after it’s merged.

How is cyclomatic complexity measured?

Measuring and tracking code cyclomatic complexity is a proactive step that helps your team get ahead of potential problems before they come up. A good indication of quality code is a CYC score of somewhere between 1–10. When the complexity score is higher, it implies that the code needs to be refined. The generally accepted parameters are:

  • 1–10: This code is simply written and is easy to maintain. This is the ideal range for most functions.
  • 11–20: Code may need refactoring for better readability; this score is considered moderately complex.
  • 21+: Possibly difficult to maintain, unnecessarily complicated, and more susceptible to errors.
  • 50+: Considered untestable due to extreme complexity, making modifications or debugging complicated.

Understanding how CYC is calculated is important, and Qodana will automatically and dynamically measure this for you and update you as your project grows or changes. Qodana tracks and analyzes CYC because monitoring it is a good way to ensure cleaner code that’s easier for software developers to work with.

To gain a quantitative measure of CYC, you add one point for every linearly independent path. For example, if a piece of code is meant to have only one response to an if/when statement, there’s only one decision point in play. Code that has only one execution path would have a score of one. As the code becomes more layered, and more paths are added, you increase the score by one point per branch.

The formula used to calculate the CYC score is:

M = E - N + 2P

Where:

  • M = Cyclomatic Complexity
  • E = Number of edges (the connections between code blocks)
  • N = Number of nodes (decision points)
  • P = Number of connected components (applicable if the code is modularized)

Simply put, you count the number of execution paths through the code. The more paths there are, the higher the complexity score will be.

Let’s analyze a practical cyclomatic complexity example to better understand the concept.

Cyclomatic complexity: A simple example

To truly grasp CYC, let’s walk through a simple example. This metric measures the number of independent paths through your code, essentially how many different routes your program can take during execution. Lower scores are a sign of clean, easy-to-understand code that’s simple to test and maintain. Higher scores? They’re a red flag that your code might be more tangled than necessary, making future updates and testing trickier.

Take a look at this basic function. It checks if a number is positive and, if so, prints a message. Because there’s only one decision point here, this function has a CYC score of 1. That means it’s straightforward, easy to maintain, and unlikely to cause headaches down the line.

This function is a great example of simplicity and reliability, with just one clear path through the code, no branches, and no extra conditions.

def is_positive(num):
    if num > 0:
        print("Positive number")

Then, let’s look at a somewhat more complex function containing several conditions. As can be seen, this function has new decision points when compared to the previous example.

Each of these decision points increases the code complexity and raises the CYC score. Firstly, the if statement introduces one decision point, and the elif statement then adds another decision point, which increases the overall complexity. The else clause creates a third branch, making the function even more layered:

def check_number(num):
    if num > 0:
        print("Positive")
    elif num < 0:
        print("Negative")
    else:
        print("Zero")

This means that the code has four execution paths and a CYC score of 4. This score predicts that the code will not be much more complicated than the first example to maintain, and should still be easy to manage. It is still within the ideal range.

When a CYC score is anywhere above 10, the code becomes harder to understand, test, and change. As a rule, developers aim to keep their code complexity as low as possible, as complicated code makes it much more challenging for future developers or teams to update.

By tracking CYC, developers can maintain clean, well-structured, reliable and scalable code.

How Qodana can help you reduce code complexity?

Cyclomatic complexity is just one piece of the code quality puzzle, and Qodana helps you see the whole picture.

By automatically measuring complexity alongside other critical metrics, Qodana empowers your team to spot risky code early, refactor with confidence, and maintain a cleaner, more resilient codebase over time.

Plus, you can use the Insights Dashboard for a quick view into how healthy your projects are.

Whether you’re integrating into your CI/CD pipeline or running targeted checks inside JetBrains IDEs, Qodana keeps code quality and maintainability at the center of your workflow, so complexity never catches you off guard.

Start using Qodana today and make clean, maintainable code your default.

cross project code analysis