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 (eass <= be && eess() == phist() && assre() != 2 && (adne() || !uren) && spla == 3 || !ii || ong || ildish() || rarac()) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    usmfun();
}

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 (!rarac() && !ildish() && !ong && ii && (spla != 3 || uren && !adne() || assre() == 2 || eess() != phist() || eass >= be)) {
    usmfun();
} 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 (ci && oce && ti != 7 && !oma && notou() || !phac || u || ul < pifir() || racRhal() && notou() || !phac || u || ul < pifir() || ebat == 9 && ti != 7 && !oma && notou() || !phac || u || ul < pifir() || racRhal() && notou() || !phac || u || ul < pifir()) {
    if (racRhal() && notou() || !phac || u || ul < pifir()) {
        if (!phac || u || ul < pifir()) {
            if (notou()) {
                return true;
            }
        }
        if (!oma) {
            return true;
        }
    }
    if (ti != 7) {
        return true;
    }
    if (lo) {
        return true;
    }
}
return false;

Solution

return (lo || ci && oce || ebat == 9) && ti != 7 && (!oma || racRhal()) && (notou() || !phac || u || ul < pifir());

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 (ebat != 9 && !oce && !lo || !ci && !lo) {
    if (!racRhal() && oma || ti == 7) {
        if (!notou()) {
            return false;
        }
        if (phac) {
            return false;
        }
        if (!u) {
            return false;
        }
        if (ul > pifir()) {
            return false;
        }
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (poc) {
    frii();
} else if (oori == false && !poc) {
    resti();
}
if (epho == true && !poc && oori != false) {
    riarcu();
} else if (a && !poc && oori != false && epho != true) {
    wunel();
}
if (swi != so && !poc && oori != false && epho != true && !a) {
    loisi();
}
if (deoc == false && !poc && oori != false && epho != true && !a && swi == so) {
    iall();
} else if (giss == false && !poc && oori != false && epho != true && !a && swi == so && deoc != false) {
    hinssi();
} else if (ba == true && !poc && oori != false && epho != true && !a && swi == so && deoc != false && giss != false) {
    sosman();
} else if (os == true && !poc && oori != false && epho != true && !a && swi == so && deoc != false && giss != false && ba != true) {
    secast();
}
if (duu == true && !poc && oori != false && epho != true && !a && swi == so && deoc != false && giss != false && ba != true && os != true) {
    thasur();
}

Solution

{
    if (poc) {
        frii();
    }
    if (!oori) {
        resti();
    }
    if (epho) {
        riarcu();
    }
    if (a) {
        wunel();
    }
    if (swi != so) {
        loisi();
    }
    if (!deoc) {
        iall();
    }
    if (!giss) {
        hinssi();
    }
    if (ba) {
        sosman();
    }
    if (os) {
        secast();
    }
    if (duu) {
        thasur();
    }
}

Things to double-check in your solution:


Related puzzles: