Re:Anti-if: The Missing Patterns
by RodgerTheGreat on Friday June 10, 2016 @02:51AM
(#11874689)
In this very specific instance, you could easily and idiomatically reuse a single instance of an immutable empty list:
https://docs.oracle.com/javase/7/docs/api/java/util/Collecti...
No dynamic heap allocation necessary.
It is more brittle, sometimes the best you can do is to prefer empty collections over null. Some environments do offer a solution by using parameter annotations. Personally I do a lot of C# with resharper where you can add the [NotNull] attribute to a parameter, which enables some compile time checking.
(I suspect the author would prefer jumping in a lake over programming in Go.)
I think it is also worth mentioning that passing a new ArrayList over a null value is somewhat wasteful of memory. Though the reference to ArrayList will cease to exist, its allocated memory will hang out until the next GC phase. In the words of Bartleby, I would prefer not to.
I get the principle that is being advocated, but the solution code doesn't pass practical muster for me.