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.



Reply via email to