Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does org.apache.commons.lang.BooleanUtils.isTrue(Boolean bool) use the ternary operator?

I F3'd into this for no particular reason, and was surprised to see this method implemented as follows:

public static boolean isTrue(Boolean bool) {
        if (bool == null) {
            return false;
        }
        return bool.booleanValue() ? true : false;
    }

Why not?

public static boolean isTrue(Boolean bool) {
        if (bool == null) {
            return false;
        }
        return bool.booleanValue();
    }

It doesn't really matter, so I wondered is there some benefit to it? Readability is a weak enough argument, I consider this to be noise. Unless there is some other benefit that I am missing.

like image 772
HellishHeat Avatar asked Oct 11 '12 09:10

HellishHeat


People also ask

Why use BooleanUtils isTrue?

The isTrue static method of the BooleanUtils class can be used to check if the passed Boolean value is equal to true .

What is BooleanUtils?

public class BooleanUtils extends Object. Operations on boolean primitives and Boolean objects. This class tries to handle null input gracefully. An exception will not be thrown for a null input. Each method documents its behavior in more detail.


1 Answers

I can't find any justifiable reason.

The API specification for Java says:

public boolean booleanValue()

Returns the value of this Boolean object as a boolean primitive.

So if our bool object is:

Boolean bool = new Boolean(true);

This is equivalent:

bool.booleanValue() == true;

The ternary comparator here is redundant and decrease the readability.

like image 109
Felipe Fernández Avatar answered Oct 07 '22 02:10

Felipe Fernández