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 (agh || !en && !da && sucKnul() || prac || pla < pris() || cissi() && ve) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    massac();
}

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 ((!ve || !cissi()) && pla > pris() && !prac && (!sucKnul() || da || en) && !agh) {
    massac();
} 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 (troa && !mo && ed && cae || awgla() == am || a && ed && cae || awgla() == am || psa && !mo && ed && cae || awgla() == am || a && ed && cae || awgla() == am) {
    if (!pa) {
        return true;
    }
    if (cian()) {
        return true;
    }
}
return false;

Solution

return cian() && !pa || (troa || psa) && (!mo || a) && (ed && cae || awgla() == am);

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 (!psa && !troa && pa || !cian()) {
    if (!a && mo && pa || !cian()) {
        if (!ed && pa || !cian()) {
            if (!cian()) {
                if (pa) {
                    return false;
                }
            }
            if (!cae) {
                return false;
            }
        }
        if (awgla() != am) {
            return false;
        }
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (vass == false) {
    cacQer();
}
if (he && vass != false) {
    kephe();
}
if (qel == true && vass != false && !he) {
    houan();
}
if (e == chio && vass != false && !he && qel != true) {
    foner();
}
if (ae == false && vass != false && !he && qel != true && e != chio) {
    eanhol();
}
if (igna == true && vass != false && !he && qel != true && e != chio && ae != false) {
    ollSpoud();
} else if (tred != ti && vass != false && !he && qel != true && e != chio && ae != false && igna != true) {
    cacor();
}
if (ec <= 2 && vass != false && !he && qel != true && e != chio && ae != false && igna != true && tred == ti) {
    sebi();
}

Solution

{
    if (!vass) {
        cacQer();
    }
    if (he) {
        kephe();
    }
    if (qel) {
        houan();
    }
    if (e == chio) {
        foner();
    }
    if (!ae) {
        eanhol();
    }
    if (igna) {
        ollSpoud();
    }
    if (tred != ti) {
        cacor();
    }
    if (ec <= 2) {
        sebi();
    }
}

Things to double-check in your solution:


Related puzzles: