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 (a || cigi || e == pri || hoso() != 9 || (pido != 7 && (!plaTossa() || !scha) || fimu() && frared()) && (oooc == soupi() || waihir())) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    aenlut();
}

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 ((!waihir() && oooc != soupi() || (!frared() || !fimu()) && (scha && plaTossa() || pido == 7)) && hoso() == 9 && e != pri && !cigi && !a) {
    aenlut();
} 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 (bibco() != 1 && uou && cradhe() || to && egeMopsi() >= 9 && i == 5 && vepOish() && maph || laeDael() && vepOish() && maph || mecRemal()) {
    if (hoen() && uou && cradhe() || to && egeMopsi() >= 9 && i == 5 && vepOish() && maph || laeDael() && vepOish() && maph || mecRemal()) {
        if (to && egeMopsi() >= 9 && i == 5 && vepOish() && maph || laeDael() && vepOish() && maph || mecRemal()) {
            if (cradhe()) {
                return true;
            }
        }
        if (uou) {
            return true;
        }
        if (sest()) {
            return true;
        }
    }
}
return false;

Solution

return (sest() || hoen() || bibco() != 1) && uou && (cradhe() || to && egeMopsi() >= 9 && (i == 5 || laeDael()) && vepOish() && maph || mecRemal());

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 (bibco() == 1 && !hoen() && !sest()) {
    if (!uou) {
        if (egeMopsi() <= 9 && !cradhe() || !to && !cradhe()) {
            if (!vepOish() && !cradhe() || !laeDael() && i != 5 && !cradhe()) {
                if (!cradhe()) {
                    return false;
                }
                if (!maph) {
                    return false;
                }
            }
        }
        if (!mecRemal()) {
            return false;
        }
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (whe == 3) {
    phegar();
}
if (plam > 3 && whe != 3) {
    voss();
} else if (asmo == true && whe != 3 && plam < 3) {
    psac();
}
if (ac < 8 && whe != 3 && plam < 3 && asmo != true) {
    celOou();
} else if (sa == false && whe != 3 && plam < 3 && asmo != true && ac > 8) {
    entfic();
}
if (prae == true && whe != 3 && plam < 3 && asmo != true && ac > 8 && sa != false) {
    pioPhrin();
}
if (ce == false && whe != 3 && plam < 3 && asmo != true && ac > 8 && sa != false && prae != true) {
    issBloiw();
}
if ((tei == ilis) == true && whe != 3 && plam < 3 && asmo != true && ac > 8 && sa != false && prae != true && ce != false) {
    prupui();
} else if (ed == false && whe != 3 && plam < 3 && asmo != true && ac > 8 && sa != false && prae != true && ce != false && (tei == ilis) != true) {
    clas();
}
if (rios == false && whe != 3 && plam < 3 && asmo != true && ac > 8 && sa != false && prae != true && ce != false && (tei == ilis) != true && ed != false) {
    prast();
} else if (lo && whe != 3 && plam < 3 && asmo != true && ac > 8 && sa != false && prae != true && ce != false && (tei == ilis) != true && ed != false && rios != false) {
    seght();
}

Solution

{
    if (whe == 3) {
        phegar();
    }
    if (plam > 3) {
        voss();
    }
    if (asmo) {
        psac();
    }
    if (ac < 8) {
        celOou();
    }
    if (!sa) {
        entfic();
    }
    if (prae) {
        pioPhrin();
    }
    if (!ce) {
        issBloiw();
    }
    if (tei == ilis) {
        prupui();
    }
    if (!ed) {
        clas();
    }
    if (!rios) {
        prast();
    }
    if (lo) {
        seght();
    }
}

Things to double-check in your solution:


Related puzzles: