Re: check for empty Strings
From: Chris Smith (cdsmith_at_twu.net)
Date: 12/14/03
- Next message: Chris Smith: "Re: BorderLayout and component height"
- Previous message: Peter BetBasoo: "BorderLayout and component height"
- In reply to: Alex Hunsley: "Re: check for empty Strings"
- Next in thread: Roedy Green: "Re: check for empty Strings"
- Reply: Roedy Green: "Re: check for empty Strings"
- Reply: Alex Hunsley: "Re: check for empty Strings"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 14 Dec 2003 15:27:59 -0700
Alex,
Hate to disagree, but...
Alex Hunsley wrote:
> > A little note here on String comparison: reversing the order of items in
> > an equals() call can be a useful defensive technique.
> >
> > For example, suppose you have a String called lastName. If it's set to
> > null, the following code will crash out on a Null pointer reference:
> >
> > if (lastName.equals(""))
> >
> > So, in order to fix the Null pointer situation, you could write:
> >
> > if (lastName != null && lastName.equals(""))
> >
> > However, you can do away with the null check completely by writing:
> >
> > if ("".equals(lastName))
This code will do exactly the same thing whether lastName is null or
non-empty, but do something else if it's non-null or empty. That's such
an unusual branch condition, really, that I'd rather see it said
uniquely, e.g. as:
if ((lastName == null) || !lastName.equals(""))
Or, do you often encounter the need to do one thing in the null-or-
nonempty case, and something else in the empty string case. In any
case, use of this very unusual condition should never become a default
when you haven't thought about the correct behavior for null in that
scenario, or when null is not a valid input.
and later:
> on the other hand, if lastName should *never* be null, by writing the
> original:
>
> if (lastName.equals(""))
>
> you'll pretty quickly find out if it ever *is* null.
> However, in this case, you're better off checking for null explicitly
> and flagging some sort of error/exception of your own.
I definitely disagree here (with that last sentence). There are many,
many cases where code is written that uses a reference, and assumes that
the reference is not null. In fact, that probably constitutes upwards
of 90% of all references in top-level code units (methods, constructors,
instance and static initializers, etc.), and upwards of 99% of actual
dereferencing of a reference variable in Java code. If every single
such use of a reference were preceeded by an if and explicit throw of
and exception, code would be well-nigh impossible to read. Even every
top-level code unit beginning with such a statement would be rather
extreme.
Not to nag you, Alex. I just fear the spread of Placate The Compiler
Syndrome (or, in the case, the related Placate The VM Syndrome), which I
fight on a daily basis when working with development teams to try and
improve their defect rates and quality of code. Adopting coding styles
that hide errors is a bad idea, and a prejudice against runtime crashes
and in favor of silent data corruption is something to fight, not
support.
-- www.designacourse.com The Easiest Way to Train Anyone... Anywhere. Chris Smith - Lead Software Developer/Technical Trainer MindIQ Corporation
- Next message: Chris Smith: "Re: BorderLayout and component height"
- Previous message: Peter BetBasoo: "BorderLayout and component height"
- In reply to: Alex Hunsley: "Re: check for empty Strings"
- Next in thread: Roedy Green: "Re: check for empty Strings"
- Reply: Roedy Green: "Re: check for empty Strings"
- Reply: Alex Hunsley: "Re: check for empty Strings"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|