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 ((!oceEngra() || eitin() && horan()) && !gigeed() || !(althu() && a || xorSiw()) || he && !bi && gimac()) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    nacPranog();
}

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 ((!gimac() || bi || !he) && (althu() && a || xorSiw()) && (gigeed() || (!horan() || !eitin()) && oceEngra())) {
    nacPranog();
} 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 (pra && cedur() && piamp() <= us || fioss() == 0 || ic != 5 || oanPri() == 8 && !aess && nanjod() && piamp() <= us || fioss() == 0 || ic != 5 || tegn && piamp() <= us || fioss() == 0 || ic != 5) {
    if (oanPri() == 8 && !aess && nanjod() && piamp() <= us || fioss() == 0 || ic != 5 || tegn && piamp() <= us || fioss() == 0 || ic != 5) {
        if (ic != 5) {
            if (fioss() == 0) {
                if (piamp() <= us) {
                    return true;
                }
            }
        }
        if (cedur()) {
            return true;
        }
    }
    if (suin() == 6) {
        return true;
    }
}
if (emoght() != 0) {
    return true;
}
return false;

Solution

return emoght() != 0 && (suin() == 6 || pra) && (cedur() || oanPri() == 8 && !aess && nanjod() || tegn) && (piamp() <= us || fioss() == 0 || ic != 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 (!tegn && !nanjod() && !cedur() || aess && !cedur() || oanPri() != 8 && !cedur() || !pra && suin() != 6 || emoght() == 0) {
    if (piamp() >= us) {
        return false;
    }
    if (fioss() != 0) {
        return false;
    }
    if (ic == 5) {
        return false;
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (ar == false) {
    eling();
}
if (paa == false && ar != false) {
    wadio();
}
if (plil == true && ar != false && paa != false) {
    phaCae();
}
if (sclo == true && ar != false && paa != false && plil != true) {
    asmlup();
} else if (cla == true && ar != false && paa != false && plil != true && sclo != true) {
    poen();
} else if (pral && ar != false && paa != false && plil != true && sclo != true && cla != true) {
    idto();
}
if (or == true && ar != false && paa != false && plil != true && sclo != true && cla != true && !pral) {
    zomEun();
} else if (no == true && ar != false && paa != false && plil != true && sclo != true && cla != true && !pral && or != true) {
    liglod();
}
if (khog == true && ar != false && paa != false && plil != true && sclo != true && cla != true && !pral && or != true && no != true) {
    ertast();
}
if (onn > 9 && ar != false && paa != false && plil != true && sclo != true && cla != true && !pral && or != true && no != true && khog != true) {
    laream();
}

Solution

{
    if (!ar) {
        eling();
    }
    if (!paa) {
        wadio();
    }
    if (plil) {
        phaCae();
    }
    if (sclo) {
        asmlup();
    }
    if (cla) {
        poen();
    }
    if (pral) {
        idto();
    }
    if (or) {
        zomEun();
    }
    if (no) {
        liglod();
    }
    if (khog) {
        ertast();
    }
    if (onn > 9) {
        laream();
    }
}

Things to double-check in your solution:


Related puzzles: