try-with-resources and null resource (original) (raw)

Tim Peierls tim at peierls.net
Mon Jan 24 16:43:15 PST 2011


On Mon, Jan 24, 2011 at 6:49 PM, Rémi Forax <forax at univ-mlv.fr> wrote:

> Those who don't want it don't have it forced on them. If the null check were > always performed, I'd have no way to use try-with-resources and get the > side-effects of executing the body up to the point where the null Resource > is dereferenced.

side-effects => hours of debugging

Maybe, but once it's debugged you don't want those side-effects to disappear because of magical null checks.

> (Not saying one always wants to do this, or even that one > often wants to do this, just that try-with-resources shouldn't prevent it.)

Do you have a use case for that ?

Burden of proof goes the other way: There's a lot of existing code out there that could be rewritten to use try-with-resources, and I'd to have to bet that none of it would be better off waiting until the first attempt to dereference to throw NPE.

And even if you find a twisted use case, do you really think anyone will be able to understand that code ?

The harder the code is to understand, the more important it is not to risk changing its behavior through over-protectiveness.

If you want a null check at initialization, add it -- it's 14 characters if you use Guava. You can claim that you'll always want to check early, but you can't dictate that no one else should ever want to defer the check.

--tim



More information about the coin-dev mailing list