Objects.nonNull() (original) (raw)

David Holmes David.Holmes at oracle.com
Thu Jan 13 23:04:27 UTC 2011


Rémi Forax said the following on 01/14/11 08:31:

I think I prefer throwIfNull() which is explicit.

+1

As to what gets thrown, none of these suggestions make that obvious from their name so I would not be concerned about that. Every Java programmer is very early introduced to NullPointerException.

David Holmes

Rémi

On 1/13/2011 4:12 PM, Tom Hawtin wrote: On 13/01/2011 20:06, Brian Goetz wrote:

Most of the other methods in this class are of the form "do the right thing if the object is null (or an array)", but this one is "throw an exception if the object is null." The intent is clear -- it is for simplifying fail-fast behavior by checking for nullity early -- but the name is wrong. This is bad for two reasons: (a) it is confusing and (b) it forecloses on using the name nonNull() for what most people expect it to do -- which is provide a reasonable default. I think this is completely the wrong way around. On encountering a null the right thing is to throw an NPE. Don't make Sir C.A.R. Hoare's billion-dollar mistake worse. Perhaps the carpet-sweeping methods should be renamed, or put into an appropriately named class. Tom



More information about the core-libs-dev mailing list