In older languages like C and C++ conditionals like if statements did not
just operate on booleans.... for example you could do something like if(45).
This meant that if you mistyped == as = you'd end up with something like:
if(myString = null)
instead of:
if(myString == null)
now in C and C++ the mistyped one, if(myString = null) will ALWAYS evaluate
to true because the assignment of null to myString is successful so you have
now changed myString to null instead of doing a simple test to see if it was
null.
To get around this people used to put null first. This is because null is
fixed and so cannot be changed. If you typed:
if(null = myString) instead of if(null == myString) the compiler would give
an error as you can't assign anything to null... null is null and that's that.
Newer languages like C# don't have this problem because conditionals only
work on boolean expressions and assignment isn't a boolean expression. If you
see it in code it's a good indication that the person who wrote it comes from
a C/C++ background.
Hope this answers your question
Brian Delahunty
Ireland
http://briandela.com/blog
"qu*******@resolutionsnet.co.uk" wrote:
This is not really a problem, but I am curious to know why.
Is this just a programming style, or are there any performance /
compilation gains from writting comparsion differently;
if ( null == TimeOfDay )
if ( TimeOfDay == null )
I've been seeing a lot a mixed code simular to this and I just want to
know if there is a particular reason for it.
Thanks for any insights