6

What is the difference between

if(null==object)

and

if(object==null)

Please give the advantage for using the above.

Greg
  • 316,276
  • 54
  • 369
  • 333

7 Answers7

12

The difference comes if you accidentally type = instead of ==:

if (null = object) - Compiler error
if (object = null) - Bug!

Greg
  • 316,276
  • 54
  • 369
  • 333
6

In the good old days, compilers would happily let you make assignments inside conditionals, leading to unintentional errors:

if(a = false)
{
  // I'll never execute
}
if(b = null)
{
  // I'll never execute 
}
b.Method(); // And now I'm null!

So some clever developers started putting their constants first in their conditionals:

if(false = a) // OOPS! Compiler error
{
  // ..
}
if(null = b) // OOPS! Compiler error
{
  // ..
}

So they trained themselves to avoid a whole class of errors. Most modern compilers will no longer let you make that error, but the practice continues.

There is one other advantage to always putting your constants first:

if(myString != null && myString.Equals("OtherString"))
{
  // ...
}

can (in .NET, Java, and most languages with an object-based string type) be reduced to:

if("OtherString".Equals(myString))
{
  // ..
}
Brian B.
  • 1,779
  • 11
  • 10
4

Well, here is something I kind of like... use extensions:

public static class ObjectExtensions
{
    public static bool IsNull(this object target)
    {
        return null == target;
    }
}

Now, you can forget about it completely:

if(item.IsNull())
Brian Genisio
  • 47,787
  • 16
  • 124
  • 167
  • I don't like that. I think is going a much too far with extensions. Also, I don't like calling static methods on member variables. And then it also requires a double take to see someone calling a method on a variable that presumably may be null. ... But it does work I suppose. – BobbyShaftoe Jan 09 '09 at 01:32
3

No difference. (null == object) is a practice from C/C++, where "=" is both used as assignment operator, and as comparison operator.

There were too many errors when if (object = null) was used.

Sunny Milenov
  • 21,990
  • 6
  • 80
  • 106
  • I have no connection with this post, but why someone just down voted it? I agree that it is duplicating the opinions but then instead of down voting person should just keep quite. – Pradeep Nov 19 '08 at 17:33
  • because the answer as originally submitted was neither complete nor helpful - "No difference" isn't enough :) – warren Nov 19 '08 at 17:34
  • It's not a dup at all (the first 3 answers, including this one) appeared at the same time. And different explanations. Anyway, I just hate when something id downvoted w/o reason. I always put a comment when I downvote. – Sunny Milenov Nov 19 '08 at 17:36
  • Get over it, Sunny. In the end you'll earn +8 pt, because someone (maybe me) will upvote your answer ;-) – splattne Nov 19 '08 at 17:37
  • "No difference" was more than enough for smart ones. What was the need to write Julius Caesar there? – Pradeep Nov 19 '08 at 17:38
  • splattne: it was not about me or about my answer. I post such a comments even on someone else's answers when they are downvoted w/o reason. – Sunny Milenov Nov 19 '08 at 17:40
  • because he asked for "why" to use one over the other... so it wasn't a complete answer – warren Nov 19 '08 at 17:40
  • warren: question in 2 parts - answer in 2 parts :). I read the second question later. – Sunny Milenov Nov 19 '08 at 17:42
  • hehe - but it was only two lines! =D – warren Nov 19 '08 at 17:51
  • what to tell you, small buffer :) – Sunny Milenov Nov 19 '08 at 18:00
2

Some prefer if (null == object) so that if you accidentally type = instead of ==, you get a compile error instead of an assignment.

bdukes
  • 152,002
  • 23
  • 148
  • 175
2

Logically, there is no difference.

From an error checking point of view, the first is more desirable because if you miss an equals sign (=), the compiler will let you know you can't make an assignment to a constant.

warren
  • 32,620
  • 21
  • 85
  • 124
0

In many languages == is the comparison operator = is the assignment operator.

It very easy to type = when you really mean ==.

Therefore the convention of typing constant==variable is preferred.

constant=variable will not compile thus showing you, your error.

variable=constant will compile and will do the wrong thing at runtime.

morechilli
  • 9,827
  • 7
  • 33
  • 54