0

This question already has an answer here:

Which of these two is the better way of doing the same thing -

public void someMethod(String s, boolean bool02){
 boolean bool01 = s!=null;
 if(bool01==bool02){
  doSomething();
 }
}

OR

public void someMethod(String s, boolean bool02){
 List<String> list=new ArrayList<String>();
 if(s!=null && bool02){
  doSomething();
 }
}

The way I understand it is, Option 1 -

  1. Computes s!=null
  2. Set it to bool01
  3. Compares bool01 to bool02

and Option 2 -

  1. Computes s!=null
  2. Compares it with true
  3. Compares bool02 to true(depending if step 2 was true)

This is not a big deal but its bugging me to know which one is better. Or does the compiler optimization(if any) converts both of them to the same thing?

marked as duplicate by gnat, maple_shaft Jul 15 '17 at 12:05

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 4
    These kind of optimizations are pointless. Do what makes the code clearer to understand and verify correctness. – whatsisname Jul 14 '17 at 23:39
  • 9
    The two pieces of code are not equivalent. Consider when s is null and bool02 is false. – DisgruntledGoat Jul 15 '17 at 0:43
  • What's with the list creation? That should slow things down if it is not optimized away. The intermediate assignment to bool01 is likely to be optimized away but if you do not care about it, get rid of it too, you want to know only the difference between AND and EQUALS, right? Now Change doSomething to something trivial and fast like increment an integer and add an else clause that does the same. Then you are good to perform a simple test. Wrap both bodies in a loop and iterate like 100000000 times. Et voila. – Martin Maat Jul 15 '17 at 4:43
  • 1
    Your two example do different things. In first example doSomething will be executed when s is not null and bool01 is true and when s is null and bool01 is false. Where in second example doSomething will be executed only when s is not null and bool01 is true. – Fabio Jul 15 '17 at 6:04
11

They are both horrible.

The better one is the one that actually does what it's supposed to do. These don't do the same thing.

s!=null is bool01  |  bool02  |  bool01==bool02  |  s!=null && bool02
        F          |    F     |        T         |          F
        F          |    T     |        F         |          F
        T          |    F     |        F         |          F
        T          |    T     |        T         |          T

Which one does what it's supposed to do is impossible to say because you've removed any hint of what's going on with meaningless names like someMethod(), doSomething() and, my good god, bool01. This is the biggest problem.

public void printLength(String s, boolean allowException) {
    if (s != null || allowException) {
        System.out.println( "String length : " + s.length() );
    }
}

There, now it means something. I can look at it without my head hurting.

For future reference the logical equivalent of == isn't &&. It's NOT XOR

       ^  !^ == &&
F  F   F  T  T  F
F  T   T  F  F  F
T  F   T  F  F  F
T  T   F  T  T  T

Now sure, I could make printLength() occasionally slightly faster by putting allowException first in the OR and use short circuit evaluation to avoid the subtraction required to compare the s reference to null. I don't care. Not because this is only a tiny savings, but because I think it's more readable to see the null check first before thinking about allowing the exception.

The only performance anyone should care about here is how this performs in your head.

  • 1
    +1 for " meaningless names like someMethod(), doSomething() and, my good god, bool01" . Often times foo, bar, myclass, myParameter, and the like, makes it impossible to understand the problem. – Tulains Córdova Jul 15 '17 at 18:27
  • 1
    @TulainsCórdova the most terrifying part of bool01 was the zero. Please tell me I won't be working on code with another 99 of these names. Whaa! :) – candied_orange Jul 15 '17 at 18:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.