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 (ecan() || ba > 8 || !trou && (!rirest() && ar || !(buo || ninHec() > noip())) && (es || gact)) {
...
...
// Pretend there is lots of code here
...
...
} else {
pacce();
}
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 ((!gact && !es || (buo || ninHec() > noip()) && (!ar || rirest()) || trou) && ba < 8 && !ecan()) {
pacce();
} 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 (hebrod() && sumdep() && oncu && esm && nixint() || ho && nixint() || xedpo() && beph) {
if (xedpo() && beph) {
if (ho && nixint()) {
if (nixint()) {
return true;
}
if (esm) {
return true;
}
}
if (oncu) {
return true;
}
}
if (sumdep()) {
return true;
}
if (si) {
return true;
}
}
if (!ic) {
return true;
}
return false;
return !ic && (si || hebrod()) && sumdep() && (oncu && (esm || ho) && nixint() || xedpo() && beph);
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 (!sumdep() || !hebrod() && !si || ic) {
if (!xedpo() && !nixint() || !ho && !esm || !oncu) {
if (!ho && !esm || !oncu) {
if (!nixint()) {
return false;
}
}
if (!beph) {
return false;
}
}
}
return true;
Simplify the following messy chain of conditionals:
if (okmo == true) {
hunggo();
} else if (!tiir && okmo != true) {
cang();
} else if (gass == true && okmo != true && tiir) {
anse();
} else if (ab == false && okmo != true && tiir && gass != true) {
oene();
} else if (chra == true && okmo != true && tiir && gass != true && ab != false) {
gleUstha();
}
if (na == false && okmo != true && tiir && gass != true && ab != false && chra != true) {
iheThaid();
} else if (fo == false && okmo != true && tiir && gass != true && ab != false && chra != true && na != false) {
proes();
}
if (aeng && okmo != true && tiir && gass != true && ab != false && chra != true && na != false && fo != false) {
eren();
} else if (ta == false && okmo != true && tiir && gass != true && ab != false && chra != true && na != false && fo != false && !aeng) {
ionwi();
}
{
if (okmo) {
hunggo();
}
if (!tiir) {
cang();
}
if (gass) {
anse();
}
if (!ab) {
oene();
}
if (chra) {
gleUstha();
}
if (!na) {
iheThaid();
}
if (!fo) {
proes();
}
if (aeng) {
eren();
}
if (!ta) {
ionwi();
}
}
Things to double-check in your solution:
== true and == false checks?else if, not just else.Related puzzles: