views:

54

answers:

1

(EDIT: Duplicate of "Why does one often see “null != variable” instead of “variable != null” in C#?". Even if C# isn't the language in question, the answers to that question give the reason one might want to do it in some languages.)

This is a somewhat philosophical question. However,... maybe there is a technical answer. I don't know.

When reading Microsoft code (especially), I often see lines like this:

if( null != whatever )
{
}

And I was always wondering why they don't write this instead:

if( whatever != null )
{
}

Which looks way more intuitive to me. Is there any reasonable answer to this?

+2  A: 

This is a classic. The reason is to protect themselves from accidentally writing

if(whatever = null)
{
}

since that becomes a syntax error if you've flipped them around:

if(null = whatever)
{
}
unwind
... which is only a problem in some languages. See the duplicate question I've linked to in an edit.
Jon Skeet