3

I am using Code Pro to review my application code and the tool reported back with the message:

Warning: equality test with boolean literal

For this code:

boolean valid;
if(valid == true)

which can be fixed by using:

if(valid) 

I have 2 questions on my mind:

  1. Is it just a good coding practice ?
  2. Is there any other benefit in terms of memory optimization or performance optimization ?
James P.
  • 19,313
  • 27
  • 97
  • 155
Ashish
  • 14,295
  • 21
  • 82
  • 127
  • For the 1st question according to me yes it's a good practice. Concerning the second one I don't have any expertise but my intuition is that it should be at least as good in term of memory optimization or performance optimization – Tako Mar 23 '13 at 04:43
  • @Tako I hope you mean `if(valid)` is a good practice. Comparing boolean values against `true` is a Bad Idea. Ashish: The reasoning is simple; `2` is true, but `2==true` is false. More generally, anything non-zero is true. – Dave Mar 23 '13 at 04:46
  • 1
    If you think it's better to write `if (valid == true)`, then why not `if(!((valid == true) != false && (!valid != true)) == !true)`? – Matt Ball Mar 23 '13 at 04:47
  • `if(valid)` is definitely the way to go. Also I believe both would compile to the same byte code. Depending on your compiler of course – Breavyn Mar 23 '13 at 04:49
  • 4
    @MattBall http://en.wikipedia.org/wiki/That_that_is_is_that_that_is_not_is_not_is_that_it_it_is – Dave Mar 23 '13 at 04:49
  • 1
    Exact duplicate of [boolean checking in if()](http://stackoverflow.com/q/4282708/139010). Thanks, sidebar! – Matt Ball Mar 23 '13 at 04:52
  • @MattBall I'm not getting at you, I agree with your point. I simply thought you might appreciate the overly-complex, bizarre statement. – Dave Mar 23 '13 at 04:53
  • @Dave yes `if(valid)` is a good practice for me – Tako Mar 23 '13 at 04:54
  • Thanks. I haven't seen that one in a while. – Matt Ball Mar 23 '13 at 04:54
  • From the link @MattBall posted. Interesting answer about is prefix. http://stackoverflow.com/a/4282921/225899 – James P. Mar 23 '13 at 05:02

4 Answers4

5

For the more direct boolean expression:

boolean valid = true;

if(valid) {
}

The following byte code is generated:

0: iconst_1      
1: istore_1      
2: iload_1       
3: ifeq          6
6: return     

Whereas with the expanded comparison:

boolean valid = true;

if(valid == true) {
}

The following byte code is generated:

0: iconst_1      
1: istore_1      
2: iload_1       
3: iconst_1      
4: if_icmpne     7
7: return

I doubt ifeq and if_icmpne differ in execution speed, so the additional cost of if(valid == true) is really just the extra constant value, which is negligible.

To summarize, there is real no performance difference, and CodePro is flagging your code as a best practice alone.

Perception
  • 79,279
  • 19
  • 185
  • 195
  • My take on it is that it can help readability. `if(isValid)` reads better than `if(isValid == true)`. – James P. Mar 23 '13 at 05:04
1

I would say that it is more of a good practice. One, the code is more compact. And two, the code reads more like natural language.

I have not idea if there is any benefit one way or the other. My guess is that the Java compiler is so mature, that both options perform about the same.

Rob Breidecker
  • 604
  • 1
  • 7
  • 12
1

Is it just a good coding practice ?

Yes. Two reasons:

  • The b == true idiom is verbose and linguistically ugly. (Would you ask, "is it true that the Pope is Catholic?" or "is the Pope Catholic?" ... ?)

  • The b == true idiom is the one any only case in Java that can be miss-written as b = true.

Is there any other benefit in terms of memory optimization or performance optimization?

Almost certainly not.

When the two bits of code result in different bytecodes, it is most likely that the JIT compiler will turn them into either the same native instruction sequences, or sequences that have identical performance. (If not, then the optimizer is "missing a trick").

Either way, the difference is likely to be too small to be too small to be significant.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

There is no difference in behavior, and while I can't speak authoritatively for every JVM it is difficult to imagine that there would be any performance difference to speak of.

j__m
  • 9,392
  • 1
  • 32
  • 56