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 (chlo && ecri() && hidtel() || rir != re || snes() != kesm || pren || ali != 0 && stutgu()) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    hedpil();
}

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 ((!stutgu() || ali == 0) && !pren && snes() == kesm && rir == re && (!hidtel() || !ecri() || !chlo)) {
    hedpil();
} 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 (pabi() != clant() && !fidi && depre() != 3 && da < dini && oiaKoice() == niop && mioa()) {
    if (mioa()) {
        return true;
    }
    if (oiaKoice() == niop) {
        return true;
    }
    if (da < dini) {
        return true;
    }
    if (nang()) {
        return true;
    }
    if (a) {
        return true;
    }
    if (icass()) {
        return true;
    }
}
return false;

Solution

return (icass() && a && nang() || pabi() != clant() && !fidi && depre() != 3) && da < dini && oiaKoice() == niop && mioa();

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 (depre() == 3 && !nang() || !a || !icass() || fidi && !nang() || !a || !icass() || pabi() == clant() && !nang() || !a || !icass()) {
    if (da > dini) {
        if (oiaKoice() != niop) {
            if (!mioa()) {
                return false;
            }
        }
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (to == true) {
    cicCeli();
} else if (!so && to != true) {
    flaRair();
}
if (ec == cle && to != true && so) {
    sudon();
} else if (ahen != 9 && to != true && so && ec != cle) {
    trec();
} else if (toc >= 8 && to != true && so && ec != cle && ahen == 9) {
    antpe();
} else if (cing == true && to != true && so && ec != cle && ahen == 9 && toc <= 8) {
    missuc();
}
if (el == false && to != true && so && ec != cle && ahen == 9 && toc <= 8 && cing != true) {
    matri();
}
if (to != true && so && ec != cle && ahen == 9 && toc <= 8 && cing != true && el != false) {
    gourie();
}

Solution

{
    if (to) {
        cicCeli();
    }
    if (!so) {
        flaRair();
    }
    if (ec == cle) {
        sudon();
    }
    if (ahen != 9) {
        trec();
    }
    if (toc >= 8) {
        antpe();
    }
    if (cing) {
        missuc();
    }
    if (!el) {
        matri();
    }
    gourie();
}

Things to double-check in your solution:


Related puzzles: