scala-style-guide icon indicating copy to clipboard operation
scala-style-guide copied to clipboard

Thoughts about Option.get

Open mgyucht opened this issue 9 years ago • 3 comments

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.

mgyucht avatar Jan 20 '16 18:01 mgyucht

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)

rxin avatar Jan 20 '16 18:01 rxin

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.

mgyucht avatar Jan 20 '16 20:01 mgyucht