scala-style-guide
scala-style-guide copied to clipboard
Thoughts about Option.get
I think we should not use Option.get in our code. When the object in question is None, the error message is a cryptic NoSuchElementException: None.get. Instead, we should use Option.getOrElse(throw new Exception(exceptionMsg)) so that it is easier to discover where and why the object was None in the first place.
I tend to agree - the only exception might be when the scope is super local and known for sure there is an element (e.g. with an if check beforehand on option.isDefined)
Yeah, that makes sense. At least it reduces a level of nesting. Also if the variable is volatile you might run into race conditions between the check and the dereference.