Best practices: MUST NOT use "return"

The return statement from Java signals a side-effect - unwind the stack and give this value to the caller. In a language in which the emphasis is on side-effect-full programming, this makes sense. However Scala is an expression-oriented language in which the emphasis is on controlling/limiting side-effects and return is not idiomatic.
To make matters worse, return probably doesn't behave as you think it does.
A return statement inside a nested anonymous function is implemented by throwing and catching a NonLocalReturnException.
Besides, return is anti-structural programming, as functions can be described with multiple exit points and if you need return, like in those gigantic methods with lots of if/else branches, the presence of a return is a clear signal that the code stinks, a magnet for future bugs and is thus in urgent need of refactoring.
Read more

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Scala snippets
Cosmo
Sign Up Now
or Subscribe for future quizzes