Objects.nonNull()

Tom Hawtin tom.hawtin at oracle.com
Fri Jan 14 03:13:24 UTC 2011


On 13/01/2011 22:11, Brian Goetz wrote:
> <rathole>
> I disagree with Tom here. We have *already* made the billion dollar
> mistake, and we can't eliminate that headache (but we can provide easier
> access to aspirin.) Java developers are *constantly* writing methods
> like the suggested carpet-sweeping nonNull() to work around this (and
> some of the Objects methods already do this, like hashCode()). If I had
> to guess how many times this exact method had been written across all
> the Java code in the world, I would be quite surprised if its common
> logarithm were less than 4.

If you come across developers abusing null to represent empty, the 
appropriate thing to do is point them in the direction of Items 38 and 
43. Java used to offer [some] technical leadership.

The two categories of methods in Objects kind of demonstrates the 
dumping ground nature of utilities classes. Perhaps the class should be 
split, and given actually meaningful names.

IMO, the name nonNull is perfect in the extremely common context.

     this.donkey = nonNull(donkey);

It's a statement about the expression. Method names don't generally say 
what they are going to do if passed illegal values. You could imagine 
other constraints expressed similarly.

Tom



More information about the core-libs-dev mailing list