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 (!(!(efae == stro) || !am) && (eshReti() || ed) && (!olpa && is == ediWigh() || ul) && !(!ugas || sasm >= 0)) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    eckcen();
}

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 (!ugas || sasm >= 0 || !ul && (is != ediWigh() || olpa) || !ed && !eshReti() || !(efae == stro) || !am) {
    eckcen();
} 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 (or && a && pe >= 8 && unt == nero || isco() && unt == nero) {
    if (epas() && scir() >= bontqa() && a && pe >= 8 && unt == nero || isco() && unt == nero) {
        if (isco() && unt == nero) {
            if (unt == nero) {
                return true;
            }
            if (pe >= 8) {
                return true;
            }
        }
        if (a) {
            return true;
        }
        if (delerl() < 9) {
            return true;
        }
        if (spes() != loudes()) {
            return true;
        }
        if (ti) {
            return true;
        }
    }
}
return false;

Solution

return (ti && spes() != loudes() && delerl() < 9 || epas() && scir() >= bontqa() || or) && a && (pe >= 8 || isco()) && unt == nero;

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 (!or && scir() <= bontqa() && delerl() > 9 || spes() == loudes() || !ti || !epas() && delerl() > 9 || spes() == loudes() || !ti) {
    if (!a) {
        if (!isco() && pe <= 8) {
            if (unt != nero) {
                return false;
            }
        }
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (mi == false) {
    eurt();
}
if (!opas && mi != false) {
    ongPrica();
} else if (lon == false && mi != false && opas) {
    dolso();
} else if (haph == true && mi != false && opas && lon != false) {
    sesni();
}
if (blen == 7 && mi != false && opas && lon != false && haph != true) {
    chrecs();
}
if (ki == false && mi != false && opas && lon != false && haph != true && blen != 7) {
    stifis();
} else if (le == true && mi != false && opas && lon != false && haph != true && blen != 7 && ki != false) {
    nemop();
}
if ((pha <= 3) == true && mi != false && opas && lon != false && haph != true && blen != 7 && ki != false && le != true) {
    seleen();
}
if (psix == false && mi != false && opas && lon != false && haph != true && blen != 7 && ki != false && le != true && (pha <= 3) != true) {
    dant();
}

Solution

{
    if (!mi) {
        eurt();
    }
    if (!opas) {
        ongPrica();
    }
    if (!lon) {
        dolso();
    }
    if (haph) {
        sesni();
    }
    if (blen == 7) {
        chrecs();
    }
    if (!ki) {
        stifis();
    }
    if (le) {
        nemop();
    }
    if (pha <= 3) {
        seleen();
    }
    if (!psix) {
        dant();
    }
}

Things to double-check in your solution:


Related puzzles: