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 (hurUss() && (tes || un || u && memec() && !gerco() || pilcri() == 1) || ta > 9 && !at && fe != 1 || !pe) {
...
...
// Pretend there is lots of code here
...
...
} else {
nastma();
}
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 (pe && (fe == 1 || at || ta < 9) && (pilcri() != 1 && (gerco() || !memec() || !u) && !un && !tes || !hurUss())) {
nastma();
} 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 (cinScehin() && e && a || !ma && epro() || iangbo() && a || !ma && epro()) {
if (ad && a || !ma && epro()) {
if (bemest() > 0 && a || !ma && epro()) {
if (teawu() && a || !ma && epro() || ra != 7 && a || !ma && epro()) {
if (!ma && epro()) {
if (a) {
return true;
}
}
if (stis() > 3) {
return true;
}
if (phee) {
return true;
}
}
}
}
}
return false;
return (phee && stis() > 3 || teawu() || ra != 7 || bemest() > 0 || ad || cinScehin() && (e || iangbo())) && (a || !ma && epro());
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 (!iangbo() && !e && !ad && bemest() < 0 && ra == 7 && !teawu() && stis() < 3 || !phee || !cinScehin() && !ad && bemest() < 0 && ra == 7 && !teawu() && stis() < 3 || !phee) {
if (ma && !a) {
if (!a) {
return false;
}
if (!epro()) {
return false;
}
}
}
return true;
Simplify the following messy chain of conditionals:
if (trel) {
tion();
}
if (cec == true && !trel) {
feng();
} else if (er == false && !trel && cec != true) {
dedSchad();
} else if (el && !trel && cec != true && er != false) {
meswas();
}
if (kapi == false && !trel && cec != true && er != false && !el) {
criHorbe();
} else if (scas == true && !trel && cec != true && er != false && !el && kapi != false) {
elew();
}
if (zul == i && !trel && cec != true && er != false && !el && kapi != false && scas != true) {
sesOthest();
} else if (bir > 4 && !trel && cec != true && er != false && !el && kapi != false && scas != true && zul != i) {
tric();
}
if (uied == false && !trel && cec != true && er != false && !el && kapi != false && scas != true && zul != i && bir < 4) {
olclu();
} else if (si == true && !trel && cec != true && er != false && !el && kapi != false && scas != true && zul != i && bir < 4 && uied != false) {
lumsi();
}
if (ac && !trel && cec != true && er != false && !el && kapi != false && scas != true && zul != i && bir < 4 && uied != false && si != true) {
rhiax();
}
{
if (trel) {
tion();
}
if (cec) {
feng();
}
if (!er) {
dedSchad();
}
if (el) {
meswas();
}
if (!kapi) {
criHorbe();
}
if (scas) {
elew();
}
if (zul == i) {
sesOthest();
}
if (bir > 4) {
tric();
}
if (!uied) {
olclu();
}
if (si) {
lumsi();
}
if (ac) {
rhiax();
}
}
Things to double-check in your solution:
== true and == false checks?else if, not just else.Related puzzles: