Navigation

RSS 2.0 New Entries Syndication Feed Atom 0.3 New Entries Syndication Feed

Show blog menu v

 

General

Use it

Documentation

Support

Sibling projects

RIFE powered

Valid XHTML 1.0 Transitional

Valid CSS!

Blogs : Archives

< Transparent PNG on IE with servlet containers   Java standards complient HTML component hoax >
The dangers of auto-unboxing (part 2)

I reported this issue to the JSR-201 comments address and got an almost immediate response (which is very encouraging). Thanks for being actually there, guys!

I think that this issue is very important and would like to collect public opinions about it. That's why I take the liberty to paste their reply and my answer.

Summarized:

  • the NPE behaviour has been adopted since the JSR-201 group came to the concensus that when converting to natural default values, masked errors can potentially be introduced,
  • I think that by throwing NPE exceptions, masked errors are always introduced since there's no clear indication of where to perform null checks.

With the current implementation in JDK 1.5, I actually think that auto-unboxing is useless and more error-prone that manual unboxing since you have to be sure to catch all occurrances manually and still introduce nullchecks all over the place.

There seem to be two very opposite point of views on this, with opponents and supporters for both and some gray area of things that are bound to taste or are dependent on the use-case (for instance when used in the Map null value retrieval problem). Maybe it's better to pull auto-unboxing back out as was originally planned and only perform auto-boxing.

Please contribute your opinion about this matter.


Their reply:

> Thanks for your input.  This topic was highly controversial.  The expert 
> group considered it in detail.  We sought public input and got many, 
> many responses.  They were *overwhelmingly* in favor of throwing 
> NullPointerException upon attempt to auto-unbox null.  The expert group 
> consensus independently favored this behavior, so it clearly won out.  
> While there are advantages to the other behavior (auto-unboxing null 
> returns the natural default value for the primitive type), it was 
> decided that the potential to mask real errors outweighed the advantages.

My answer:

thanks for the quick reply.

I can understand from a theoretical point of view that so many opted for the NPE since it sounds the safest. I would probably myself have opted for it when considering it in a detached way. I think however that now that the technology is actually available in beta form, people that are actually using it should speak out. You know that I got bitten so much by this already that I asked the makers of CodeGuide (JDK 1.5-capable IDE) for the possibility to disable auto-unboxing or to see a big fat red warning where they occur. I prefer to see at compile-time where a potential NPE can lurk than to get mails from customers saying something weird happened at runtime.

I'm really interested in a real-world example where the default initialization approach introduces real errors. If you're using auto-unboxing, you're also using auto-boxing, and most probably to store primitives in collections or to use them as parametrized types. There are no other real reasons to use the class counterparts of primitives. Now, if you're expecting to work with primitive types, you certainly don't expect any NPE and it's pretty normal to expect the natural default values to appear. With the current 1.5 implementation however, you have to be extremely careful with all code to make sure that you didn't miss any possibility of auto-unboxing. As you know, that's one of the biggest reason of introducing new bugs: relying on a human's ability to spot everything consistently manually. In the end, I feel that auto-unboxing doesn't buy me anything since I still need to add if-statements, except that it got a little harder to do since a lot can pass by unnoticed and it's still as bulky as before (apart from one little conversion call). Any other language that has primitives as first-class objects (which is what you're trying to simulate somehow here anyway), will using the natural default value since you're thinking about primitives, really.

I really urge you to open up this issue for debate again.

posted by Geert Bevin in Java on Mar 12, 2004 10:07 PM : 6 comments [permalink]
 

Comments

Re: The dangers of auto-unboxing (part 2)

"I prefer to see at compile-time where a potential NPE can lurk than to get mails from customers saying something weird happened at runtime." What?? Yeah, that's cool about being able to see where they can happen. But what - you think a default value being returned instead of NullPointerException could possibly be better? Now instead of being told "We're getting this strange stack trace", you'll be hearing "Your program says 1+4 = 4. At least with a NullPointerException, you get a line number for where the error occured.

Re: The dangers of auto-unboxing (part 2)

All examples that advocate the importance of the NPE are related to checking the existance of a key in & map. I think these are wrong since you rely on null while unboxing to a primitive. A primitive can't be null, it can only be uninitialized. If you need to work with primtives, check the existance of a key through the containsKey() method first. This is a clear rule with a very clear foundation: "you can't check for existance by using null when unboxing to primitives, since primitives simply can't be null".

Re: The dangers of auto-unboxing (part 2)

Frankly speaking, I totally agree with Geert. Throwing NPE's is safe since maps provide a test method to verify the presence of a key.

However, some Map implementations allow nulls to be stored. This should be considered quite carefully.

Re: The dangers of auto-unboxing (part 2)

Well - I have not used JDK1.5 yet, but what I understand is this:

For the line of code below

Integer i = map.get(key);

the equivalent of auto-unboxing *should* be:

int i = (map.get(key) != null) ? map.get(key).intValue() : defaultValue;

But the current JDK implementation does not provide anything to set the defaultValue's value. And defining this value to 0 is not a good solution - how would you know that the map does not contain the asked pair (how would you know that your zero means 0 or null). On the other hand, primitives can't be null thus auto-unboxing could assume null = 0.

The NPE solution is also a bit dangerous : you could get those exceptions popping out of your program and crashing it like that - destroying the avantages of auto-unboxing.

As someone suggested in TSS.com, maybe the solution would be to permit the developer to specified the defaultValue whenever a null is returned by the collection. This way, you avoid testing NPE, and you can specify the value null would be converted to when auto-unboxing.

Re: The dangers of auto-unboxing (part 2)

Totally agree with Geert: auto-boxing should really really be pulled out. In an application I'm working no now we have performed profiling of coplex code drawing somewhat excel-like tables on screen. The code did work slowly. Do you know what happened to be the memory-hog?

new Double( v )

Notice: this is not an early optimization, it is a result of profiling real-world business application (I work for a bank).

And now what? I will not even NOTICE my, or my colleague's code is doing new Double() behind the scenes? I know that now with modern garbage collectors we shouldn't worry too much about memory allocation ... except for the bits of code that ARE extremely performance critical and where really LOTS of objects are allocated. And this sounds exactly like the bits of code that perform heavy floating-point computations. A small impedance in the interfaces of two libraries -- an here you go a HUGE memory hog you have. Poor us. This technique SHOULD be pulled out.

Re: The dangers of auto-unboxing (part 2)

I totally agree with Anton. Coming also from a bank background dealing with daily performance botlenecks auto-xing sounds very dangerous.

At the present I could not find any informations about the performance impacts of the new features.

Sure there are some very cool demos where it looks very sexy to have easy generics collection access with auto-boxing. I think the realworld looks a little bit stronger.

The point is that if you need to catch the Null values you are free to do it. There is no need for throwing the nasty NPE and making life dificult.

But it would be very helpful when the new IDEs would outline all places where auto-xing happens. Every developer, even the beginning programmer, should be aware of the point that s/he is using this feature and not asking in the news groups why 1 + 4 = 4.

Add a new comment

Comments on this blog entry have been closed.

< Transparent PNG on IE with servlet containers   Java standards complient HTML component hoax >
 
 
 
Google
rifers.org web