What is the difference between Cyclomatic Complexity and Cognitive Complexity ?

Gilles Fabre
3 min readFeb 14, 2022

Introduction

You probably heard about the Cyclomatic Complexity, but did you hear about the Cognitive Complexity ? What are the differences between them and which one should you use ? Let’s examine this more closely.

Cyclomatic Complexity

The cyclomatic complexity is a well-known software metric which was defined by Thomas MacCabe in 1976. It represents the number of linear independent paths (or branches) of a given source code*.

Most of the developers are using this metric to measure the complexity of a program, but what exactly means the word “complexity” ? Intuitively, we think about something like “the difficulty to understand a program”. Unfortunately, the cyclomatic complexity is absolutely not a good tool to measure that.

Remember, the cyclomatic complexity is simply the number of forks of a code snippet (the number of if/else, switch cases, etc.). Consequently, a function without forks has a cyclomatic complexity equal to… zero ! Even if this function is extremely difficult to understand, its cyclomatic complexity is always equal to zero if there is no fork inside it.

Even junior developers know that most of the “complexity” of a code snippet is not relative to its number of if / else. Each line of the code may be difficult to understand, independently of its inclusion in a given path.

So, in which cases should you use cyclomatic complexity ? Indeed, this metric is useful to measure the testability of the code. A function which has a huge number of branches will be difficult to test, and you should split it into shorter functions.

In my opinion, the only case where the cyclomatic complexity is really useful is when you want to increase the testability of your code. If your goal is to measure its difficulty of comprehension, you should use the cognitive complexity.

Cognitive Complexity

The cognitive complexity is a metric which measures the difficulty of comprehension of a given program. This metric is highly correlated with the maintainability of the code: if your program is simple to understand, the future developers which will read it will be able to debug it easily and to add new functionalities without too many problems.

The cost of the maintenance of a software is usually much higher than its cost of development. That’s why it is so important to write clean code and respect the best practices of development.

The aim of the cognitive complexity is to take care about all the elements which could increase the difficulty of comprehension of the code, including its readability, its structure, its length, and of course its algorithm. The number of branches is only a small part of the cognitive complexity score.

As far as I know, the only open-source tool dedicated to the cognitive complexity is Genese Cpx (@genese/complexity )**. With only one command-line, you have a precise overview of the cognitive complexity of all your project. You even may review the cognitive complexity of each line of your code, which is very helpful when you need to refactor it.

@genese/complexity dashboard
@genese/complexity code analysis

@genese/complexity is, in my opinion, the best tool for companies which want to be sure that the code delivered by their providers will be easy to maintain. It may also be a good tool for you, as developer, when you want to be sure that your code will be easily maintainable.

Remarks:

* By convention, a function having only one path (no if, no switch, etc.) has a cyclomatic complexity equal to zero.

** The well-known tool SonarQube© is also giving a score called “cognitive complexity”, but their algorithm only takes care of a few factors which represent a very small part of the elements increasing the difficulty of comprehension of the code. For more details about the SonarQube©’s algorithm, you may read my previous article: “What is the Cognitive Complexity and how to use it ?”

--

--

Gilles Fabre

TypeScript expert. Specialization in static code analysis and in software complexity