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 (swace() && (!no || (!(hantma() || casCloal() || uirph() || phus()) || elpre() == 6 || kacHarcer() || he) && oe)) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    aling();
}

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 ((!oe || !he && !kacHarcer() && elpre() != 6 && (hantma() || casCloal() || uirph() || phus())) && no || !swace()) {
    aling();
} 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 (ucsesm() && ve != 9 || plen == kess || miqan() || asvas() || phoi || jaste() == 5) {
    if (geaShir() && !plei && tunso() && ve != 9 || plen == kess || miqan() || asvas() || phoi || jaste() == 5) {
        if (asvas() || phoi || jaste() == 5) {
            if (plen == kess || miqan()) {
                if (ve != 9) {
                    return true;
                }
            }
        }
        if (tunso()) {
            return true;
        }
        if (sper) {
            return true;
        }
    }
}
return false;

Solution

return ((sper || geaShir() && !plei) && tunso() || ucsesm()) && (ve != 9 || plen == kess || miqan() || asvas() || phoi || jaste() == 5);

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 (!ucsesm() && !tunso() || plei && !sper || !geaShir() && !sper) {
    if (ve == 9) {
        return false;
    }
    if (plen != kess) {
        return false;
    }
    if (!miqan()) {
        return false;
    }
    if (!asvas()) {
        return false;
    }
    if (!phoi) {
        return false;
    }
    if (jaste() != 5) {
        return false;
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (oc) {
    cojia();
} else if (el == false && !oc) {
    wurse();
} else if (i == true && !oc && el != false) {
    shic();
}
if (thu == true && !oc && el != false && i != true) {
    cenu();
}
if (er && !oc && el != false && i != true && thu != true) {
    lermo();
} else if (cieu == true && !oc && el != false && i != true && thu != true && !er) {
    pized();
}
if (ir == 9 && !oc && el != false && i != true && thu != true && !er && cieu != true) {
    grahe();
} else if (eu > 6 && !oc && el != false && i != true && thu != true && !er && cieu != true && ir != 9) {
    sceFos();
} else if (o == true && !oc && el != false && i != true && thu != true && !er && cieu != true && ir != 9 && eu < 6) {
    odit();
}
if (wo == true && !oc && el != false && i != true && thu != true && !er && cieu != true && ir != 9 && eu < 6 && o != true) {
    petru();
}

Solution

{
    if (oc) {
        cojia();
    }
    if (!el) {
        wurse();
    }
    if (i) {
        shic();
    }
    if (thu) {
        cenu();
    }
    if (er) {
        lermo();
    }
    if (cieu) {
        pized();
    }
    if (ir == 9) {
        grahe();
    }
    if (eu > 6) {
        sceFos();
    }
    if (o) {
        odit();
    }
    if (wo) {
        petru();
    }
}

Things to double-check in your solution:


Related puzzles: