50

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.

5
  • 1
    Question: do you interpret "general-purpose" to mean "100% of purposes" or "95% of purposes"? Feb 12, 2010 at 17:12
  • 1
    Re: edit 2: This is to claim that all those intermediate points along the way should be agnostic about whether they're passing nulls through or not. I don't agree with this! Every one of these APIs should either explicitly allow null or explicitly disallow it. Everyone can continue to press this point all you want, but please realize it's pure complaining, and not constructive at all. Even if you convinced every one of us we were wrong, wrong, wrong (unlikely, of course), it still wouldn't matter: it's not like we can change it anymore anyway. Feb 13, 2010 at 18:58
  • 1
  • "general-purpose" means "useful for 100% of purposes". Otherwise it's not a general solution, it's just a 95%-general solution. Even if Google "personally" don't like nulls (I don't really like them either, admittedly) sometimes you have to store an array of names where some things don't have names, and using an Optional wrapper wastes storage space (the point of ImmutableList is to be efficient. Allegedly.)
    – Hakanai
    May 28, 2013 at 23:43
  • Link provided by @KevinBourrillion expired. I think nowadays more or less similar is a Using and avoiding null Mar 12, 2018 at 8:44

4 Answers 4

38

I explained this at the 25-minute point of this video: https://youtu.be/ZeO_J2OcHYM?t=1495

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.

13
  • 26
    -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
    Feb 13, 2010 at 11:58
  • 36
    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. Feb 13, 2010 at 18:51
  • 1
    Collections.unmodifiableList() isn't really immutable because you never know if someone is holding onto a copy of the backing list. So I find it to be an unacceptable workaround. So what we would end up doing to work around this limitation is to write a BetterImmutableList class which works exactly the same way but permits nulls. I can always use BetterImmutableList<@NotNull String> if I want to assert that it doesn't contain nulls anyway. But it's tragic, you know, because Guava is just so good in so many other ways.
    – Hakanai
    Nov 18, 2014 at 0:31
  • 1
    Your concern about unmodifiableList doesn't apply to my example. And are you going to also rewrite EnumMap, ConcurrentHashMap, all the JDK Queues, etc. etc. for this reason? Feb 5, 2015 at 1:12
  • 3
    I suspect if it needs to watch a video to explain, that must be some nonsense. Nov 16, 2017 at 11:56
13

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

0
1

From Guava's Github Page

Careless use of null can cause a staggering variety of bugs. Studying the Google code base, we found that something like 95% of collections weren't supposed to have any null values in them, and having those fail fast rather than silently accept null would have been helpful to developers.

The Guava position is largely, that there are other ways to avoid nulls in collections. For example, fetching a batch of items with a specific key. E.g.

// If a widget for the given id does not exist, return `null` in the list
private List<Widget> getWidgets(List<String> widgetIds);

// Could be restructured to use a Map type like this and avoids nulls completely.

// If a widget for the given id does not exist, no entry in list
private Map<String, Widget> getWidgets(List<String> widgetIds);
-3

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

2
  • 6
    No, the performance improvements we got from this were VERY small in the grand scheme of things. Feb 12, 2010 at 17:10
  • Except Function does annotate its parameter as @Nullable, so you will actually have to check that for null to satisfy code inspection, despite the fact that the list can't contain nulls. (lol)
    – Hakanai
    Jun 11, 2013 at 4:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.