views:

323

answers:

7

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?

+7  A: 

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.

MatrixFrog
Wanted to make an answer but you already said what i wanted to say. So - i just upvoted yours. :)
Arnis L.
+14  A: 

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
> So if(null == var) is a defensive practiceI 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
+1 It comes from C and seems confirmed by those with other language backgrounds who failed to find any reason of it.
stefanB
A: 

var == null is the obvious answer. Why would you check null, if you already know what null is? You have to have something to check, before you can check it, so logical ordering is "object == somevaluewearecheckingfor".

David Anderson
"Why would you check null, if you already know what null is?" -- You don't check null, you check if an equality statement holds. You also miss the whole C/C++ defensive programming technique point.
foljs
my only point was that for readability it only makes sense to do var == null. you cant count how many apples you have if you don't have any
David Anderson
A: 

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

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.

tvanover
A: 

Language-agnostic?

In Scheme, no ambiguity:

(null? var)
leppie
A: 

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
I'm starting to get tired of people who downvote without commenting why.
lc