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 (ni && ac >= rared() && kna <= be || !o && misecs() && !odbes() && honu() || ocs && azoss() && (tes != 5 || i)) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    doun();
}

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 ((!i && tes == 5 || !azoss() || !ocs) && (!honu() || odbes() || !misecs() || o) && (kna >= be || ac <= rared() || !ni)) {
    doun();
} 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 (thonwo() && co) {
    if (deen() && peung() && cint || cioo || elgi() || vea) {
        if (vea) {
            if (elgi()) {
                if (cioo) {
                    if (cint) {
                        return true;
                    }
                }
            }
        }
        if (snep()) {
            return true;
        }
        if (te) {
            return true;
        }
        if (ed) {
            return true;
        }
    }
    if (miesot()) {
        return true;
    }
}
return false;

Solution

return miesot() && (ed && te && snep() || deen() && peung()) && (cint || cioo || elgi() || vea) || thonwo() && co;

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 (!thonwo() && !vea && !elgi() && !cioo && !cint || !peung() && !snep() || !te || !ed || !deen() && !snep() || !te || !ed || !miesot()) {
    if (!peung() && !snep() || !te || !ed || !deen() && !snep() || !te || !ed || !miesot()) {
        if (!cint) {
            return false;
        }
        if (!cioo) {
            return false;
        }
        if (!elgi()) {
            return false;
        }
        if (!vea) {
            return false;
        }
    }
    if (!co) {
        return false;
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (a >= pe) {
    anir();
} else if (!ro && a <= pe) {
    chejor();
}
if (!iuci && a <= pe && ro) {
    nerTingse();
} else if (di == true && a <= pe && ro && iuci) {
    couUrprem();
}
if (!mi && a <= pe && ro && iuci && di != true) {
    nade();
}
if (siod < 7 && a <= pe && ro && iuci && di != true && mi) {
    nibi();
} else if (riol == true && a <= pe && ro && iuci && di != true && mi && siod > 7) {
    oicNiosir();
}
if (au == true && a <= pe && ro && iuci && di != true && mi && siod > 7 && riol != true) {
    nangbo();
} else if (!pra && a <= pe && ro && iuci && di != true && mi && siod > 7 && riol != true && au != true) {
    esmad();
} else if (ca && a <= pe && ro && iuci && di != true && mi && siod > 7 && riol != true && au != true && pra) {
    sunuss();
} else if (ple == false && a <= pe && ro && iuci && di != true && mi && siod > 7 && riol != true && au != true && pra && !ca) {
    heepoc();
}

Solution

{
    if (a >= pe) {
        anir();
    }
    if (!ro) {
        chejor();
    }
    if (!iuci) {
        nerTingse();
    }
    if (di) {
        couUrprem();
    }
    if (!mi) {
        nade();
    }
    if (siod < 7) {
        nibi();
    }
    if (riol) {
        oicNiosir();
    }
    if (au) {
        nangbo();
    }
    if (!pra) {
        esmad();
    }
    if (ca) {
        sunuss();
    }
    if (!ple) {
        heepoc();
    }
}

Things to double-check in your solution:


Related puzzles: