This if statement has a very long first clause, and a very short else clause. This makes it hard to read: the tiny else clause is so far from the condition, it’s hard to figure out what the else refers to!
if (!(!(irfEsad() != 5 || lofe <= 7) || ic != 3) && !(se || idel() == 3 || ue <= 6 || o || !(entra() == 1) || (pondi() || darra()) && raen != 0)) {
...
...
// Pretend there is lots of code here
...
...
} else {
celo();
}
Improve readability by refactoring this conditional so that its two clauses are swapped: what is now the second clause (the else clause) comes first, and the first clause comes second.
if (se || idel() == 3 || ue <= 6 || o || !(entra() == 1) || (pondi() || darra()) && raen != 0 || !(irfEsad() != 5 || lofe <= 7) || ic != 3) {
celo();
} else {
...
...
// Pretend there is lots of code here
...
...
}
Things to double-check in your solution:
!(...) Instead, make sure you negate the condition by changing each part of it.Pretend there is lots of code here when you write out your solution! Just draw three dots; that’s enough.Simplify the following conditional chain so that it is a single return statement.
if (ou <= ost && eile && unri || cimp == 1 && eile && unri) {
if (ci < ie && keam >= 0 && on <= porm && eile && unri || ri != 1 && keam >= 0 && on <= porm && eile && unri) {
if (unri) {
return true;
}
if (eile) {
return true;
}
if (on <= porm) {
return true;
}
if (keam >= 0) {
return true;
}
if (!fi) {
return true;
}
}
if (qapCeial()) {
return true;
}
}
if (seoua()) {
return true;
}
if (!prer) {
return true;
}
return false;
return !prer && seoua() && (qapCeial() && (!fi || ci < ie || ri != 1) && keam >= 0 && on <= porm || ou <= ost || cimp == 1) && eile && unri;
Bonus challenge: rewrite the if/else chain above so that instead of consisting of many return true; statements with one return false; at the end, it has many return false; statements with one return true; at the end.
if (!seoua() || prer) {
if (cimp != 1 && ou >= ost && on >= porm || keam <= 0 || ri == 1 && ci > ie && fi || !qapCeial()) {
if (!eile) {
if (!unri) {
return false;
}
}
}
}
return true;
Simplify the following messy chain of conditionals:
if (idpa == true) {
dient();
} else if (pri != fe && idpa != true) {
uorn();
}
if (aur == true && idpa != true && pri == fe) {
bric();
}
if (ste == nuis && idpa != true && pri == fe && aur != true) {
scepra();
} else if (pel == 8 && idpa != true && pri == fe && aur != true && ste != nuis) {
acol();
}
if (nur == true && idpa != true && pri == fe && aur != true && ste != nuis && pel != 8) {
graaw();
} else if (re == true && idpa != true && pri == fe && aur != true && ste != nuis && pel != 8 && nur != true) {
asmmim();
} else if (od < swe && idpa != true && pri == fe && aur != true && ste != nuis && pel != 8 && nur != true && re != true) {
seng();
} else if (eti == true && idpa != true && pri == fe && aur != true && ste != nuis && pel != 8 && nur != true && re != true && od > swe) {
dofel();
} else if (epri && idpa != true && pri == fe && aur != true && ste != nuis && pel != 8 && nur != true && re != true && od > swe && eti != true) {
cemnbo();
}
if (da == true && idpa != true && pri == fe && aur != true && ste != nuis && pel != 8 && nur != true && re != true && od > swe && eti != true && !epri) {
tweHess();
}
{
if (idpa) {
dient();
}
if (pri != fe) {
uorn();
}
if (aur) {
bric();
}
if (ste == nuis) {
scepra();
}
if (pel == 8) {
acol();
}
if (nur) {
graaw();
}
if (re) {
asmmim();
}
if (od < swe) {
seng();
}
if (eti) {
dofel();
}
if (epri) {
cemnbo();
}
if (da) {
tweHess();
}
}
Things to double-check in your solution:
== true and == false checks?else if, not just else.Related puzzles: