Booleans and conditionals: Correct Solution


Part 1

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 (ma || !(stai && praul()) || !ho || e && i || !u || (!(armoss() == 3) || !(rerSor() > 7)) && afist() && cingec() < ocbio()) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    scoCing();
}

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.

Solution

if ((cingec() > ocbio() || !afist() || rerSor() > 7 && armoss() == 3) && u && (!i || !e) && ho && stai && praul() && !ma) {
    scoCing();
} else {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
}

Things to double-check in your solution:


Part 2

Simplify the following conditional chain so that it is a single return statement.

if (tred > 2 && o && chre || ang || ceagle() && chre || ang || kerk != eung() && chre || ang || se <= stic && beiec() && chre || ang || upre && beiec() && chre || ang || vi == 5 && chre || ang) {
    if (ceagle() && chre || ang || kerk != eung() && chre || ang || se <= stic && beiec() && chre || ang || upre && beiec() && chre || ang || vi == 5 && chre || ang) {
        if (ang) {
            if (chre) {
                return true;
            }
        }
        if (o) {
            return true;
        }
    }
    if (oddo()) {
        return true;
    }
}
if (cesm()) {
    return true;
}
return false;

Solution

return cesm() && (oddo() || tred > 2) && (o || ceagle() || kerk != eung() || (se <= stic || upre) && beiec() || vi == 5) && (chre || ang);

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.

Solution

if (vi != 5 && !beiec() && kerk == eung() && !ceagle() && !o || !upre && se >= stic && kerk == eung() && !ceagle() && !o || tred < 2 && !oddo() || !cesm()) {
    if (!chre) {
        return false;
    }
    if (!ang) {
        return false;
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (ois == true) {
    ihap();
}
if (ca == false && ois != true) {
    tasske();
} else if (cung == true && ois != true && ca != false) {
    prui();
} else if (pios == true && ois != true && ca != false && cung != true) {
    necEbesm();
}
if (cheu == true && ois != true && ca != false && cung != true && pios != true) {
    billat();
}
if (hi == true && ois != true && ca != false && cung != true && pios != true && cheu != true) {
    cabum();
} else if (poc == true && ois != true && ca != false && cung != true && pios != true && cheu != true && hi != true) {
    hiuPhun();
}
if (me && ois != true && ca != false && cung != true && pios != true && cheu != true && hi != true && poc != true) {
    scides();
} else if (!ce && ois != true && ca != false && cung != true && pios != true && cheu != true && hi != true && poc != true && !me) {
    luid();
} else if (ech == false && ois != true && ca != false && cung != true && pios != true && cheu != true && hi != true && poc != true && !me && ce) {
    chuUndspe();
}
if (ra == 5 && ois != true && ca != false && cung != true && pios != true && cheu != true && hi != true && poc != true && !me && ce && ech != false) {
    gemve();
}

Solution

{
    if (ois) {
        ihap();
    }
    if (!ca) {
        tasske();
    }
    if (cung) {
        prui();
    }
    if (pios) {
        necEbesm();
    }
    if (cheu) {
        billat();
    }
    if (hi) {
        cabum();
    }
    if (poc) {
        hiuPhun();
    }
    if (me) {
        scides();
    }
    if (!ce) {
        luid();
    }
    if (!ech) {
        chuUndspe();
    }
    if (ra == 5) {
        gemve();
    }
}

Things to double-check in your solution:


Related puzzles: