Card 08/ 08
All 8 cards
RecapDifficulty: Advanced1 min
What the Signature Says, and What It Costs
Five things this topic established, and where each one was.
| The thing | Where |
|---|---|
Optional puts "there might be nothing" into the signature | A Return Type That Says There Might Be Nothing Here |
orElse for a value in hand, orElseGet for anything that costs | orElse, orElseGet and orElseThrow |
orElse's argument is evaluated whether or not it is needed | Why orElse Ran Your Fallback |
get() without a check throws, exactly as the null would have | Why get() Without a Check |
| A return type, never a field, a parameter or an element | Where Optional Does Not Belong |
The one worth carrying: Optional makes absence visible and does not make anybody handle it. Everything that goes wrong with it is a caller unwrapping it immediately, which puts the original problem back with a longer name.