Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Returning an empty list or empty array is different from returning no list at all.

Imagine I want to query a database of tagged articles. Let's say I want to get articles with an exact set of tags.

Passing this method [A, B] will give me articles with tags A and B, passing this method [] will give me articles with no tags. Not specifying the query parameter at all will result in no parameter. I could use a boolean value to keep track of that or just use an optional.



The point is that returning an Optional gives you nothing above returning a null - in fact it increases complexity and makes calling code more error-prone for no gain.

If your contract says you return a Foo, you could be returning a null. If you change that to Optional<Foo> you could still be returning a null.

It's not what Optionals are for. They are just widely misunderstood and abused.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: