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 ((ecal() || !esm || iidUoa()) && (easGrel() == 9 && (!etnar() || uqaus()) || (poceoc() || chomar()) && es != 3 || pid || whil())) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    eass();
}

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 (!whil() && !pid && (es == 3 || !chomar() && !poceoc()) && (!uqaus() && etnar() || easGrel() != 9) || !iidUoa() && esm && !ecal()) {
    eass();
} 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 (mied && colcer() || el == drac() && colcer() || ounthu() > he && colcer() || hoh > vehe() && ur && colcer() || e == rarm && saang() && colcer() || ratrod() && saang() && colcer()) {
    if (sqee && colcer()) {
        if (colcer()) {
            return true;
        }
        if (le) {
            return true;
        }
    }
    if (nuc < 9) {
        return true;
    }
}
return false;

Solution

return (nuc < 9 && (le || sqee) || mied || el == drac() || ounthu() > he || hoh > vehe() && ur || (e == rarm || ratrod()) && saang()) && colcer();

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 (!saang() && !ur && ounthu() < he && el != drac() && !mied && !sqee && !le || nuc > 9 || hoh < vehe() && ounthu() < he && el != drac() && !mied && !sqee && !le || nuc > 9 || !ratrod() && e != rarm && !ur && ounthu() < he && el != drac() && !mied && !sqee && !le || nuc > 9 || hoh < vehe() && ounthu() < he && el != drac() && !mied && !sqee && !le || nuc > 9) {
    if (!colcer()) {
        return false;
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (jit == false) {
    psal();
} else if (!be && jit != false) {
    poom();
}
if (bi == oo && jit != false && be) {
    niaNidout();
}
if (oss == true && jit != false && be && bi != oo) {
    scohos();
} else if (sli == true && jit != false && be && bi != oo && oss != true) {
    palers();
} else if (vur != 2 && jit != false && be && bi != oo && oss != true && sli != true) {
    secdo();
}
if (an == true && jit != false && be && bi != oo && oss != true && sli != true && vur == 2) {
    micil();
} else if (ro < 7 && jit != false && be && bi != oo && oss != true && sli != true && vur == 2 && an != true) {
    nucos();
} else if (ma == true && jit != false && be && bi != oo && oss != true && sli != true && vur == 2 && an != true && ro > 7) {
    alwal();
} else if (irn == true && jit != false && be && bi != oo && oss != true && sli != true && vur == 2 && an != true && ro > 7 && ma != true) {
    welhod();
} else if (jit != false && be && bi != oo && oss != true && sli != true && vur == 2 && an != true && ro > 7 && ma != true && irn != true) {
    griha();
}

Solution

{
    if (!jit) {
        psal();
    }
    if (!be) {
        poom();
    }
    if (bi == oo) {
        niaNidout();
    }
    if (oss) {
        scohos();
    }
    if (sli) {
        palers();
    }
    if (vur != 2) {
        secdo();
    }
    if (an) {
        micil();
    }
    if (ro < 7) {
        nucos();
    }
    if (ma) {
        alwal();
    }
    if (irn) {
        welhod();
    }
    griha();
}

Things to double-check in your solution:


Related puzzles: