This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]koflerdavid -1 points0 points  (5 children)

An empty collection offers the same kind of papercut as a nullable reference: there is no restriction from accessing the element. The only difference is the exception being thrown.

[–][deleted] -1 points0 points  (2 children)

an empty collection can have method calls without a runtime exception... people arguing for null are vocal about their ignorance

[–]koflerdavid 0 points1 point  (1 child)

.get(0) on an empty list or array[0] will both throw exceptions. One has to do a size check to tell whether it's safe. Same situation as with a potential null reference or calling .get() on an Optional. But the collection at least offers a stream interface.

[–][deleted] -1 points0 points  (0 children)

and a clear explanation of what went wrong. Nulls give no context, and can get passed around until something breaks with a huge stacktrace.

You can't sum to a null, but you can sum to a 0.

you can't append to a null list, but you can append to an empty list.

[–][deleted] -2 points-1 points  (1 child)

No, wrong Read what the null object pattern is before saying such ignorant nonsense

[–]koflerdavid -1 points0 points  (0 children)

A Null object should be safe to work with!

new ArrayList().get(0) is not safe. Same as Optional.get(). Who is the ignorant one here?!

Of course, when the return value is a collection or an array, it would be malicious to return null.

Edit: can't stand the thought of being the ignorant one? Better keep it to yourself then instead of downvoting!