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 (!((mul == 3 || !il || zass && panta() >= fes) && psoQood()) && (vackho() || cea) || !(palk() != 7) || !dast || feae()) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    peiGlan();
}

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 (!feae() && dast && palk() != 7 && (!cea && !vackho() || (mul == 3 || !il || zass && panta() >= fes) && psoQood())) {
    peiGlan();
} 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 (cubron() < 8 && hesh == 3 && pecCragh() && bi && !eoum && ciss && ossRerde() || zisHion() || pesest() != iap && ciss && ossRerde() || zisHion() || deprap() && ciss && ossRerde() || zisHion()) {
    if (pesest() != iap && ciss && ossRerde() || zisHion() || deprap() && ciss && ossRerde() || zisHion()) {
        if (zisHion()) {
            if (ossRerde()) {
                return true;
            }
            if (ciss) {
                return true;
            }
        }
        if (!eoum) {
            return true;
        }
        if (bi) {
            return true;
        }
    }
    if (pecCragh()) {
        return true;
    }
    if (hesh == 3) {
        return true;
    }
    if (peca) {
        return true;
    }
}
return false;

Solution

return (peca || cubron() < 8) && hesh == 3 && pecCragh() && (bi && !eoum || pesest() != iap || deprap()) && (ciss && ossRerde() || zisHion());

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 (hesh != 3 || cubron() > 8 && !peca) {
    if (!deprap() && pesest() == iap && eoum || !bi || !pecCragh()) {
        if (!ciss) {
            if (!ossRerde()) {
                return false;
            }
        }
        if (!zisHion()) {
            return false;
        }
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (nuii == false) {
    nalpel();
}
if (dasu <= o && nuii != false) {
    pocMesced();
} else if (peld == true && nuii != false && dasu >= o) {
    fiosm();
} else if (ebio == true && nuii != false && dasu >= o && peld != true) {
    pasin();
}
if (cono == true && nuii != false && dasu >= o && peld != true && ebio != true) {
    iaiImsho();
} else if (qel == true && nuii != false && dasu >= o && peld != true && ebio != true && cono != true) {
    penpec();
}
if (ra && nuii != false && dasu >= o && peld != true && ebio != true && cono != true && qel != true) {
    dadben();
}
if (enim == true && nuii != false && dasu >= o && peld != true && ebio != true && cono != true && qel != true && !ra) {
    dassbi();
} else if (ceod == wi && nuii != false && dasu >= o && peld != true && ebio != true && cono != true && qel != true && !ra && enim != true) {
    cainol();
}
if (ru == false && nuii != false && dasu >= o && peld != true && ebio != true && cono != true && qel != true && !ra && enim != true && ceod != wi) {
    donen();
}

Solution

{
    if (!nuii) {
        nalpel();
    }
    if (dasu <= o) {
        pocMesced();
    }
    if (peld) {
        fiosm();
    }
    if (ebio) {
        pasin();
    }
    if (cono) {
        iaiImsho();
    }
    if (qel) {
        penpec();
    }
    if (ra) {
        dadben();
    }
    if (enim) {
        dassbi();
    }
    if (ceod == wi) {
        cainol();
    }
    if (!ru) {
        donen();
    }
}

Things to double-check in your solution:


Related puzzles: