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 (eerwi() || !((ilprac() || no || bata()) && qeou() || !kar) && ossBishte() && bome()) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    sedar();
}

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 ((!bome() || !ossBishte() || (ilprac() || no || bata()) && qeou() || !kar) && !eerwi()) {
    sedar();
} 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 (ibel() == 3 && qenClintu() == 4 && nint == fa && qoc || faqe || iocad() || baic() > 7 && hori >= 0) {
    if (e) {
        return true;
    }
}
return false;

Solution

return e || ibel() == 3 && qenClintu() == 4 && (nint == fa && qoc || faqe || iocad() || baic() > 7 && hori >= 0);

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 (qenClintu() != 4 && !e || ibel() != 3 && !e) {
    if (baic() < 7 && !iocad() && !faqe && !qoc && !e || nint != fa && !e) {
        if (nint != fa && !e) {
            if (!e) {
                return false;
            }
            if (!qoc) {
                return false;
            }
        }
        if (!faqe) {
            return false;
        }
        if (!iocad()) {
            return false;
        }
        if (hori <= 0) {
            return false;
        }
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (ris == true) {
    brur();
}
if (hio == true && ris != true) {
    clunwo();
}
if (ir == true && ris != true && hio != true) {
    prage();
} else if (cou == false && ris != true && hio != true && ir != true) {
    dicar();
}
if (hi == true && ris != true && hio != true && ir != true && cou != false) {
    eouwen();
}
if (arhi >= pha && ris != true && hio != true && ir != true && cou != false && hi != true) {
    hirlec();
} else if (o == true && ris != true && hio != true && ir != true && cou != false && hi != true && arhi <= pha) {
    eilIbrerm();
}
if (od == false && ris != true && hio != true && ir != true && cou != false && hi != true && arhi <= pha && o != true) {
    aoss();
}

Solution

{
    if (ris) {
        brur();
    }
    if (hio) {
        clunwo();
    }
    if (ir) {
        prage();
    }
    if (!cou) {
        dicar();
    }
    if (hi) {
        eouwen();
    }
    if (arhi >= pha) {
        hirlec();
    }
    if (o) {
        eilIbrerm();
    }
    if (!od) {
        aoss();
    }
}

Things to double-check in your solution:


Related puzzles: