If (null == something) or if (something == null)
-
A lot of code I have come across recently has If (null == something) whereas I tend to write if (something == null) which seems to me to be much more logical and easily readable - So the question is, is there a good reason for using the former over the latter?
___________________________________________ .\\axxx (That's an 'M')
-
A lot of code I have come across recently has If (null == something) whereas I tend to write if (something == null) which seems to me to be much more logical and easily readable - So the question is, is there a good reason for using the former over the latter?
___________________________________________ .\\axxx (That's an 'M')
Hi, the origin is it protects you against a typo where you drop one equal sign; then
constant=variable
yields an error, whereasvariable=constant
may not (it wouldn't in C/C++, it most often would in C# unless the types are bool). (*) However, I am with you, it does not look good, and any decent compiler would normally (there are exceptions conceivable) generate a WARNING message, either saying: "are you sure that is what you intend?" or "condition will always be true/false". However MS compilers seem not to do so. [ADDED] (*) which is another good reason never to write things such asif (someBool==true)...
, just writeif (someBool) ...
. [/ADDED] :)Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
modified on Thursday, July 2, 2009 8:10 PM
-
A lot of code I have come across recently has If (null == something) whereas I tend to write if (something == null) which seems to me to be much more logical and easily readable - So the question is, is there a good reason for using the former over the latter?
___________________________________________ .\\axxx (That's an 'M')
The "null == something" form (or in general, " == " form), is simply not useful in C# or Java. The purpose of this form was to avoid accidentally typing an assignment rather than a comparison in C/C++. C# and Java will not let you make this mistake since the condition must evaluate to a boolean and a simple assignment will not (unless you compare the assignment to something).
David Anton http://www.tangiblesoftwaresolutions.com Convert VB to C#, C++, or Java Convert C# to VB, C++, or Java Convert C++ to C#, VB, or Java Convert Java to C#, C++, or VB
-
A lot of code I have come across recently has If (null == something) whereas I tend to write if (something == null) which seems to me to be much more logical and easily readable - So the question is, is there a good reason for using the former over the latter?
___________________________________________ .\\axxx (That's an 'M')
_Maxxx_ wrote:
is there a good reason for using the former over the latter?
There is, and AFAIK this reason comes from the good old C/C++ days. It was a very common error (the most common of all) to type
if (reason = null)
instead ofif (reason == null)
. This small little typo led to very strange program behaviour and was very hard to debug, because it's very easy to overread it when inspecting the code. The problem was that the C/C++ - Compiler didn't even issue a warning when you typed something likeif (reason = null)
, but it immediately complained aboutif (null = reason)
. So it's all about correctness and maintainability. I even saw companies with coding guidelines that dictated this style. Regards Thomaswww.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Programmer - an organism that turns coffee into software. -
A lot of code I have come across recently has If (null == something) whereas I tend to write if (something == null) which seems to me to be much more logical and easily readable - So the question is, is there a good reason for using the former over the latter?
___________________________________________ .\\axxx (That's an 'M')
Yeah, what they said. It only works when comparing an Rvalue to an Lvalue, which isn't necessarily all that frequently, and my argument against it is that if I can remember to write it that way, then I will probably not make that mistake anyway so it's fairly pointless. I worked for a company with the former specified in the company coding standard (for writing C), but by the time I left, even the "guru" (who had written the standard) had admitted that it wasn't that important and wasn't enforcing it.
-
A lot of code I have come across recently has If (null == something) whereas I tend to write if (something == null) which seems to me to be much more logical and easily readable - So the question is, is there a good reason for using the former over the latter?
___________________________________________ .\\axxx (That's an 'M')
So in summary, people used to do this in some other programming language, so have continued to do it in a different language, even though it serves no purpose, and indeed makes the code harder to read? Sack 'em,. I say, Sack 'em!
___________________________________________ .\\axxx (That's an 'M')
-
So in summary, people used to do this in some other programming language, so have continued to do it in a different language, even though it serves no purpose, and indeed makes the code harder to read? Sack 'em,. I say, Sack 'em!
___________________________________________ .\\axxx (That's an 'M')
_Maxxx_ wrote:
Sack 'em,. I say, Sack 'em!
I am also in your side that this is not that readable comapring the other version of it (ie. something == null - which is more simpler and expected), but I dont agree with you that (null==something) is that much "unreadable" that you need to sack somebody. If you have hardship reading this style, according to your way of thought, you are also a good candidate to get sacked. :laugh:
Moim Hossain R&D Project Manager BlueCielo ECM Solutions BV
-
A lot of code I have come across recently has If (null == something) whereas I tend to write if (something == null) which seems to me to be much more logical and easily readable - So the question is, is there a good reason for using the former over the latter?
___________________________________________ .\\axxx (That's an 'M')
If (null == something) and if (something == null) are nothing but the same. But I/many people always prefer - If (null == something), because if we use - if(something == null), sometimes it may happens that instead of == we use = and that change the value of 'something'. And if 'something' is used throuout the programe, you can imagine how wrong result it will create. So using If (null == something) is always be safe. Hope this will be helpful to you. Thanks, -Yogesh Patil.
-
So in summary, people used to do this in some other programming language, so have continued to do it in a different language, even though it serves no purpose, and indeed makes the code harder to read? Sack 'em,. I say, Sack 'em!
___________________________________________ .\\axxx (That's an 'M')
_Maxxx_ wrote:
Sack 'em,. I say, Sack 'em!
On a serious note, people who write code this way are generally people who've used something like C and have been caught out by assigning a value rather than doing a comparison. The fact that they've made a conscious effort to change their coding style to make sure that their code is less error prone is probably an indicator of a good developer, rather than anything else.
It definitely isn't definatley
-
So in summary, people used to do this in some other programming language, so have continued to do it in a different language, even though it serves no purpose, and indeed makes the code harder to read? Sack 'em,. I say, Sack 'em!
___________________________________________ .\\axxx (That's an 'M')
Right, but even "modern" C compilers will issue a warning if the mistake is made:
Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland
aa.c:
Warning W8060 aa.c 25: Possibly incorrect assignment in function mainAnd HP C:
if ( result = 5 )
....^
%CC-I-CONTROLASSIGN, In this statement, the assignment expression "result=5" is used as the controlling expression of an if, while or for statement.
at line number 13 in file MY$ROOT:[000000]AA.C;1The D language defines the mistake as an error.