4 ms·
Agreed. The sumOf function is now made more brittle by that fact, and would be unworkably so in a publicly-available API. This is easily fixed by a try-catch bl
by peterevans 10y ago
Agreed. The sumOf function is now made more brittle by that fact, and would be unworkably so in a publicly-available API. This is easily fixed by a try-catch block if one finds that they are indeed allergic to if statements, although that too is a form of flow control. If you are programming in Go, you're going to have to bite the bullet and use an if.
(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.
- RodgerTheGreat 10y agoIn 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/Collections.html#emptyList() https://docs.oracle.com/javase/7/docs/api/java/util/Collecti... No dynamic heap allocation necessary.
- rejschaap 10y agoIt 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.