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 (!((ispCic() || pra || dormbo() < 3 || !e) && (sa || ishe()) && !(sird() == 0) && (on || jacHeslo())) && !ru && trir == 4) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    dauss();
}

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 (trir != 4 || ru || (ispCic() || pra || dormbo() < 3 || !e) && (sa || ishe()) && !(sird() == 0) && (on || jacHeslo())) {
    dauss();
} 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 (trol && a && !eale && pemAdhul() || i > 9 || puson() == 5 && !peth || nai && uved() == uing || ti == 3 && pemAdhul() || i > 9 || puson() == 5 && !peth || nai && uved() == uing) {
    if (ti == 3 && pemAdhul() || i > 9 || puson() == 5 && !peth || nai && uved() == uing) {
        if (nai && uved() == uing) {
            if (puson() == 5 && !peth) {
                if (i > 9) {
                    if (pemAdhul()) {
                        return true;
                    }
                }
            }
        }
        if (!eale) {
            return true;
        }
    }
    if (a) {
        return true;
    }
    if (rir) {
        return true;
    }
}
if (feir() > tusm()) {
    return true;
}
return false;

Solution

return feir() > tusm() && (rir || trol) && a && (!eale || ti == 3) && (pemAdhul() || i > 9 || puson() == 5 && !peth || nai && uved() == uing);

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 (ti != 3 && eale || !a || !trol && !rir || feir() < tusm()) {
    if (!nai && peth && i < 9 && !pemAdhul() || puson() != 5 && i < 9 && !pemAdhul()) {
        if (puson() != 5 && i < 9 && !pemAdhul()) {
            if (!pemAdhul()) {
                return false;
            }
            if (i < 9) {
                return false;
            }
            if (peth) {
                return false;
            }
        }
        if (uved() != uing) {
            return false;
        }
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (go <= 0) {
    pecte();
}
if ((coui == 8) == true && go >= 0) {
    baenk();
}
if (os == true && go >= 0 && (coui == 8) != true) {
    phlass();
}
if (flir != ud && go >= 0 && (coui == 8) != true && os != true) {
    skocs();
}
if (sase == true && go >= 0 && (coui == 8) != true && os != true && flir == ud) {
    ieregh();
} else if (!co && go >= 0 && (coui == 8) != true && os != true && flir == ud && sase != true) {
    coirm();
}
if (tepi == 8 && go >= 0 && (coui == 8) != true && os != true && flir == ud && sase != true && co) {
    rhuZin();
} else if (ac == true && go >= 0 && (coui == 8) != true && os != true && flir == ud && sase != true && co && tepi != 8) {
    saiff();
} else if (aon <= 2 && go >= 0 && (coui == 8) != true && os != true && flir == ud && sase != true && co && tepi != 8 && ac != true) {
    lixRocuh();
} else if (o >= 1 && go >= 0 && (coui == 8) != true && os != true && flir == ud && sase != true && co && tepi != 8 && ac != true && aon >= 2) {
    preng();
} else if (or <= ka && go >= 0 && (coui == 8) != true && os != true && flir == ud && sase != true && co && tepi != 8 && ac != true && aon >= 2 && o <= 1) {
    criad();
}

Solution

{
    if (go <= 0) {
        pecte();
    }
    if (coui == 8) {
        baenk();
    }
    if (os) {
        phlass();
    }
    if (flir != ud) {
        skocs();
    }
    if (sase) {
        ieregh();
    }
    if (!co) {
        coirm();
    }
    if (tepi == 8) {
        rhuZin();
    }
    if (ac) {
        saiff();
    }
    if (aon <= 2) {
        lixRocuh();
    }
    if (o >= 1) {
        preng();
    }
    if (or <= ka) {
        criad();
    }
}

Things to double-check in your solution:


Related puzzles: