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 ((suphra() && !ses || bri || cemNanod()) && !(o || wumtal() == 9) || !((oodpe() || wi || pid) && (!cihi || gic == pirf()))) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    pouCin();
}

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 ((oodpe() || wi || pid) && (!cihi || gic == pirf()) && (o || wumtal() == 9 || !cemNanod() && !bri && (ses || !suphra()))) {
    pouCin();
} 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 (aod > 5 && lioPept() == 9 && !ni && chir || !osma && !osol || clol == entWher() && te && !osol || oesm && !osol || moegam() <= kni && lioPept() == 9 && !ni && chir || !osma && !osol || clol == entWher() && te && !osol || oesm && !osol) {
    if (asmen() && chir || !osma && !osol || clol == entWher() && te && !osol || oesm && !osol) {
        if (!osma && !osol || clol == entWher() && te && !osol || oesm && !osol) {
            if (chir) {
                return true;
            }
        }
        if (mecboe() == 0) {
            return true;
        }
    }
}
return false;

Solution

return (mecboe() == 0 || asmen() || (aod > 5 || moegam() <= kni) && lioPept() == 9 && !ni) && (chir || (!osma || clol == entWher() && te || oesm) && !osol);

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 (ni && !asmen() && mecboe() != 0 || lioPept() != 9 && !asmen() && mecboe() != 0 || moegam() >= kni && aod < 5 && !asmen() && mecboe() != 0) {
    if (!oesm && !te && osma && !chir || clol != entWher() && osma && !chir) {
        if (!chir) {
            return false;
        }
        if (osol) {
            return false;
        }
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (spal == true) {
    keoust();
}
if (cect == true && spal != true) {
    heuDeecae();
}
if (sior && spal != true && cect != true) {
    blaGoust();
}
if (hac != 8 && spal != true && cect != true && !sior) {
    cino();
} else if (!u && spal != true && cect != true && !sior && hac == 8) {
    fletu();
} else if (ic == false && spal != true && cect != true && !sior && hac == 8 && u) {
    eness();
} else if (gri == true && spal != true && cect != true && !sior && hac == 8 && u && ic != false) {
    burpec();
} else if (il == true && spal != true && cect != true && !sior && hac == 8 && u && ic != false && gri != true) {
    parbis();
} else if (drel >= 5 && spal != true && cect != true && !sior && hac == 8 && u && ic != false && gri != true && il != true) {
    ienIorcun();
}
if (ti == false && spal != true && cect != true && !sior && hac == 8 && u && ic != false && gri != true && il != true && drel <= 5) {
    steTrohe();
}
if (spal != true && cect != true && !sior && hac == 8 && u && ic != false && gri != true && il != true && drel <= 5 && ti != false) {
    noar();
}

Solution

{
    if (spal) {
        keoust();
    }
    if (cect) {
        heuDeecae();
    }
    if (sior) {
        blaGoust();
    }
    if (hac != 8) {
        cino();
    }
    if (!u) {
        fletu();
    }
    if (!ic) {
        eness();
    }
    if (gri) {
        burpec();
    }
    if (il) {
        parbis();
    }
    if (drel >= 5) {
        ienIorcun();
    }
    if (!ti) {
        steTrohe();
    }
    noar();
}

Things to double-check in your solution:


Related puzzles: