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 (!meus && pabDotoc() || !(ha || isol || iapros()) || !knor) {
...
...
// Pretend there is lots of code here
...
...
} else {
trism();
}
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 (knor && (ha || isol || iapros()) && (!pabDotoc() || meus)) {
trism();
} 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 (honad() && gense() && !ir) {
if (vosi() <= 1 && gense() && !ir) {
if (!ir) {
return true;
}
if (gense()) {
return true;
}
if (gonto() == ci) {
return true;
}
}
}
if (melpa()) {
return true;
}
if (hou) {
return true;
}
return false;
return hou && melpa() && (gonto() == ci || vosi() <= 1 || honad()) && gense() && !ir;
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 (!honad() && vosi() >= 1 && gonto() != ci || !melpa() || !hou) {
if (!gense()) {
if (ir) {
return false;
}
}
}
return true;
Simplify the following messy chain of conditionals:
if (mosm <= 7) {
ongAspri();
}
if (nen == true && mosm >= 7) {
phomi();
} else if (in && mosm >= 7 && nen != true) {
astpec();
}
if (fran == true && mosm >= 7 && nen != true && !in) {
praBieor();
}
if (haun == true && mosm >= 7 && nen != true && !in && fran != true) {
maudre();
} else if (ili <= edan && mosm >= 7 && nen != true && !in && fran != true && haun != true) {
wesip();
}
{
if (mosm <= 7) {
ongAspri();
}
if (nen) {
phomi();
}
if (in) {
astpec();
}
if (fran) {
praBieor();
}
if (haun) {
maudre();
}
if (ili <= edan) {
wesip();
}
}
Things to double-check in your solution:
== true and == false checks?else if, not just else.Related puzzles: