Cognitive Complexity is a measure of how hard the control flow of a function is to understand. Functions with high Cognitive Complexity will be
difficult to maintain.
Most of the time a block of code is empty when a piece of code is really missing. So such empty block must be either filled or removed.
Noncompliant Code Example
for ($i = 0; $i < 42; $i++){} // Empty on purpose or missing piece of code ?
Exceptions
When a block contains a comment, this block is not considered to be empty.
Having too many return statements in a function increases the function's essential complexity because the flow of execution is broken each time a
return statement is encountered. This makes it harder to read and understand the logic of the function.
Noncompliant Code Example
With the default threshold of 3:
function myFunction(){ // Noncompliant as there are 4 return statements
if (condition1) {
return true;
} else {
if (condition2) {
return false;
} else {
return true;
}
}
return false;
}
Cognitive Complexity is a measure of how hard the control flow of a function is to understand. Functions with high Cognitive Complexity will be
difficult to maintain.
A class that grows too much tends to aggregate too many responsibilities and inevitably becomes harder to understand and therefore to maintain.
Above a specific threshold, it is strongly advised to refactor the class into smaller ones which focus on well defined topics.
Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.
On the other hand, constants can be referenced from many places, but only need to be updated in a single place.
Noncompliant Code Example
With the default threshold of 3:
function run() {
prepare('action1'); // Non-Compliant - 'action1' is duplicated 3 times
execute('action1');
release('action1');
}
Compliant Solution
ACTION_1 = 'action1';
function run() {
prepare(ACTION_1);
execute(ACTION_1);
release(ACTION_1);
}
Exceptions
To prevent generating some false-positives, literals having less than 5 characters are excluded.
Unused parameters are misleading. Whatever the value passed to such parameters is, the behavior will be the same.
Noncompliant Code Example
function doSomething($a, $b) { // "$a" is unused
return compute($b);
}
Compliant Solution
function doSomething($b) {
return compute($b);
}
Exceptions
Functions in classes that override a class or implement interfaces are ignored.
class C extends B {
function doSomething($a, $b) { // no issue reported on $b
compute($a);
}
}
See
MISRA C++:2008, 0-1-11 - There shall be no unused parameters (named or unnamed) in nonvirtual functions.
MISRA C:2012, 2.7 - There should be no unused parameters in functions
CERT, MSC12-C. - Detect and remove code that has no effect or is never
executed
Cognitive Complexity is a measure of how hard the control flow of a function is to understand. Functions with high Cognitive Complexity will be
difficult to maintain.
Cognitive Complexity is a measure of how hard the control flow of a function is to understand. Functions with high Cognitive Complexity will be
difficult to maintain.
Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.
On the other hand, constants can be referenced from many places, but only need to be updated in a single place.
Noncompliant Code Example
With the default threshold of 3:
function run() {
prepare('action1'); // Non-Compliant - 'action1' is duplicated 3 times
execute('action1');
release('action1');
}
Compliant Solution
ACTION_1 = 'action1';
function run() {
prepare(ACTION_1);
execute(ACTION_1);
release(ACTION_1);
}
Exceptions
To prevent generating some false-positives, literals having less than 5 characters are excluded.
Having too many return statements in a function increases the function's essential complexity because the flow of execution is broken each time a
return statement is encountered. This makes it harder to read and understand the logic of the function.
Noncompliant Code Example
With the default threshold of 3:
function myFunction(){ // Noncompliant as there are 4 return statements
if (condition1) {
return true;
} else {
if (condition2) {
return false;
} else {
return true;
}
}
return false;
}
Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.
On the other hand, constants can be referenced from many places, but only need to be updated in a single place.
Noncompliant Code Example
With the default threshold of 3:
function run() {
prepare('action1'); // Non-Compliant - 'action1' is duplicated 3 times
execute('action1');
release('action1');
}
Compliant Solution
ACTION_1 = 'action1';
function run() {
prepare(ACTION_1);
execute(ACTION_1);
release(ACTION_1);
}
Exceptions
To prevent generating some false-positives, literals having less than 5 characters are excluded.
Cognitive Complexity is a measure of how hard the control flow of a function is to understand. Functions with high Cognitive Complexity will be
difficult to maintain.
A class that grows too much tends to aggregate too many responsibilities and inevitably becomes harder to understand and therefore to maintain.
Above a specific threshold, it is strongly advised to refactor the class into smaller ones which focus on well defined topics.
Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.
On the other hand, constants can be referenced from many places, but only need to be updated in a single place.
Noncompliant Code Example
With the default threshold of 3:
function run() {
prepare('action1'); // Non-Compliant - 'action1' is duplicated 3 times
execute('action1');
release('action1');
}
Compliant Solution
ACTION_1 = 'action1';
function run() {
prepare(ACTION_1);
execute(ACTION_1);
release(ACTION_1);
}
Exceptions
To prevent generating some false-positives, literals having less than 5 characters are excluded.
Having too many return statements in a function increases the function's essential complexity because the flow of execution is broken each time a
return statement is encountered. This makes it harder to read and understand the logic of the function.
Noncompliant Code Example
With the default threshold of 3:
function myFunction(){ // Noncompliant as there are 4 return statements
if (condition1) {
return true;
} else {
if (condition2) {
return false;
} else {
return true;
}
}
return false;
}
Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.
On the other hand, constants can be referenced from many places, but only need to be updated in a single place.
Noncompliant Code Example
With the default threshold of 3:
function run() {
prepare('action1'); // Non-Compliant - 'action1' is duplicated 3 times
execute('action1');
release('action1');
}
Compliant Solution
ACTION_1 = 'action1';
function run() {
prepare(ACTION_1);
execute(ACTION_1);
release(ACTION_1);
}
Exceptions
To prevent generating some false-positives, literals having less than 5 characters are excluded.