cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Berin Loritsch <blorit...@apache.org>
Subject Re: Coding style question: backwards null checks
Date Fri, 07 Mar 2003 17:50:29 GMT
Tony Collen wrote:
> On Fri, 7 Mar 2003, Berin Loritsch wrote:
> 
> Likewise, I've seen something like this in code... can't remember if it's
> anywhere in Cocoon:
> 
> if ( "something".equals(stringToCompare) {
>    ...
> }
> 
> IMO it seems more straightforward and easier to read if it's:
> 
> if ( stringToCompare.equals("something") ) {
>    ...
> }
> 
> Is this just a matter of style as well?  The first way seems goofy if you
> ask me.

Actually, this *does* have a purpose.  In the event that
stringToCompare is null, then you would get spurious
NullPointerExceptions that are not always easy to trace.

The "something".equals(somethingElse) guarantees that
you won't get the NullPointerException every time without
resorting to the longer form of:

if ( stringToCompare != null && stringToCompare.equals("something") ) {
     ....
}

It's a lot more typing to be _truly_ functionally
equivalent.  The compiler cannot guarantee that
the stringToCompare variable is never null, so it
will not give you a warning.


Mime
View raw message