LearnViz CompSci
Home / Learn / Software Engineering / Cyclomatic Complexity

Advanced Topic 42 of 50 Software Engineering

Cyclomatic Complexity

Count of independent paths through code.

How it works

Cyclomatic complexity, defined by Thomas McCabe, counts the number of linearly independent paths through a function's control-flow graph. It is computed as the number of decision points, such as if, loops, case labels and logical operators, plus one, or as edges minus nodes plus two. It indicates how many test cases are needed for path coverage and how hard the code is to understand. Values above about 10 are often considered a warning.

Where it is used

Linters and static analysis tools report it per function so that teams can find code to simplify and test thoroughly.

Common pitfall

High complexity correlates with defects, but the metric ignores nesting depth and data complexity and can be inflated by flat switch statements. Splitting a function merely to lower the number can hurt clarity. Use it as a signal to review, refactor with guard clauses, polymorphism or lookup tables, and make sure the complex code has tests.

Where it fits in the course

Cyclomatic Complexity is topic 42 of 50 in Software Engineering, rated Advanced. It follows Software Metrics. The next topic is Performance Profiling.

Practice and explore

How to study this topic

  1. Read the explanation above and restate the definition in your own words.
  2. Open the interactive lesson and answer its mini quiz.
  3. Read the common pitfall, then write a short example that avoids it.
  4. Check your understanding in the Software Engineering quiz, and revisit the topic if you miss a question.

Frequently asked questions

What is Cyclomatic Complexity?
Count of independent paths through code. It belongs to Software Engineering and is an advanced-level topic.
Where do people use Cyclomatic Complexity?
Linters and static analysis tools report it per function so that teams can find code to simplify and test thoroughly.
What is the most common mistake with Cyclomatic Complexity?
High complexity correlates with defects, but the metric ignores nesting depth and data complexity and can be inflated by flat switch statements.
What should I learn before this topic?
Start with Software Metrics, the topic just before it in Software Engineering.
How can I practice it?
Answer the mini quiz in the interactive lesson, then take the Software Engineering quiz. You can also explore Growth Rates (Big-O) in the Visual Lab.
Is Cyclomatic Complexity hard to learn?
It is an Advanced topic. Expect to read the explanation more than once and to connect it with earlier topics. Working through a small example by hand helps most.
How long does it take to learn Cyclomatic Complexity?
Most learners can read the explanation and finish the interactive lesson in under an hour. Fluency comes from applying the idea, so plan a few short practice sessions with the quiz.
Which topics are related to Cyclomatic Complexity?
Software Metrics, Performance Profiling, GraphQL are closely connected. The full list is in the related topics section of this page.
What should I study after Cyclomatic Complexity?
Continue with Performance Profiling, the next topic in Software Engineering.

Related topics