4

Possible Duplicates:
(0 == variable) or (null == obj): An outdated practice in C#?
Why does one often see “null != variable” instead of “variable != null” in C#?

I have seen many times people evaluating null to a variable instead of evaluating variable to a null.

if(null== user)

instead of

if(user==null)

I know both are trying to achieve the same functionality.So,Is it some standard or just pure personal preference. Please comment.

Community
  • 1
  • 1
Rohit Raghuvansi
  • 2,824
  • 8
  • 46
  • 74

10 Answers10

22

user == null is human speak.

null == user is Yoda speak.

To the compiler they are the same, so pick the one you are most comfortable with.

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
6

No, no difference.

You use the former because sometimes, in some languages, accidental assignment can result in 'true' (JavaScript, for example).

So you prefer to write the constant on the left, so that it's a compile time (if you have it, JavaScript obviously doesn't) error, or at best, a runtime error, instead of just a bug.

Noon Silk
  • 54,084
  • 6
  • 88
  • 105
6

I think that null == user is an best practice in C/C++ world.

It prevents typos like:

if (user = null) {

to happening, since they pass silently and they are very dangerous. Using null = user is safer in C/C++ (but not in C# since the compiler will complain) since the compiler cannot compile it.

dfa
  • 114,442
  • 31
  • 189
  • 228
5

if(user==null) is easier to read

ozczecho
  • 8,649
  • 8
  • 36
  • 42
5

As far as I know...

null == object would be a reference comparison. object == null may be overloaded.

  • Good point, I was just going to mention overloading. null == object can also be overloaded though: `public static bool operator ==(object a, Person b)` – Kobi Aug 12 '09 at 07:59
  • If all overloads have the same type in left and right operand, so say there's an overload `operator ==(User a, User b)`, but no overloads of `==` where the left and the right operands have distinct types, then `null == obj` will hit the same overload as `obj == null`. This is by far the most common situation. A "heterogenous" overload like in @Kobi's comment is rare and a bit "pathological". – Jeppe Stig Nielsen May 03 '13 at 21:52
3

it comes from C

You could easily write if(user=null) by mistake with only one = sign which instead of comparing assigns null to user and then tests the value of null.

If you wrote it as "if (NULL == user)" then you got a compiler error if you accidentally only wrote one =

I never liked it, it was as mistake I rarely made, and if I did it usually got caught first time I ran the program. But it made code in my opinion MUCH harder to read.

In c# there is no reason other than habit.

jcoder
  • 29,554
  • 19
  • 87
  • 130
1

No, both are equal

rahul
  • 184,426
  • 49
  • 232
  • 263
1

No deference between the two statements, they are equal. And that's related to the way that the developer used to write the code.

Wael Dalloul
  • 22,172
  • 11
  • 48
  • 57
  • Not the same if (==) is overridden by the user class! – Jerry Nixon Jul 16 '12 at 19:37
  • @JerryNixon-MSFT The user-defined overload must be really sick for the two to behave differently. But I can write a sick one like `public static operator ==(User a, User b) { return object.ReferenceEquals(b, null); }`. My operator looks only at its right operand, and disregards the left operand entirely. – Jeppe Stig Nielsen May 03 '13 at 21:57
0

It just makes more logical sense to do:

if (user == null)

as it is the user you are evaluating, you are just using null as a comparison.

James
  • 80,725
  • 18
  • 167
  • 237
0

They achieve the same. Personally I have never seen null == user in any of my projects, but if I should render a guess, I'd say it's connected to how you read your every day language. If you are from Europe, odds are you read from left to right, but some languages read from right to left, that may be where it comes from.

Jesper Fyhr Knudsen
  • 7,802
  • 2
  • 35
  • 46