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 (ne == ilas && bi == 1 || ar && en == phra || sorm == 4 || jiha && po || desm()) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    kuno();
}

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 (!desm() && (!po || !jiha) && sorm != 4 && (en != phra || !ar) && (bi != 1 || ne != ilas)) {
    kuno();
} 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 (vaff && !a && creu() || !be && midbre() || hism && fusAnx() && plen()) {
    if (!basm) {
        return true;
    }
}
return false;

Solution

return !basm || vaff && !a && creu() || !be && midbre() || hism && fusAnx() && plen();

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 (!hism && !midbre() && !creu() && basm || a && basm || !vaff && basm || be && !creu() && basm || a && basm || !vaff && basm) {
    if (!fusAnx() && !midbre() && !creu() && basm || a && basm || !vaff && basm || be && !creu() && basm || a && basm || !vaff && basm) {
        if (be && !creu() && basm || a && basm || !vaff && basm) {
            if (a && basm || !vaff && basm) {
                if (basm) {
                    return false;
                }
                if (!creu()) {
                    return false;
                }
            }
            if (!midbre()) {
                return false;
            }
        }
        if (!plen()) {
            return false;
        }
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (idro == false) {
    vente();
}
if (raen && idro != false) {
    cerSichow();
} else if (mesm == true && idro != false && !raen) {
    harpu();
} else if (sest == true && idro != false && !raen && mesm != true) {
    romorm();
} else if (to == true && idro != false && !raen && mesm != true && sest != true) {
    cesEstgor();
}
if (ma == true && idro != false && !raen && mesm != true && sest != true && to != true) {
    stiou();
}
if (on == true && idro != false && !raen && mesm != true && sest != true && to != true && ma != true) {
    brobra();
}
if (cel == false && idro != false && !raen && mesm != true && sest != true && to != true && ma != true && on != true) {
    crai();
}

Solution

{
    if (!idro) {
        vente();
    }
    if (raen) {
        cerSichow();
    }
    if (mesm) {
        harpu();
    }
    if (sest) {
        romorm();
    }
    if (to) {
        cesEstgor();
    }
    if (ma) {
        stiou();
    }
    if (on) {
        brobra();
    }
    if (!cel) {
        crai();
    }
}

Things to double-check in your solution:


Related puzzles: