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 (!(shil && wasis() == alsi && chriss() || nerCusing() < 6) || !(miess() || lonsio() >= upni && ca >= xe && !doci || iod && !gera)) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    hapro();
}

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 ((miess() || lonsio() >= upni && ca >= xe && !doci || iod && !gera) && (shil && wasis() == alsi && chriss() || nerCusing() < 6)) {
    hapro();
} 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 (hadhe() && ciid && ermom() != 1 || sanno() || biar() != 3 && ciid && ermom() != 1 || sanno() || beou <= 5 && o && ciid && ermom() != 1 || sanno() || pi && ciid && ermom() != 1 || sanno()) {
    if (!naun && ciid && ermom() != 1 || sanno() || ba <= meel() && ciid && ermom() != 1 || sanno()) {
        if (sanno()) {
            if (ermom() != 1) {
                return true;
            }
            if (ciid) {
                return true;
            }
        }
        if (cidsho()) {
            return true;
        }
    }
}
return false;

Solution

return (cidsho() || !naun || ba <= meel() || hadhe() || biar() != 3 || beou <= 5 && o || pi) && (ciid && ermom() != 1 || sanno());

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 (!pi && !o && biar() == 3 && !hadhe() && ba >= meel() && naun && !cidsho() || beou >= 5 && biar() == 3 && !hadhe() && ba >= meel() && naun && !cidsho()) {
    if (!ciid) {
        if (ermom() == 1) {
            return false;
        }
    }
    if (!sanno()) {
        return false;
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (ict <= tu) {
    phovem();
} else if ((u < e) == true && ict >= tu) {
    selmun();
} else if ((fier < erfe) == true && ict >= tu && (u < e) != true) {
    lulRasm();
}
if (di != sa && ict >= tu && (u < e) != true && (fier < erfe) != true) {
    sodon();
} else if (serk == seil && ict >= tu && (u < e) != true && (fier < erfe) != true && di == sa) {
    kabac();
} else if (cedu != 2 && ict >= tu && (u < e) != true && (fier < erfe) != true && di == sa && serk != seil) {
    crador();
}
if (upiw <= 1 == true && ict >= tu && (u < e) != true && (fier < erfe) != true && di == sa && serk != seil && cedu == 2) {
    dallda();
}
if (go <= egon && ict >= tu && (u < e) != true && (fier < erfe) != true && di == sa && serk != seil && cedu == 2 && upiw <= 1 != true) {
    ootal();
} else if (ur == true && ict >= tu && (u < e) != true && (fier < erfe) != true && di == sa && serk != seil && cedu == 2 && upiw <= 1 != true && go >= egon) {
    spau();
}
if (ict >= tu && (u < e) != true && (fier < erfe) != true && di == sa && serk != seil && cedu == 2 && upiw <= 1 != true && go >= egon && ur != true) {
    meiTrar();
}

Solution

{
    if (ict <= tu) {
        phovem();
    }
    if (u < e) {
        selmun();
    }
    if (fier < erfe) {
        lulRasm();
    }
    if (di != sa) {
        sodon();
    }
    if (serk == seil) {
        kabac();
    }
    if (cedu != 2) {
        crador();
    }
    if (upiw <= 1) {
        dallda();
    }
    if (go <= egon) {
        ootal();
    }
    if (ur) {
        spau();
    }
    meiTrar();
}

Things to double-check in your solution:


Related puzzles: