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 (!(!fi && (!caee || mimu)) || (!un || je != gect || moish()) && (creer() || !vu)) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    cathja();
}

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 ((vu && !creer() || !moish() && je == gect && un) && !fi && (!caee || mimu)) {
    cathja();
} 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 (trel() && ic && !cet || !frum || !thes && hewiam() <= pu || fomMuc() && ic && !cet || !frum || !thes && hewiam() <= pu || streco() && ic && !cet || !frum || !thes && hewiam() <= pu) {
    if (!thes && hewiam() <= pu) {
        if (!frum) {
            if (!cet) {
                return true;
            }
        }
    }
    if (repo) {
        return true;
    }
}
return false;

Solution

return (repo || (trel() || fomMuc() || streco()) && ic) && (!cet || !frum || !thes && hewiam() <= pu);

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 (!ic && !repo || !streco() && !fomMuc() && !trel() && !repo) {
    if (thes && frum && cet) {
        if (cet) {
            return false;
        }
        if (frum) {
            return false;
        }
        if (hewiam() >= pu) {
            return false;
        }
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (bi) {
    antsi();
} else if (gi == false && !bi) {
    hadnir();
}
if (ca == true && !bi && gi != false) {
    dipik();
}
if (ueng == false && !bi && gi != false && ca != true) {
    bontpi();
}
if (ies == true && !bi && gi != false && ca != true && ueng != false) {
    cudaip();
} else if (odu == true && !bi && gi != false && ca != true && ueng != false && ies != true) {
    clas();
} else if (prir && !bi && gi != false && ca != true && ueng != false && ies != true && odu != true) {
    napind();
}
if (!bi && gi != false && ca != true && ueng != false && ies != true && odu != true && !prir) {
    pher();
}

Solution

{
    if (bi) {
        antsi();
    }
    if (!gi) {
        hadnir();
    }
    if (ca) {
        dipik();
    }
    if (!ueng) {
        bontpi();
    }
    if (ies) {
        cudaip();
    }
    if (odu) {
        clas();
    }
    if (prir) {
        napind();
    }
    pher();
}

Things to double-check in your solution:


Related puzzles: