views:

202

answers:

3

Summary pretty much says it all. Here's the relevant snippet of code in ImmutableList.createFromIterable():

  if (element == null) {
    throw new NullPointerException("at index " + index);
  }

I've run into this several times and can't see why a general-purpose library function should impose this limitation.

Edit 1: by "general-purpose", I'd be happy with 95% of cases. But I don't think I've written 100 calls to ImmutableList.of() yet, and have been bitten by this more than once. Maybe I'm an outlier, though. :)

Edit 2: I guess my big complaint is that this creates a "hiccup" when interacting with standard java.util collections. As you pointed out in your talk, problems with nulls in collections can show up far away from where those nulls were inserted. But if I have a long chain of code that puts nulls in a standard collection at one end and handles them properly at the other, then I'm unable to substitute a google collections class at any point along the way, because it'll immediately throw a NullPointerException.

A: 

One reason is that it allows functions that work on the list not to have to check every element for Null, significantly improving performance.

0xfe
No, the performance improvements we got from this were VERY small in the grand scheme of things.
Kevin Bourrillion
+2  A: 

In general in Google Collections the developers are of the group that does not believe that nulls should be an expected general purpose parameter.

You can see an example of that thinking here.

Yishai
+7  A: 

I explained this at the 25-minute point of this video: http://www.youtube.com/watch?v=ZeO_J2OcHYM

Sorry for the lazy answer, but this is after all only a "why" question (arguably not appropriate to StackOverflow?).

EDIT: Here's another point I'm not sure I made clear in the video: the total (across all of the world's Java code), amount of extra code that has to be written for those null-friendly cases to use the old standbys Collections.unmodifiableList(Arrays.asList(...)) etc. is overwhelmed by the total (across all of the world's Java code) amount of extra checkArgument(!foos.contains(null)) calls everyone would need to add if our collections didn't take care of that for you. Most, by FAR, usages of a collection do not expect any nulls to be present, and really should fail fast if any are.

Kevin Bourrillion
I guess my track record of questions about google collections on SO isn't so hot. :-/
Matt McHenry
-1. Don't tell me what I should or should not be putting in my collections. Nulls are often valid in my lists. It's rare that I want nulls in a set or map, but there are plenty of other tools I can use if I want to guard against null elements.
finnw
First, who's telling you not to put null in a collection? If you need to, as we Googlers need to about 5% of the time (we studied this carefully), just use a collection that supports it.Second, a choice was made, and this question asks why the choice was made. Does it really make sense to downvote the answer because you don't like the choice? It's still the answer to the question asked.
Kevin Bourrillion