On Sep 30, 1:26 am, "Scott A. Hightower" <VastEr...@SpamGourmet.com>
wrote:
...
A related question is whether to test for an empty List passed as an
argument value and throw an IllegalArgumentException or to let
NoSuchElementException through and document that it is *supposed to* happen.
The utility accepts Lists as arguments to several public methods. The only
thing that it does with each List is get an Iterator from it and extract the
elements of the list for comparison and storage.
A null List or a List with no elements is meaningless in the context of the
utility. The documentation explains why and currently warns that a
NullPointerException or NoSuchElementException will be thrown, respectively.
...
While it's very possible that your situation differs, I would be chary
of explicitly
precluding non-null lists of 0 length. I've frequently found that
being able to treat lists or arrays of 0 length the same as lists or
arrays with elements makes for cleaner logic. Are you making callers
of your utility write
if (list.size() > 0) {
utility(list);
}
rather than just
utility(list);
so that you are transferring the cost of understanding the special
status
of 0 length lists a level further up the chain? That may be right but
I've generally been happier with the other choice. I'd have my
utility method do nothing with an empty list.
lists, which is most cases. An empty list is always sorted. It does not
contain whatever you are searching for. Its set of permutations is empty.
However, some operations have minimum input sizes. An empty list of
numbers does not have a mean or median.