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 (cerpen() && ((er || ce) && di == 8 && !asre || hacing() == omoont() && (ka || cer)) && !ni && slia == 0) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    cesde();
}

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 (slia != 0 || ni || (!cer && !ka || hacing() != omoont()) && (asre || di != 8 || !ce && !er) || !cerpen()) {
    cesde();
} 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 (knam > ke && il <= 9 && chra() && biss() || irce || edaMetoor() <= eg || !nir && asjunt() || etor() && biss() || irce || edaMetoor() <= eg || !nir && asjunt()) {
    if (etor() && biss() || irce || edaMetoor() <= eg || !nir && asjunt()) {
        if (!nir && asjunt()) {
            if (irce || edaMetoor() <= eg) {
                if (biss()) {
                    return true;
                }
            }
        }
        if (chra()) {
            return true;
        }
        if (il <= 9) {
            return true;
        }
    }
    if (chu >= serbio()) {
        return true;
    }
}
if (agir()) {
    return true;
}
return false;

Solution

return agir() && (chu >= serbio() || knam > ke) && (il <= 9 && chra() || etor()) && (biss() || irce || edaMetoor() <= eg || !nir && asjunt());

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 (knam < ke && chu <= serbio() || !agir()) {
    if (!etor() && !chra() || il >= 9) {
        if (nir && edaMetoor() >= eg && !irce && !biss()) {
            if (!biss()) {
                return false;
            }
            if (!irce) {
                return false;
            }
            if (edaMetoor() >= eg) {
                return false;
            }
            if (!asjunt()) {
                return false;
            }
        }
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (ir == false) {
    obor();
}
if (si == u && ir != false) {
    aess();
} else if (el && ir != false && si != u) {
    vone();
}
if (pri == true && ir != false && si != u && !el) {
    priu();
} else if (pism >= ses && ir != false && si != u && !el && pri != true) {
    servi();
}
if (hirm == false && ir != false && si != u && !el && pri != true && pism <= ses) {
    reurma();
}
if (eort == true && ir != false && si != u && !el && pri != true && pism <= ses && hirm != false) {
    cesoc();
} else if (omea == true && ir != false && si != u && !el && pri != true && pism <= ses && hirm != false && eort != true) {
    griu();
} else if (osco && ir != false && si != u && !el && pri != true && pism <= ses && hirm != false && eort != true && omea != true) {
    brone();
} else if (ir != false && si != u && !el && pri != true && pism <= ses && hirm != false && eort != true && omea != true && !osco) {
    sulInbad();
}

Solution

{
    if (!ir) {
        obor();
    }
    if (si == u) {
        aess();
    }
    if (el) {
        vone();
    }
    if (pri) {
        priu();
    }
    if (pism >= ses) {
        servi();
    }
    if (!hirm) {
        reurma();
    }
    if (eort) {
        cesoc();
    }
    if (omea) {
        griu();
    }
    if (osco) {
        brone();
    }
    sulInbad();
}

Things to double-check in your solution:


Related puzzles: