tags:

views:

312

answers:

7
+5  Q: 

Null Difference

What is the difference between

if(null==object)

and

if(object==null)

Please give the advantage for using the above.

+12  A: 

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

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

Greg
In C# using assignment operator in place of boolean result is a compile time error.
Pradeep
that is good to know.. but the question wasn't originally marked 'C#' :D
warren
Indeed... I've removed the tag
Greg
:) Agree. It is tagged in .net. Doesn't VB.Net or managed C++ provide this feature?
Pradeep
VB uses = for both assignment and equality.
Joel Coehoorn
VB uses = for value equality. When checking for nulls or reference equality, VB uses "Is".
Jonathan Allen
+3  A: 

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
Now, be brave, and tell me why this is down voted.
Sunny
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
because the answer as originally submitted was neither complete nor helpful - "No difference" isn't enough :)
warren
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
Get over it, Sunny. In the end you'll earn +8 pt, because someone (maybe me) will upvote your answer ;-)
splattne
"No difference" was more than enough for smart ones. What was the need to write Julius Caesar there?
Pradeep
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
because he asked for "why" to use one over the other... so it wasn't a complete answer
warren
warren: question in 2 parts - answer in 2 parts :). I read the second question later.
Sunny
hehe - but it was only two lines! =D
warren
what to tell you, small buffer :)
Sunny
fwiw.. i did undo my down vote :)
warren
+2  A: 

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

bdukes
... if you're using C or C++, and haven't got warnings turned up high enough.
Jon Skeet
+2  A: 

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
A: 

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
+3  A: 

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
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
So, in essence, you just don't like extension methods...
Brian Genisio
+5  A: 

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.