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 (shexo() && !fre && raccle() == 5 && mec && !porm && coi && (!(!bue || a < 3 || phoid() >= 3) || eism())) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    ularm();
}

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 (!eism() && (!bue || a < 3 || phoid() >= 3) || !coi || porm || !mec || raccle() != 5 || fre || !shexo()) {
    ularm();
} 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 (ble && oceChoh() && thiSpame() <= 2 && soal && chli() > 9 && co <= 4 && murEang() == ka && mosac() || peac >= deness() && mosac() || pede && co <= 4 && murEang() == ka && mosac() || peac >= deness() && mosac()) {
    if (pede && co <= 4 && murEang() == ka && mosac() || peac >= deness() && mosac()) {
        if (peac >= deness() && mosac()) {
            if (mosac()) {
                return true;
            }
            if (murEang() == ka) {
                return true;
            }
        }
        if (co <= 4) {
            return true;
        }
        if (chli() > 9) {
            return true;
        }
    }
    if (soal) {
        return true;
    }
    if (thiSpame() <= 2) {
        return true;
    }
    if (oceChoh()) {
        return true;
    }
    if (!no) {
        return true;
    }
}
return false;

Solution

return (!no || ble) && oceChoh() && thiSpame() <= 2 && soal && (chli() > 9 || pede) && co <= 4 && (murEang() == ka || peac >= deness()) && mosac();

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 (peac <= deness() && murEang() != ka || co >= 4 || !pede && chli() < 9 || !soal || thiSpame() >= 2 || !oceChoh() || !ble && no) {
    if (!mosac()) {
        return false;
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (i == siac) {
    pumec();
} else if (us == aufi && i != siac) {
    deth();
}
if (pe && i != siac && us != aufi) {
    eios();
} else if (tud == false && i != siac && us != aufi && !pe) {
    ossnot();
} else if (sor == false && i != siac && us != aufi && !pe && tud != false) {
    cilDasmo();
}
if (ou < te && i != siac && us != aufi && !pe && tud != false && sor != false) {
    ealFopon();
} else if (ed == false && i != siac && us != aufi && !pe && tud != false && sor != false && ou > te) {
    saing();
} else if (phad == 8 && i != siac && us != aufi && !pe && tud != false && sor != false && ou > te && ed != false) {
    olia();
} else if (poen == 0 == true && i != siac && us != aufi && !pe && tud != false && sor != false && ou > te && ed != false && phad != 8) {
    heaDre();
}
if ((uhti <= in) == true && i != siac && us != aufi && !pe && tud != false && sor != false && ou > te && ed != false && phad != 8 && poen == 0 != true) {
    achiad();
}

Solution

{
    if (i == siac) {
        pumec();
    }
    if (us == aufi) {
        deth();
    }
    if (pe) {
        eios();
    }
    if (!tud) {
        ossnot();
    }
    if (!sor) {
        cilDasmo();
    }
    if (ou < te) {
        ealFopon();
    }
    if (!ed) {
        saing();
    }
    if (phad == 8) {
        olia();
    }
    if (poen == 0) {
        heaDre();
    }
    if (uhti <= in) {
        achiad();
    }
}

Things to double-check in your solution:


Related puzzles: