1 2 3
That was easy! Option.get removes the Option wrapper and returns the inner value so you can get to work with it.
Well, until you starting seeing these stack traces in production:
1 2 3
Everything was fine when your method was returning the expected Some wrapper in development… unit tests… staging… and production. But then came an unexpected condition when None was returned and your code wasn’t expecting it. None.get will instead throw the above exception, probably in a place where you weren’t expecting to catch one.
Recommendation: Always use Option.getOrElse instead of Option.get
Use Option.getOrElse to provide a default value or behavior when your reference is empty.
- If your reference is defined, getOrElse returns the unwrapped value
- If your reference is empty, getOrElse returns the result of your function literal or default value
1 2 3
By providing a workaround for empty references, you are making your application sturdier and emphasizing how it handles unexpected conditions. Future maintainers of your code (and you should always plan for them) will thank you.
Note: your function literal is not actually required to return a valid default value here. In cases where an exception is more appropriate there would not actually be anything returned:
1 2 3 4 5
Update - A Very Relevant Tweet On This Subject
Okay, Paul is only half-kidding here. Still its surprising how often one finds our fellow developers still calling Option.get (and ending up with None.get).
Stick with Option.getOrElse and you’ll never get caught by surprise.