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 (!((udun || !le || !((!o || !heaw) && einAfren())) && !(!(rusi < 7) || !chra) && seot) || !dukgor() && (itin || onco != 5)) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    cuess();
}

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 ((onco == 5 && !itin || dukgor()) && (udun || !le || !((!o || !heaw) && einAfren())) && !(!(rusi < 7) || !chra) && seot) {
    cuess();
} 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 (qadmar() || hane && oo && i && !icum && !bect || !cao || ot == 0 && !oen) {
    if (nenim() || si) {
        if (skon) {
            return true;
        }
    }
}
return false;

Solution

return skon || nenim() || si || qadmar() || hane && (oo && i && (!icum && !bect || !cao) || ot == 0 && !oen);

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 (!hane && !qadmar() && !si && !nenim() && !skon) {
    if (ot != 0 && cao && bect && !qadmar() && !si && !nenim() && !skon || icum && !qadmar() && !si && !nenim() && !skon || !i && !qadmar() && !si && !nenim() && !skon || !oo && !qadmar() && !si && !nenim() && !skon) {
        if (!i && !qadmar() && !si && !nenim() && !skon || !oo && !qadmar() && !si && !nenim() && !skon) {
            if (icum && !qadmar() && !si && !nenim() && !skon) {
                if (!skon) {
                    return false;
                }
                if (!nenim()) {
                    return false;
                }
                if (!si) {
                    return false;
                }
                if (!qadmar()) {
                    return false;
                }
                if (bect) {
                    return false;
                }
            }
            if (cao) {
                return false;
            }
        }
        if (oen) {
            return false;
        }
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (ed == false) {
    oustis();
}
if (ciss == false && ed != false) {
    osti();
} else if (e == false && ed != false && ciss != false) {
    ismon();
} else if (scre == a && ed != false && ciss != false && e != false) {
    letrin();
} else if (he > fla && ed != false && ciss != false && e != false && scre != a) {
    geohul();
} else if ((ju != ior) == true && ed != false && ciss != false && e != false && scre != a && he < fla) {
    poeu();
} else if (anid == false && ed != false && ciss != false && e != false && scre != a && he < fla && (ju != ior) != true) {
    uanSme();
}
if (iet == false && ed != false && ciss != false && e != false && scre != a && he < fla && (ju != ior) != true && anid != false) {
    dicos();
}
if (hefe == true && ed != false && ciss != false && e != false && scre != a && he < fla && (ju != ior) != true && anid != false && iet != false) {
    cecUckcon();
} else if (!sa && ed != false && ciss != false && e != false && scre != a && he < fla && (ju != ior) != true && anid != false && iet != false && hefe != true) {
    fliwk();
} else if (ed != false && ciss != false && e != false && scre != a && he < fla && (ju != ior) != true && anid != false && iet != false && hefe != true && sa) {
    silod();
}

Solution

{
    if (!ed) {
        oustis();
    }
    if (!ciss) {
        osti();
    }
    if (!e) {
        ismon();
    }
    if (scre == a) {
        letrin();
    }
    if (he > fla) {
        geohul();
    }
    if (ju != ior) {
        poeu();
    }
    if (!anid) {
        uanSme();
    }
    if (!iet) {
        dicos();
    }
    if (hefe) {
        cecUckcon();
    }
    if (!sa) {
        fliwk();
    }
    silod();
}

Things to double-check in your solution:


Related puzzles: