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 (esphe() || rean || !((po <= mo || !icae || direcs() > 3) && !o && !(kior == 6) && !usal && !sce)) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    iolad();
}

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 ((po <= mo || !icae || direcs() > 3) && !o && !(kior == 6) && !usal && !sce && !rean && !esphe()) {
    iolad();
} 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 (deng <= stri() && !de && rir == memon() && fene < 4 || coi != relat() && fene < 4 || plis() && rir == memon() && fene < 4 || coi != relat() && fene < 4 || mesmon() && rir == memon() && fene < 4 || coi != relat() && fene < 4) {
    if (ard || hellfa()) {
        if (ha) {
            return true;
        }
    }
}
return false;

Solution

return ha || ard || hellfa() || deng <= stri() && (!de || plis() || mesmon()) && (rir == memon() || coi != relat()) && fene < 4;

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 (!mesmon() && !plis() && de && !hellfa() && !ard && !ha || deng >= stri() && !hellfa() && !ard && !ha) {
    if (coi == relat() && rir != memon() && !hellfa() && !ard && !ha) {
        if (!ha) {
            return false;
        }
        if (!ard) {
            return false;
        }
        if (!hellfa()) {
            return false;
        }
        if (fene > 4) {
            return false;
        }
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (ce != uso) {
    glurt();
} else if (essi == true && ce == uso) {
    elcack();
}
if (at != rewi && ce == uso && essi != true) {
    brini();
} else if (a == true && ce == uso && essi != true && at == rewi) {
    fucbe();
}
if (!esm && ce == uso && essi != true && at == rewi && a != true) {
    ectpri();
} else if (veci == true && ce == uso && essi != true && at == rewi && a != true && esm) {
    rheeng();
} else if (mowo && ce == uso && essi != true && at == rewi && a != true && esm && veci != true) {
    nephi();
} else if ((ili != cosm) == true && ce == uso && essi != true && at == rewi && a != true && esm && veci != true && !mowo) {
    cocfro();
}
if (ce == uso && essi != true && at == rewi && a != true && esm && veci != true && !mowo && (ili != cosm) != true) {
    stird();
}

Solution

{
    if (ce != uso) {
        glurt();
    }
    if (essi) {
        elcack();
    }
    if (at != rewi) {
        brini();
    }
    if (a) {
        fucbe();
    }
    if (!esm) {
        ectpri();
    }
    if (veci) {
        rheeng();
    }
    if (mowo) {
        nephi();
    }
    if (ili != cosm) {
        cocfro();
    }
    stird();
}

Things to double-check in your solution:


Related puzzles: