Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. 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 comprehend.
A method's cognitive complexity is based on a few simple rules:
Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
Code is considered more complex for each "break in the linear flow of the code"
Code is considered more complex when "flow breaking structures are nested"
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. 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 comprehend.
A method's cognitive complexity is based on a few simple rules:
Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
Code is considered more complex for each "break in the linear flow of the code"
Code is considered more complex when "flow breaking structures are nested"
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. 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 comprehend.
A method's cognitive complexity is based on a few simple rules:
Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
Code is considered more complex for each "break in the linear flow of the code"
Code is considered more complex when "flow breaking structures are nested"
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. 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 comprehend.
A method's cognitive complexity is based on a few simple rules:
Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
Code is considered more complex for each "break in the linear flow of the code"
Code is considered more complex when "flow breaking structures are nested"
Jump statements (return, break, continue, and goto) and throw expressions move
control flow out of the current code block. Typically, any statements in a block that come after a jump or throw are simply wasted
keystrokes lying in wait to confuse the unwary.
Rarely, as illustrated below, code after a jump or throw is reachable. However, such code is difficult to understand, and should be
refactored.
Noncompliant Code Example
function fun($a) {
$i = 10;
return $i + $a;
$i++; // this is never executed
}
function foo($a) {
if ($a == 5) {
goto error;
} else {
// do the job
}
return;
error:
printf("don't use 5"); // this is reachable but unreadable
}
Compliant Solution
function fun($a) {
$i = 10;
return $i + $a;
}
function foo($a) {
if ($a == 5) {
handleError();
} else {
// do the job
}
return;
}
See
MISRA C:2004, 14.1 - There shall be no unreachable code
MISRA C++:2008, 0-1-1 - A project shall not contain unreachable code
MISRA C++:2008, 0-1-9 - There shall be no dead code
MISRA C:2012, 2.1 - A project shall not contain unreachable code