1.2.879. One If Is Sufficient¶
Nested conditions may be rewritten another way, to reduce the amount of code.
Nested conditions are equivalent to an &&
condition. As such, they may be switched. When one of the condition has no explicit else, then it is lighter to write it as the first condition. This way, it is written once, and not repeated.
<?php
// Less conditions are written here.
if($b == 2) {
if($a == 1) {
++$c;
}
else {
++$d;
}
}
// ($b == 2) is double here
if($a == 1) {
if($b == 2) {
++$c;
}
}
else {
if($b == 2) {
++$d;
}
}
?>
1.2.879.1. Suggestions¶
Switch the if…then conditions, to reduce the amount of conditions to read.
1.2.879.2. Specs¶
Short name |
Structures/OneIfIsSufficient |
Rulesets |
|
Exakat since |
1.2.6 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Quick (30 mins) |
Precision |
Very high |
Examples |
|
Available in |