1

NOTE: I went through these but didn't get my answer Best way to check for null values in Java? and (obj == null) vs (null == obj)?

I was studying this Android [Java] official documentation on Background Tasks -> Run code on a thread pool thread -> Interrupt running code, and the null check code in the sample is:

if (null != thread) {
    thread.interrupt();
}

which is different from what we usually see/use:

if (object != null) {
    //do something;
}

So, my question is that:

Does it make any difference (like helping avoid null pointer or something) if we write "null != thread" instead of "thread != null" or Google Official documentation is just randomly swapping the operands without any benefit?

EDIT:

  1. I am asking about != and not ==. In case of ==, the programmer may do assignment(=) instead of comparison(==). But that is not the case in !=.
  2. I am talking about Java and not C language. So, that assignment and comparison confusion doesn't apply here.
Suryakant Bharti
  • 673
  • 1
  • 6
  • 24
  • this is a very strange observation and also a quite odd question :) but i don't think there's any difference. – a_local_nobody Jan 30 '20 at 09:51
  • 2
    Note that `==` and `!=` are symmetric relations, so order does not make any difference. – Gyro Gearless Jan 30 '20 at 09:56
  • @a_local_nobody actually, when I read a program which is written by a better (my presumption since author works for google here) programmer, and I see any different style of coding, I wonder that I maybe doing it wrong... – Suryakant Bharti Jan 30 '20 at 09:58
  • 1
    @GyroGearless that is probably worth an answer :D – a_local_nobody Jan 30 '20 at 09:59
  • 2
    Also note that the usage `null != foo` is sometimes preferred by seasoned C programmers, where it was easy to a accidentally write `foo=null` (an assignment) instead of `foo==null` (an comparison). There is no strict reason to do the same in java. – Gyro Gearless Jan 30 '20 at 10:00
  • @GyroGearless Actually, I thought that it might make a difference in preventing NullPointException in some cases, since condition evalution process in Java is always left to right... – Suryakant Bharti Jan 30 '20 at 10:01
  • @GyroGearless ya, I read about that "accidental assignment" instead of "comparison" thing in the other answer. – Suryakant Bharti Jan 30 '20 at 10:03
  • @Oo.oO Hmmm... maybe that is the programmer's (author of the above documentation) pattern of writing. – Suryakant Bharti Jan 30 '20 at 10:05
  • @OleV.V. Good point to note. Thanks – Suryakant Bharti Jan 30 '20 at 10:21
  • There are so many good answer to the linked original question. You’ll learn much more from reading those than from reading the correct and helpful comment by @GyroGearless turned into an answer here. I think that closing as a duplicate was meant to help you. Do you think that asking about `!=` makes it a (very) different question from the one about `==`? – Ole V.V. Jan 30 '20 at 10:25
  • I understand that there are good answers there. But, I think it makes difference because for ==, most answer only talk about accidental assignment instead of comparison. – Suryakant Bharti Jan 30 '20 at 10:27
  • For the record, this question was previusly closed as a duplicate of [(obj == null) vs (null == obj)?](https://stackoverflow.com/questions/6883646/obj-null-vs-null-obj). It has been reopened after a request from the questioner since the core of the explanation for `==` doesn’t make much sense for `!=`. – Ole V.V. Jan 30 '20 at 15:02

1 Answers1

3

No semantic difference

if (null != thread) {
    thread.interrupt();
}

Why the otherwise bright folks at Google wrote the null check in this backward way remains guesswork. I agree with you that it is harder to read than the natural thread != null.

There is no gain whatsoever from the backward writing. The semantics are exactly the same.

Backward writing of an if condition is known as a Yoda condition. Yoda conditions are debated, some say they have their place in some programming languages (like C) under some circumstances. In case of == rather than != for comparison you can read some of the arguments in the previously linked duplicate (I repeat the link at the bottom). In Java one might have a sought-after argument in a case like the following:

boolean b;
if (b == false) {
    // Do something
}

Imagine that a programmer coming from Pascal or another programming language that uses single = for comparison and putting single = here by mistake. It would change the meaning to assigning false to b and never executing the conditional code. So the argument goes that by writing false == b the compiler will catch such a mistake for you, which is a clear advantage. However:

  1. Most compilers and IDEs do catch the mistake anyway. For example if I put a single = in the above if statement, my Eclipse says

    Possible accidental assignment in place of a comparison. A condition expression should not be reduced to an assignment

  2. The if statement would usually be written in the following way, which excludes any meaningful debate about a Yoda condition and is also generally recommended:

    if (! b) {
        // Do something
    }
    

So going back to your question my guess is: A programmer at Google had the habit of writing Yoda conditions like null == thread and by false analogy extended it to null != thread too.

So does the order of != operands never carry any semantics?

For the sake of completeness, when I say that thread != null and null != thread have the same semantics, I am talking about this quoted code. In case of for example someObj.foo() != anotherObj.bar() the order imposes an order of the two method calls. If both methods have side effects that interfere somehow, the order of applying those side effects may make a difference in some cases.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161