Terseness in Boolean Expressions

You may or may not be doing all this extra work where your code needs a Boolean expression. The examples here apply to many non-Java languages, too.

Terseness in Boolean Expressions

Have you seen or written code like this?

while (isAdvanced == true && isPremium() == false) {
...
}

Or this?

if (isAdvanced) then {
    return true;
}
return false;

Or this?

return isAdvanced ? true : false;

How about this?

boolean isBasicUser = isPremium() == false ? true : false;

While they will all compile and work as you expect, they could certainly be shortened. Your code reviewers would appreciate terseness. The less time they spend on your pull request, the happier they are.

The key thing to note here is that Boolean variables or Boolean-returning methods are already Boolean expressions themselves. There's no need to compare them with true or false. That would be redundant.

In the first example, the variable "isAdvanced" is equivalent to "isAdvanced == true" and the return value of isPremium() can be negated to become equivalent to "isPremium() == false".

With that said, the first example could be shortened to:

while (isAdvanced && !isPremium()) {
...
}

I don't know about you, but to me, that's so much more readable. It's closer to English (or whatever language you choose to use for your variable names).

The next two examples would both collapse to:

return isAdvanced;

Finally, the last example would just be:

boolean isBasicUser = !isPremium();

There you go.

boolean happyCodeReviewer = isShort(pullRequest);