2

Dupe: Null Difference

A lifetime ago I came across an article that explained that the following were not equal (in c#):

if (o == null) {}
if (null == o) {}

The article explained that the latter was preferred because it resulted in a more accurate test. I've been coding like that ever since. Now that I understand so much more I was looking for the article, or another like it, to see what the exact findings were, but I can't find anything on the subject.

Thoughts? Is there a difference? First glance would say no. But who knows what happens in the bowels of IL and C# compilation.

Community
  • 1
  • 1
Tony Basallo
  • 3,000
  • 2
  • 29
  • 47
  • 1
    Re: "But who knows what happens in the bowels of IL and C# compilation." -- Anyone with a copy of reflector? – GEOCHET Jan 08 '09 at 16:18
  • ;) I thought that was for when one wanted see how they looked from the codes point of view... I saw no diff, I even did performance tetsing with no diff. That's why I asked. I prefer if (o==null)..it's more readable to me. Now, I can use it again. :) – Tony Basallo Jan 08 '09 at 16:26
  • checked with reflector, "ldnull, ldarg.1, ceq, ldc.i4.0, ceq" compared to "ldarg.1, ldnull, ceq, ldc.i4.0, ceq" – Jimmy Jan 08 '09 at 16:26
  • If the IL is equivalent, then why on earth would it matter? – GEOCHET Jan 08 '09 at 16:37
  • That's why I asked - it didn't make sense. Perhaps I was missing something. Memories play a terrible trick on one and I thought there was some really good reasoning behind it - it was a c# exmaple after all. – Tony Basallo Jan 08 '09 at 16:50
  • Duplicate: [http://stackoverflow.com/questions/302701/null-difference](http://stackoverflow.com/questions/302701/null-difference) – Sunny Milenov Jan 08 '09 at 16:13

5 Answers5

9

it's an old habit to prevent you from typing if (o = null). if (null = o) is a syntax error. kind of pointless in C#, because null values aren't ever coerced into booleans.

Jimmy
  • 89,068
  • 17
  • 119
  • 137
3

The latter is a holdover from the C/C++ days, where it was possible to accidentally assign a value instead of compare. C# won't allow you to do this, so either/or is acceptable (but I find the former more readable).

Bullines
  • 5,626
  • 6
  • 53
  • 93
2

In c# there's no difference. It's an old habit from C/C++ developers to avoid a common bug where this is correct syntax:

if(o = null)

In C# that wouldn't compile but in C and C++ it would, leaving you with a pretty nasty bug. Therefore, many people got used to doing

if(null == o)
BFree
  • 102,548
  • 21
  • 159
  • 201
0

Irrelevant for C#, this is from C. See this question for a discussion.

Community
  • 1
  • 1
orip
  • 73,323
  • 21
  • 116
  • 148
0

I work with Java ..and I do have the habit of having constants on the LHS for all commutative comparisons.

"name".equals(name)

 null == obj

"55".compareTo(numString)

etc..Just avoiding unnecessary NullPointerExceptions...

Prasanna S
  • 316
  • 2
  • 2
  • For string literals (i.e. the first and third), that makes sense. For the middle one, it's an unnecessary readability penalty. – Jon Skeet Jan 08 '09 at 16:25