Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Testing for null - simple! (http://www.velocityreviews.com/forums/t149233-testing-for-null-simple.html)

java_programmer 01-11-2006 05:52 PM

Testing for null - simple!
 
Hi all,

I have seen the following test for null written

if(null != myObject)
{
....
}

as opposed to

if(myObject != null)
{
...
}

I have been told that the first version is slightly optimised and more
performant but I can't work out why. Does anyone have any thoughts?

Thanks


Paul Bilnoski 01-11-2006 06:15 PM

Re: Testing for null - simple!
 
java_programmer wrote:
> Hi all,
>
> I have seen the following test for null written
>
> if(null != myObject)
> {
> ...
> }
>
> as opposed to
>
> if(myObject != null)
> {
> ..
> }
>
> I have been told that the first version is slightly optimised and more
> performant but I can't work out why. Does anyone have any thoughts?
>
> Thanks
>


It really shouldn't be any more performant. I suspect it's a carryover
from C/C++ to protect against assigning on accident:

if (myObject = null)
{
... will not execute
}

--Paul

Thomas Hawtin 01-11-2006 06:35 PM

Re: Testing for null - simple!
 
java_programmer wrote:
>
> I have seen the following test for null written
>
> if(null != myObject)
> [...]
> as opposed to
>
> if(myObject != null)
> [...]
> I have been told that the first version is slightly optimised and more
> performant but I can't work out why. Does anyone have any thoughts?


It really shouldn't make any difference to performance (but check for
yourself if you like). The reason why a minority of programmers prefer
the former is that in C and C++ (but not Java) if you accidentally write
"if (ptr = null)" instead of "if (ptr == null)" (or "if (!ptr)"), then
the code will compile but produce incorrect results. In Java the mistake
can only happens if comparing two booleans, or from 1.5 comparing a
java.lang.Boolean.

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/

Tony Morris 01-11-2006 10:06 PM

Re: Testing for null - simple!
 

"java_programmer" <davidebacci@gmail.com> wrote in message
news:1137001938.351820.234160@g44g2000cwa.googlegr oups.com...
> Hi all,
>
> I have seen the following test for null written
>
> if(null != myObject)
> {
> ...
> }
>
> as opposed to
>
> if(myObject != null)
> {
> ..
> }
>
> I have been told that the first version is slightly optimised and more
> performant but I can't work out why. Does anyone have any thoughts?
>
> Thanks
>


Broad claims regarding performance are almost always false.
This case is no different except that it exhibits the phenomena to an
extreme - that is, even a drunken monkey would call 'bulls**t' on the claim.
I suggest that you question the authenticity of your source, not the source
itself.

--
Tony Morris
http://tmorris.net/




Roedy Green 01-11-2006 10:13 PM

Re: Testing for null - simple!
 
On 11 Jan 2006 09:52:18 -0800, "java_programmer"
<davidebacci@gmail.com> wrote, quoted or indirectly quoted someone who
said :

>if(null != myObject)


There is a related pattern:
if ( "blue".equals(skyColour) )...

rather than
if ( skyColour.equals('blue") )...

The first awkward-sounding pattern will not raise an exception if
skyColour is null; it will simply evaluate as false.

In English, "The skycolour is blue" and "the skycolour equals blue"
have different meanings. What you usually mean in Java is closer to
the English "is" even though you express it with equals or ==.

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

java_programmer 01-12-2006 11:46 AM

Re: Testing for null - simple!
 
Thanks all. It seems so obvious now that it has been pointed out and I
really should have know better! I quite like Roedy Greens string
comparison code that means you don't need to check a string for null -
I will look out for that in future,

Much appreciated.


Scott W Gifford 01-13-2006 03:29 AM

Re: Testing for null - simple!
 
"java_programmer" <davidebacci@gmail.com> writes:

> if(null != myObject)

[...]
> as opposed to
> if(myObject != null)

[...]
>
> I have been told that the first version is slightly optimised and more
> performant but I can't work out why. Does anyone have any thoughts?


Sounds dubious. In C, a common reason to do comparisons with the
constant first is so if you mean "if (myObject == null)" but instead
type "if (myObject = null)", the compiler can catch it (since "if
(null = myObject)" is illegal). Java will catch this error (unless
myObject is a boolean), but I bet this style is mostly a leftover
habit from C. It certainly doesn't hurt anything.

---ScottG.

Stefan Ram 01-13-2006 03:45 AM

Re: Testing for null - simple!
 
Scott W Gifford <gifford@umich.edu> writes:
>> if(null != myObject) [...]
>> as opposed to
>> if(myObject != null) [...]

>Sounds dubious. In C, a common reason to do comparisons with
>the constant first is so if you mean "if (myObject == null)"


In C, I'd prefer to write »if( myObject )« instead
of »if( myObject == NULL )« or »if( NULL == myObject )«,
given that NULL is as defined in »stddef.h«.


Eric Sosman 01-13-2006 05:05 PM

[OT] Re: Testing for null - simple!
 


Stefan Ram wrote On 01/12/06 22:45,:
> Scott W Gifford <gifford@umich.edu> writes:
>
>>>if(null != myObject) [...]
>>>as opposed to
>>>if(myObject != null) [...]

>>
>>Sounds dubious. In C, a common reason to do comparisons with
>>the constant first is so if you mean "if (myObject == null)"

>
>
> In C, I'd prefer to write »if( myObject )« instead
> of »if( myObject == NULL )« or »if( NULL == myObject )«,
> given that NULL is as defined in »stddef.h«.


Note that your preferred style does not mean the
same thing as the alternatives. If your preferred
style leads even you to make elementary mistakes, it
may be a good idea to re-examine your preferences ...

--
Eric.Sosman@sun.com


Stefan Ram 01-13-2006 05:27 PM

Re: [OT] Re: Testing for null - simple!
 
Eric Sosman <eric.sosman@sun.com> writes:
>> In C, I'd prefer to write »if( myObject )« instead
>> of »if( myObject == NULL )« or »if( NULL == myObject )«,
>> given that NULL is as defined in »stddef.h«.

> Note that your preferred style does not mean the
>same thing as the alternatives. If your preferred
>style leads even you to make elementary mistakes, it
>may be a good idea to re-examine your preferences ...


Thank you for the correction!

Yes, I should have used "!=" instead of "==".

However, it is only an interpretation that this
mistake of mine was /caused/ by my preferred style.

To me, to test whether an object refered to by the name "o"
exists, it is most natural to write

if( o ){ /* ... */ }

Yes, I learned C first, then Java.

When I am forced to use one of "==" or "!=", it makes me to
start thinking about it and possibly erring -- which also
could be interpret as a cause of my mistake.



All times are GMT. The time now is 03:53 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.