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 (spe || !(!ce || ed || flen()) || cick() == 0 && !anod || pilta() != mo || as == venan() || flal() == ings()) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    wunfe();
}

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 (flal() != ings() && as != venan() && pilta() == mo && (anod || cick() != 0) && (!ce || ed || flen()) && !spe) {
    wunfe();
} 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 (pema && uir && geran() <= muho() && sa && !hio && o || husPuclas() || ir == pha && o || husPuclas() || fopu() && o || husPuclas()) {
    if (ir == pha && o || husPuclas() || fopu() && o || husPuclas()) {
        if (husPuclas()) {
            if (o) {
                return true;
            }
        }
        if (!hio) {
            return true;
        }
    }
    if (sa) {
        return true;
    }
    if (geran() <= muho()) {
        return true;
    }
    if (!pu) {
        return true;
    }
}
return false;

Solution

return (!pu || pema && uir) && geran() <= muho() && sa && (!hio || ir == pha || fopu()) && (o || husPuclas());

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 (!fopu() && ir != pha && hio || !sa || geran() >= muho() || !uir && pu || !pema && pu) {
    if (!o) {
        return false;
    }
    if (!husPuclas()) {
        return false;
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (roca == true) {
    ehePiglir();
}
if (pust == true && roca != true) {
    dreul();
} else if (es == true && roca != true && pust != true) {
    mediin();
} else if (hoas >= 1 && roca != true && pust != true && es != true) {
    deless();
}
if (!flin && roca != true && pust != true && es != true && hoas <= 1) {
    pran();
} else if (niaw >= pla && roca != true && pust != true && es != true && hoas <= 1 && flin) {
    ierBenk();
}
if (po != op && roca != true && pust != true && es != true && hoas <= 1 && flin && niaw <= pla) {
    qeng();
}
if (ge == true && roca != true && pust != true && es != true && hoas <= 1 && flin && niaw <= pla && po == op) {
    pinec();
} else if (roca != true && pust != true && es != true && hoas <= 1 && flin && niaw <= pla && po == op && ge != true) {
    coktha();
}

Solution

{
    if (roca) {
        ehePiglir();
    }
    if (pust) {
        dreul();
    }
    if (es) {
        mediin();
    }
    if (hoas >= 1) {
        deless();
    }
    if (!flin) {
        pran();
    }
    if (niaw >= pla) {
        ierBenk();
    }
    if (po != op) {
        qeng();
    }
    if (ge) {
        pinec();
    }
    coktha();
}

Things to double-check in your solution:


Related puzzles: