4

Possible Duplicate:
Conditional styles: if (0 == resultIndex) vs if (resultIndex ==0)

I've seen both in code, and I do not know why one is better over the other, but which do you prefer to use, and why?

Community
  • 1
  • 1
esac
  • 24,099
  • 38
  • 122
  • 179
  • I know this is a duplicate but it's bloody hard to search on this! – Matt Hamilton May 22 '09 at 06:32
  • I didn't see it in my searches or suggestions either, if so I apologize. – esac May 22 '09 at 06:39
  • Here is one.. It has other duplicates list. http://stackoverflow.com/questions/892284/conditional-styles-if-0-resultindex-vs-if-resultindex-0-closed – aJ. May 22 '09 at 06:46
  • My first programming language was C and I made that error few times before I learned what I'm actually doing. However I don't use the reversed form as I don't make that mistake that often and for me it (var == null) is more readable. But definitely you can get burned in C if you write "if (var = null)". – stefanB May 22 '09 at 06:50
  • I was one of the persons who closed the other question too :). But seriously SO search needs to be improved. – aJ. May 22 '09 at 06:55

6 Answers6

17

The if(null == var) practice comes from C, where you could accidentally write if(var = null) (note the assignment operator), which would compile (since in C everything not 0 is true) but will be a semantic error. So if(null == var) is a defensive practice, since null is an lvalue (again C/C++ terminology) and lvalues cannot be assigned to.

In modern languages (particularly in C#) compiler will warn you if you do assignment in if statement.

Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288
  • > So if(null == var) is a defensive practice I did this one quite a few times in C/C++ for as you put it defensive reasons. It seems the answer on usage may depend on what programming language you are using. – Rob Segal May 22 '09 at 06:36
  • +1 It comes from C and seems confirmed by those with other language backgrounds who failed to find any reason of it. – stefanB May 22 '09 at 06:47
8

I would say var==null because it's more like what you would actually say. "If my variable is null..." makes more sense than any variant of "If null is my variable..." or "If nothing is my variable..." etc.

Tyler
  • 21,762
  • 11
  • 61
  • 90
0

I prefer to use (var==null) myself. It makes more sense to me, since I'm checking "if var is null". The other order comes across when I read it as "if null is var", which never makes sense. I also like how VB.NET makes it quite readable, and makes a check for null different from other comparisons, with "if var is nothing".

Jimmy
  • 27,142
  • 5
  • 87
  • 100
0

They both have the same functionality and produce the same result. The choice is purely a personal one to make.

I personally prefer the first because "var is null" or "var equals null" reads better than "null is var" or "var is null".

Some people prefer the second and claim it reduces typo-induced bugs because null = var causes an error while var = null still compiles. These days, however, compilers issue warnings for this, so I believe it is a moot point.

lc.
  • 113,939
  • 20
  • 158
  • 187
-1

Typically I tend to go unknown value then known value when I am comparing two values. That way I know first what I am looking in, giving context to the test. Most languages don't care, but I think some may.

Tyson of the Northwest
  • 2,086
  • 2
  • 21
  • 34
-1

Language-agnostic?

In Scheme, no ambiguity:

(null? var)
leppie
  • 115,091
  • 17
  • 196
  • 297