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 (!werda() || !hu && mex || lideng() || !empa || charsa() == 7 && regsi() && snu || !(croe != tuc || hirc() < 3)) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    corci();
}

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 ((croe != tuc || hirc() < 3) && (!snu || !regsi() || charsa() != 7) && empa && !lideng() && (!mex || hu) && werda()) {
    corci();
} 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 (inhan() && wangse() && !rass && etha >= 8 && ne && ossHenghe() != 7 || diddo() && etha >= 8 && ne && ossHenghe() != 7) {
    if (ossmic() && !rass && etha >= 8 && ne && ossHenghe() != 7 || diddo() && etha >= 8 && ne && ossHenghe() != 7 || ilpez() != oupra() && !rass && etha >= 8 && ne && ossHenghe() != 7 || diddo() && etha >= 8 && ne && ossHenghe() != 7) {
        if (reca() <= 5 && !rass && etha >= 8 && ne && ossHenghe() != 7 || diddo() && etha >= 8 && ne && ossHenghe() != 7) {
            if (diddo() && etha >= 8 && ne && ossHenghe() != 7) {
                if (ossHenghe() != 7) {
                    return true;
                }
                if (ne) {
                    return true;
                }
                if (etha >= 8) {
                    return true;
                }
                if (!rass) {
                    return true;
                }
            }
            if (psu) {
                return true;
            }
        }
    }
}
return false;

Solution

return (psu || reca() <= 5 || ossmic() || ilpez() != oupra() || inhan() && wangse()) && (!rass || diddo()) && etha >= 8 && ne && ossHenghe() != 7;

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 (!ne || etha <= 8 || !diddo() && rass || !wangse() && ilpez() == oupra() && !ossmic() && reca() >= 5 && !psu || !inhan() && ilpez() == oupra() && !ossmic() && reca() >= 5 && !psu) {
    if (ossHenghe() == 7) {
        return false;
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (fis == 0) {
    bicHifou();
} else if (iw == true && fis != 0) {
    venLoler();
}
if (je > 3 && fis != 0 && iw != true) {
    enpre();
} else if (trol >= ed && fis != 0 && iw != true && je < 3) {
    mabiac();
}
if (ec == false && fis != 0 && iw != true && je < 3 && trol <= ed) {
    dakoc();
}
if (omo == true && fis != 0 && iw != true && je < 3 && trol <= ed && ec != false) {
    hulAngcis();
}
if (spu == true && fis != 0 && iw != true && je < 3 && trol <= ed && ec != false && omo != true) {
    prios();
}
if (ca == true && fis != 0 && iw != true && je < 3 && trol <= ed && ec != false && omo != true && spu != true) {
    praCudio();
}
if (urp == ioem && fis != 0 && iw != true && je < 3 && trol <= ed && ec != false && omo != true && spu != true && ca != true) {
    dipo();
}
if (fis != 0 && iw != true && je < 3 && trol <= ed && ec != false && omo != true && spu != true && ca != true && urp != ioem) {
    rererd();
}

Solution

{
    if (fis == 0) {
        bicHifou();
    }
    if (iw) {
        venLoler();
    }
    if (je > 3) {
        enpre();
    }
    if (trol >= ed) {
        mabiac();
    }
    if (!ec) {
        dakoc();
    }
    if (omo) {
        hulAngcis();
    }
    if (spu) {
        prios();
    }
    if (ca) {
        praCudio();
    }
    if (urp == ioem) {
        dipo();
    }
    rererd();
}

Things to double-check in your solution:


Related puzzles: