8

About this function: org.apache.commons.lang3.BooleanUtils.isFalse(Boolean bool) and the similar isTrue, my co-work (less experienced) use it for every boolean in the code. I am trying to convince him that it is not a good practice because make the code confusing. In my POV, it should be used ONLY if the boolean to be tested is a type of non-primitive Boolean and it can be null. Actually, even this I think it is unnecessary, because the implementation of this function is simply Boolean.TRUE.equals(bool) or Boolean.FALSE.equals(bool). Anyway, I think it is totally crazy do something like:

boolean isReady = true;

if (BooleanUtils.isTrue(isReady)) {
   // ...
}

when you cold simply do

if (isReady) {
   // ...
}

or (!isReady) for the opposite.

His only argument to use this is "it is easy to read". I just can't accept this argument. Am I wrong? What arguments can I use to convince him that no useless code is better than useless code in this case?

Thank you guys.

Felipe
  • 329

3 Answers3

11

It's absurd to use BooleanUtils.isTrue(isReady) when isReady is a boolean, not a Boolean. Your colleague seems like one of those guys who thinks that the more code he writes, the more productive he is. Maybe he should go find a job programming in Ada.

kevin cline
  • 33,798
3

1) Bugs come from code. The more code you have, the more bugs. Always try to write less code, even if that's just a few words here and there.

2) Easy isn't Easiest. Which is easiest to read?

Try compromising with if(isReady == True), if he really wants that explicit comparison.

1

I think the goal should be to have descriptive boolean variable names, as you have shown in your example.

Dan Teesdale
  • 119
  • 3