Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

What's the difference between cyclomatic complexity and cognitive complexity?


Asked by Dario Petersen on Dec 13, 2021 FAQ



Both of these code snippets have a cyclomatic complexity of 4 whilst this code is clearly much harder to understand. Cyclomatic complexity is simply bound to the number of lines of code and the number of control structures (if, while, switch) used.
Subsequently,
Cyclomatic complexity is a static analysis measure of how difficult is code to test. Cognitive complexity tells us, how difficult code is to understand by a reader. Today, we'll see why is the later better and how to check it in your code with a Sniff.
Also, Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and understand. Now, Code Climate can help you identify which methods are overly difficult to understand and prevent introducing them into your code.
Besides,
In psychology, cognitive complexity is how well people perceive things. It has to do with how a person looks at events, structures, or experiences and how correctly an individual analyzes based on how complex their cognition, or thinking, structures have become. How can cyclomatic complexity be reduced?
In addition,
Complexity in programming and computer science has a few meanings. Most programmers may be familiar with Cyclomatic Complexity which attempts to tell us how difficult a method will be to test or maintain. It does this by creating a score for every branch in a method. Like so: