Hacker News new | ask | show | jobs
by vbezhenar 3887 days ago
Why it's bad? If I'm sure that this optional contains value, I don't see why it's bad. May be I checked this value presence few lines above.
1 comments

If you are certain optional has a value, don't use optional.

Checking presence of value of an optional is an anti-pattern, the same as doing != null checks.

You should treat Optional the same as a List type with size 1. You never check if a list is size 1 before doing map or filter.

I'm not always own code. Simple example:

    getCharsetOpt("UTF-8")
There is no way this code will return None in this particular case. So any additional checks are unnecessary and make code less readable.
I don't know who said it, and this is probably a paraphrase:

"If it can't happen, it will".

If a function says the result is optional, then it's optional. It's not not optional because you decided it can't ever fail.