L
Lew
Tom said:The method already does that - if it's null, you get a
NullPointerException.
Umm, the point of my comment was to *avoid* the NPE, duhh. Why cause trouble
for the method's client?
Tom said:The method already does that - if it's null, you get a
NullPointerException.
Lew said:Umm, the point of my comment was to *avoid* the NPE, duhh. Why cause
trouble for the method's client?
Umm, the point of my comment was to *avoid* the NPE, duhh. Why cause trouble
for the method's client?
What if there are null entries in the list?
This snippet also, of course, assumes that T is a comparable object (I
believe Collections.sort fails if there's no comparator and T can't be
cast to Comparable).
Tom said:I'm hoping that you're being sarcastic. Or that i've misunderstood - i
read your comment as suggesting that the method should null-check the
incoming argument.
The right thing for this method to do if passed a null argument is to
throw an exception. There is no uncertainty about that. And it already
does that, so no check is necessary.
List<String> list = new ArrayList<String>();
for (int i = 0; i < 100; ++i)
list.add(new String("unique"));
? Both "one" and "one hundred" are reasonable answers.
Tom said:I ...
read your comment as suggesting that the method should null-check the
incoming argument.
The right thing for this method to do if passed a null argument is to
throw an exception. There is no uncertainty about that. And it already
does that, so no check is necessary.
Umm, the point of my comment was to *avoid* the NPE, duhh. Why cause trouble
for the method's client?
That's what I recommended, yes.
Not necessarily.
As Mike pointed out, some action may be needed upon discerning a null. To
his example, for example, I'd add logging in some scenarios. The method may
be one whose contract is to deliver some default value, such as Mike's
suggestion for the OP's case
boolean areListElementsUnique(List<?> l)
that a null argument be deemed trivially as containing no duplicate elements.
There is no reason for that check method to throw an NPE when it could
reasonably return 'true'.
I honestly can't imagine a situation where it would be reasonable for
it to do that. If your design involves passing around nulls to
represent empty lists, or lists in some other way not containing
duplicates, then your design is broken.
Mike said:That wasn't my suggestion.
Lew said:The point is that it depends on the required contract. Some apps may
wish to consider the empty list (including null) as trivially not
violating the uniqueness constraint. It isn't necessarily an error
that the list be null.
And even if it is, as you yourself pointed out you'll wish to handle
it at the point of detection, and I add, at the least you'll want to
log it and quite possibly wrap in a different exception,
so it's not
possible from one's throne to decree for all and sundry that they
must accept the implicit NPE willy-nilly. Contrary to Tom's [sic]
pronouncement, there very well may need to be a null check even with
a rethrow.
Oops, I didn't intend that misattribution. I meant that the method was your
suggestion.
Sorry about that, chief.
The point is that it depends on the required contract. Some apps may wish to
consider the empty list (including null) as trivially not violating the
uniqueness constraint.
It isn't necessarily an error that the list be null.
And even if it is, as you yourself pointed out you'll wish to handle it
at the point of detection,
and I add, at the least you'll want to log it
and quite possibly wrap in a different exception, so it's not possible
from one's throne to decree for all and sundry that they must accept the
implicit NPE willy-nilly. Contrary to Tom's [sic] pronouncement, there
very well may need to be a null check even with a rethrow.
Tom said:Should NPE and IOOBE be subclasses of IAE?
Tom said:I will interject quietly to say that Mike's point about detecting
nulls early and dispatching them promptly, rather than waiting for the
inevitable spooky NPE at a distance, was spot on, and i'm a fool for
not having thought of it.
Don't say that. I don't want to be a fool every time you think of something
I overlooked.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.