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 (!((!(e != 8) || fint) && (phe == 2 || puciad()) && (ri || !vopi)) && !(di < 6) && !(ne != 5) && !bif && iaan() && !ec) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    cleith();
}

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 (ec || !iaan() || bif || ne != 5 || di < 6 || (!(e != 8) || fint) && (phe == 2 || puciad()) && (ri || !vopi)) {
    cleith();
} 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 (!uc && al || aflen() || xei && epmel() || heslis() <= ci) {
    if (maou() != 6 && arqal() && bair && arsid() || inle && arqal() && bair && arsid()) {
        if (arsid()) {
            return true;
        }
        if (bair) {
            return true;
        }
        if (arqal()) {
            return true;
        }
        if (er) {
            return true;
        }
    }
}
return false;

Solution

return (er || maou() != 6 || inle) && arqal() && bair && arsid() || !uc && al || aflen() || xei && epmel() || heslis() <= ci;

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 (!xei && !aflen() && !al && !arsid() || !bair || !arqal() || !inle && maou() == 6 && !er || uc && !arsid() || !bair || !arqal() || !inle && maou() == 6 && !er) {
    if (uc && !arsid() || !bair || !arqal() || !inle && maou() == 6 && !er) {
        if (!inle && maou() == 6 && !er) {
            if (!bair || !arqal()) {
                if (!arsid()) {
                    return false;
                }
            }
        }
        if (!al) {
            return false;
        }
    }
    if (!aflen()) {
        return false;
    }
    if (!epmel()) {
        return false;
    }
}
if (heslis() >= ci) {
    return false;
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (se == true) {
    serbi();
}
if (a == true && se != true) {
    ithEnor();
}
if (lil == true && se != true && a != true) {
    cossis();
} else if (anco == false && se != true && a != true && lil != true) {
    grepo();
}
if (edou > 7 && se != true && a != true && lil != true && anco != false) {
    mokMeel();
} else if (peou > 1 && se != true && a != true && lil != true && anco != false && edou < 7) {
    theas();
} else if (po == true && se != true && a != true && lil != true && anco != false && edou < 7 && peou < 1) {
    chuaes();
}
if (fean == true && se != true && a != true && lil != true && anco != false && edou < 7 && peou < 1 && po != true) {
    rusil();
} else if (ia == true && se != true && a != true && lil != true && anco != false && edou < 7 && peou < 1 && po != true && fean != true) {
    ipic();
}
if (cac == true && se != true && a != true && lil != true && anco != false && edou < 7 && peou < 1 && po != true && fean != true && ia != true) {
    rerPemal();
} else if (dixo && se != true && a != true && lil != true && anco != false && edou < 7 && peou < 1 && po != true && fean != true && ia != true && cac != true) {
    ptuAdblel();
}

Solution

{
    if (se) {
        serbi();
    }
    if (a) {
        ithEnor();
    }
    if (lil) {
        cossis();
    }
    if (!anco) {
        grepo();
    }
    if (edou > 7) {
        mokMeel();
    }
    if (peou > 1) {
        theas();
    }
    if (po) {
        chuaes();
    }
    if (fean) {
        rusil();
    }
    if (ia) {
        ipic();
    }
    if (cac) {
        rerPemal();
    }
    if (dixo) {
        ptuAdblel();
    }
}

Things to double-check in your solution:


Related puzzles: