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 ((su || onta() == tio || fing && selbie() >= ibri()) && risil() && !(mu != 7 || aenShilre()) && (jevon() || en > 7 || i)) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    troo();
}

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 (!i && en < 7 && !jevon() || mu != 7 || aenShilre() || !risil() || (selbie() <= ibri() || !fing) && onta() != tio && !su) {
    troo();
} 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 (id && rurHedpi() || ceo && ro || tro < 3 && shid || nierho() || cil >= 5 || !me && shid || nierho() || cil >= 5 || !he && rurHedpi() || ceo && ro || tro < 3 && shid || nierho() || cil >= 5 || !me && shid || nierho() || cil >= 5) {
    if (on) {
        return true;
    }
}
return false;

Solution

return on || (id || !he) && (rurHedpi() || ceo && ro || (tro < 3 || !me) && (shid || nierho() || cil >= 5));

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 (he && !id && !on) {
    if (me && tro > 3 && !ro && !rurHedpi() && !on || !ceo && !rurHedpi() && !on) {
        if (!ceo && !rurHedpi() && !on) {
            if (!on) {
                return false;
            }
            if (!rurHedpi()) {
                return false;
            }
            if (!ro) {
                return false;
            }
        }
        if (!shid) {
            return false;
        }
        if (!nierho()) {
            return false;
        }
        if (cil <= 5) {
            return false;
        }
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (in == true) {
    icsced();
} else if (fint == false && in != true) {
    cesmi();
} else if (!ce && in != true && fint != false) {
    hilOpho();
} else if (kum < 8 && in != true && fint != false && ce) {
    reoc();
} else if (op == true && in != true && fint != false && ce && kum > 8) {
    heph();
} else if (iam == true && in != true && fint != false && ce && kum > 8 && op != true) {
    duouss();
} else if (!ias && in != true && fint != false && ce && kum > 8 && op != true && iam != true) {
    cimra();
} else if (adso == true && in != true && fint != false && ce && kum > 8 && op != true && iam != true && ias) {
    faent();
} else if (ezor == true && in != true && fint != false && ce && kum > 8 && op != true && iam != true && ias && adso != true) {
    eniNewor();
}
if (in != true && fint != false && ce && kum > 8 && op != true && iam != true && ias && adso != true && ezor != true) {
    cesPeew();
}

Solution

{
    if (in) {
        icsced();
    }
    if (!fint) {
        cesmi();
    }
    if (!ce) {
        hilOpho();
    }
    if (kum < 8) {
        reoc();
    }
    if (op) {
        heph();
    }
    if (iam) {
        duouss();
    }
    if (!ias) {
        cimra();
    }
    if (adso) {
        faent();
    }
    if (ezor) {
        eniNewor();
    }
    cesPeew();
}

Things to double-check in your solution:


Related puzzles: