Objects.nonNull()
mark.reinhold at oracle.com
mark.reinhold at oracle.com
Fri Jan 14 00:20:28 UTC 2011
> Date: Thu, 13 Jan 2011 18:15:30 -0500
> From: brian.goetz at oracle.com
> ...
>
> Between checkNonNull() and throwIfNull(), I lean towards the former. In any
> case the answer should be driven by what is more obvious to the reader.
Agreed.
> So
> let's look at some typical code:
>
> public Moo fooWrapper(String x, String y) {
> return foo(throwIfNull(x), throwIfNull(y));
> }
>
> vs
>
> public Moo fooWrapper(String x, String y) {
> return foo(checkNonNull(x), checkNonNull(y));
> }
>
> Since throwing should be the exceptional path, it feels to me that having throw
> in the name sets slightly the wrong expectations. ...
Agreed.
I'm still troubled by the "check" prefix, though. It implies that the
named condition will be tested but it doesn't clearly relate the result
of that test to the method's exception-throwing behavior.
Here's an idea: Why not treat this as a (degenerate) kind of conversion
operation? Call it asNonNull(x) -- it (trivially) converts its argument
to a non-null value, and if it can't then it throws an NPE.
public Moo fooWrapper(String x, String y) {
return foo(asNonNull(x), asNonNull(y));
}
Of all the names we've considered, this looks the most natural to me.
- Mark
More information about the core-libs-dev
mailing list