I once ran some tests to test this. What I found is that checking against null 
is much faster. Sorry I do not have the test code around anymore but it would 
be very simple to build again.

My guess as to why is that if (foo) is implicit and the conditional could be 
met by a couple things. if foo was a boolean it being null or false would cause 
the conditional to fail. If the object was a number it being null or <= 0 would 
cause the condition to fail. So on and so forth.

When checking if (foo != null) you are explicitly checking if that object is 
null not if that object is null or false, or 0, ect so this check is much 
faster.

I also think if (foo != null) is better syntax to look at because I can tell 
exactly what you are checking for, that is you are intersted in if foo is null 
not that it is  == false or <= 0 or anything like that. I feel that if (foo) is 
lazy.

So in the interest of speed and clean code you should always check for exactly 
what you want.

--- In [email protected], "mitchgrrt" <mitch_g...@...> wrote:
>
> Other than style, are there reasons reasons to prefer using 
> 
>   if (foo != null)
> 
> rather than
> 
>   if (foo)
> 
> Thanks.
>


Reply via email to