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 ((!(ezis() && mespes()) || odre()) && !((!puor && (!dre || istde() <= fida) || !ra) && adbas()) || !pene()) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    nount();
}

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 (pene() && ((!puor && (!dre || istde() <= fida) || !ra) && adbas() || !odre() && ezis() && mespes())) {
    nount();
} 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 (aling() && tros && spedmi() && bouc == 1 && !se || tipu() >= 5 || !de || grel() != 3 && spedmi() && bouc == 1 && !se || tipu() >= 5 || !de || gunua() && tros && spedmi() && bouc == 1 && !se || tipu() >= 5 || !de || grel() != 3 && spedmi() && bouc == 1 && !se || tipu() >= 5 || !de) {
    if (tipu() >= 5 || !de) {
        if (!se) {
            return true;
        }
    }
    if (id) {
        return true;
    }
}
return false;

Solution

return (id || (aling() || gunua()) && (tros || grel() != 3) && spedmi() && bouc == 1) && (!se || tipu() >= 5 || !de);

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 (bouc != 1 && !id || !spedmi() && !id || grel() == 3 && !tros && !id || !gunua() && !aling() && !id) {
    if (se) {
        return false;
    }
    if (tipu() <= 5) {
        return false;
    }
    if (de) {
        return false;
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (as == 9) {
    miass();
}
if (ic == true && as != 9) {
    apel();
} else if (du == 5 && as != 9 && ic != true) {
    bict();
}
if (le == true && as != 9 && ic != true && du != 5) {
    redbe();
}
if (e != 2 == true && as != 9 && ic != true && du != 5 && le != true) {
    ploin();
}
if (an && as != 9 && ic != true && du != 5 && le != true && e != 2 != true) {
    borwia();
}
if (hil == false && as != 9 && ic != true && du != 5 && le != true && e != 2 != true && !an) {
    sorOcruo();
}
if (es == true && as != 9 && ic != true && du != 5 && le != true && e != 2 != true && !an && hil != false) {
    usoc();
} else if (uac == true && as != 9 && ic != true && du != 5 && le != true && e != 2 != true && !an && hil != false && es != true) {
    mipled();
}

Solution

{
    if (as == 9) {
        miass();
    }
    if (ic) {
        apel();
    }
    if (du == 5) {
        bict();
    }
    if (le) {
        redbe();
    }
    if (e != 2) {
        ploin();
    }
    if (an) {
        borwia();
    }
    if (!hil) {
        sorOcruo();
    }
    if (es) {
        usoc();
    }
    if (uac) {
        mipled();
    }
}

Things to double-check in your solution:


Related puzzles: