Gianugo Rabellino wrote:
I've seen in the new API that in a number of places there are contracts that return null if something isn't found: since these are all perfect candidates for nasty NPEs, how about switching to NullObject pattern or throw Exceptions instead?

I think it all depends on whether not finding something is to be considered a normal occurrence or an abnormal condition.


Should java.util.Map.get throw an exception if there's no entry with the given key? Since the key might be derived from user input, not finding the entry should be considered a possible occurrence and not an error, so returning null is a better option than throwing an exception, IMHO.

Conversely, if the thing you're looking for _must_ be there, not finding it is clearly a programming error, so you should throw an _unchecked_ exception.

I would avoid checked exceptions in any case.

Since I haven't looked at the Kernel code, I cannot tell if we're talking about the former or the latter case.

As for the NullObject pattern, I have never used it, so I won't comment on it.

> Just my 0.2c, but since we're talking foundations I think it could be a
> right time to be strict about code principles...

Just my 0.02€ (so this is worth 10 times your opinion, or about 12 if you're talking dollars ;-) ).

Ugo



Reply via email to