Code review: 7012540 (java.util.Objects.nonNull() incorrectly named)

Ulf Zibis Ulf.Zibis at gmx.de
Wed Jan 26 13:21:48 UTC 2011


Am 26.01.2011 13:31, schrieb Ulf Zibis:
>
> The funtionality is:
> Check the argument for non-nullity AND return it if the condition holds, otherwise fail by 
> throwing NPE.
>

IIRC, Object.nonNull(x) was introduced to preserve the developer from repetitively coding:
     if (x == null) {
         throw new NullPointerException();
     }

But isn't this not only half the rent? The 2nd part of the repetition is:
     try {
         ...
     } catch (NullPointerException npe) {
         doAlternatively();
     }

I think, in case of an expected null value, the developer has a specific idea, how to handle it.
Just throwing an exception should be reserved for _un_expected cases.

So something like "nullChecked(x, runnable)" could serve the full rent.
Fairly this should be delayed to closures.

-Ulf




More information about the core-libs-dev mailing list