1

Possible Duplicate:
What is wrong in comparing a null with an object rather than an object with a null

I see some developers using the following null object checking in C#:

if (null == myObject)
{

}

rather than:

if (myObject == null)
{

}

I prefer the second statement, since it reads naturally (for me) from left to right.

Is there any reason for why the first one would be used? Are there any performance benefits, or is it purely taste?

Community
  • 1
  • 1
Rebecca
  • 13,914
  • 10
  • 95
  • 136
  • 4
    None whatsoever. I do prefer the second myself. – Dimitri Jun 26 '12 at 13:32
  • 1
    There is a similar issue that does matter. myString.equals("") as opposed to "".equals(myString); The second one insures that you are never trying to call a function on a null variable. myString can be null and crash in the first one, but equals(null) will not cause a crash – Frank Sposaro Jun 26 '12 at 13:41
  • I've heard these referred to as "yoda conditions" (as in "null, my object is") :) – Steven Doggart Jun 26 '12 at 13:48
  • I disagree with the duplicate tag. This question was specific to C# and to null checking. – Rebecca Jun 26 '12 at 15:40
  • I have now located a more appropriate duplicate. – Rebecca Jun 26 '12 at 20:33

3 Answers3

13

Some people (Mostly C developers) prefer the first way because if you forget one = sign the code wont compile in C.

For example, when i forget one =;

int a = 0;

if(a=1) //Accidental assignment, luckily the C# compiler warns us for this. The C compiler wouldnt.
{

}    
if(1=a) // This is not logical, and not valid in either C# or C.
{

}

However as Jamietre pointed out that unlike C its invalid in C# to implicitly cast an int to a boolean. The compiler still produces an error. It will however work when you compare booleans as such: if(a == true). However that in itself is rather odd, as you can (and should in my opinion) omit the == true.

TJHeuvel
  • 12,403
  • 4
  • 37
  • 46
  • 4
    C# won't compile either one. Given that, I think putting the thing you are testing first is better in C# since it's a more natural way to read the code for most people and there are no risks. (But of course either is perfectly valid... comes down to preference.) – Jamie Treworgy Jun 26 '12 at 13:34
  • Well no, in C# the first one won't compile either. The compiler will stop with a "Cannot implicitly convert type 'int' to 'bool'" on that line. – Karl-Johan Sjögren Jun 26 '12 at 13:37
  • OK, for this example it won't compile, but if a were declared as a bool and he forgot an equals sign e.g. 'if (a=true)' then it would compile. Edit: Aha, @TJHeuvel has made that exact point! – Matt Hogan-Jones Jun 26 '12 at 13:38
  • @Matt Jones, who does that? `if (a) { }` – Jamie Treworgy Jun 26 '12 at 13:38
  • @jamietre To quote Rutger Hauer, I've seen things you wouldn't believe! If there's a way to write bad code, someone somewhere will find it. – Matt Hogan-Jones Jun 26 '12 at 13:39
  • haha, fair enough! Anyway I tend to code in a way that is easiest to read first. I'm not a fan of awkward-feeling constructs that are used primarily with the intent of avoiding bugs, unless there's a really compelling/common case. I can't remember a single time that I both explicity tested a bool, and forgot the 2nd equals! It must come up, but not enough for me to want to switch everything backwards. – Jamie Treworgy Jun 26 '12 at 13:41
  • If possible, write code that reads from left to right like natural language. Period. If your code like yoda speak looks it will be harder for the next guy it to read. – Anders Forsgren Jun 26 '12 at 13:41
  • If bugs you cannot avoid, backwards make your evaluations. – Jamie Treworgy Jun 26 '12 at 13:43
  • @AndersForsgren i dont always agree, for example most functional languages dont use infix notation for operators (`+ 1 2` instead of `1 + 2`). At first i found this very confusing, however i actually prefer it now. – TJHeuvel Jun 26 '12 at 13:43
  • hence the *if possible* remark of course. if you have a choice with no other drawbacks, write code that reads naturally. In C for example you actually have a drawback of a==1 vs 1==a, so you have to weigh the pros and cons. Having functional/non-infix notations for operators (when used) in code feels smelly, but that is a separate question. "Math" should read like math on paper, same thing as english. – Anders Forsgren Jun 26 '12 at 13:49
2

Purely taste. They both will return exactly the same thing.

eazimmerman
  • 599
  • 4
  • 20
0

Pure taste, same as with comas, brackets etc.

cichy
  • 10,464
  • 4
  • 26
  • 36