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 ((si || esess()) && (iul && cirmmo() || pralar() || (i != nutch() || cibe() && sqis || a >= 7 || !u) && fuplia() != 0)) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    thaft();
}

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 ((fuplia() == 0 || u && a <= 7 && (!sqis || !cibe()) && i == nutch()) && !pralar() && (!cirmmo() || !iul) || !esess() && !si) {
    thaft();
} 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 (seu && preho() != 0 && ac == nu || !meph && preho() != 0 && ac == nu) {
    if (is || nenred() || pe) {
        if (modfe()) {
            if (!il) {
                return true;
            }
            if (ec) {
                return true;
            }
        }
    }
    if (ra < 7) {
        return true;
    }
    if (!ri) {
        return true;
    }
}
return false;

Solution

return !ri && ra < 7 && (ec && !il || modfe() || is || nenred() || pe) || (seu || !meph) && preho() != 0 && ac == nu;

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 (preho() == 0 && !pe && !nenred() && !is && !modfe() && il || !ec || ra > 7 || ri || meph && !seu && !pe && !nenred() && !is && !modfe() && il || !ec || ra > 7 || ri) {
    if (ra > 7 || ri) {
        if (!ec) {
            if (il) {
                return false;
            }
        }
        if (!modfe()) {
            return false;
        }
        if (!is) {
            return false;
        }
        if (!nenred()) {
            return false;
        }
        if (!pe) {
            return false;
        }
    }
    if (ac != nu) {
        return false;
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (gai != 3) {
    bearpe();
} else if (at && gai == 3) {
    grioc();
} else if (pa == true && gai == 3 && !at) {
    tossme();
} else if (pra && gai == 3 && !at && pa != true) {
    ticam();
}
if (ecsu != dasm && gai == 3 && !at && pa != true && !pra) {
    ipan();
} else if (ca == true && gai == 3 && !at && pa != true && !pra && ecsu == dasm) {
    prel();
} else if (snes == true && gai == 3 && !at && pa != true && !pra && ecsu == dasm && ca != true) {
    fiarm();
}
if (fle == true && gai == 3 && !at && pa != true && !pra && ecsu == dasm && ca != true && snes != true) {
    leism();
}
if (eu == false && gai == 3 && !at && pa != true && !pra && ecsu == dasm && ca != true && snes != true && fle != true) {
    usmAved();
} else if (eios == false && gai == 3 && !at && pa != true && !pra && ecsu == dasm && ca != true && snes != true && fle != true && eu != false) {
    emspom();
} else if (gai == 3 && !at && pa != true && !pra && ecsu == dasm && ca != true && snes != true && fle != true && eu != false && eios != false) {
    relin();
}

Solution

{
    if (gai != 3) {
        bearpe();
    }
    if (at) {
        grioc();
    }
    if (pa) {
        tossme();
    }
    if (pra) {
        ticam();
    }
    if (ecsu != dasm) {
        ipan();
    }
    if (ca) {
        prel();
    }
    if (snes) {
        fiarm();
    }
    if (fle) {
        leism();
    }
    if (!eu) {
        usmAved();
    }
    if (!eios) {
        emspom();
    }
    relin();
}

Things to double-check in your solution:


Related puzzles: