I am doing scala through the functional programming course on coursera. I noticed that the automatic style checker tells me that the use of 'return' is a bad habit. Why is that? To me it seems like the use of return would make the code more readable because any other programmers can instantly see that and what the function is returning.
Example, why is this;
def sum(xs: List[Int]): Int = {
if( xs.length == 0){
return 0
}else{
return xs.head + sum(xs.tail)
}
}
Considered to be worse than this;
def sum(xs: List[Int]): Int = {
if( xs.length == 0){
0
}else{
xs.head + sum(xs.tail)
}
}
I am used to javascript, so that might be a reason why I feel uneasy about it. Still, can anybody make it obvious why the addition of the return statement makes my code worse? If so, why is there a return statement in the language?
In scala, every line is an expression, not a statement. Statements generally don't have a return value, but expressions do.
The last result of a block will be the returned value, and so the style guide operates on this assumption. A return would be an exceptional exit from a block.
def sum(xs: List[Int]): Int = { if(xs.isEmpty) return 0 xs.head + sum(xs.tail) }
The return statement there would cause the function to bail at that return, and generally leads to less understandable code than if you wrote it with the if/else logic, as you did earlier. I believe the rational behind the style decision is to discourage this type of programming as it makes the programs more difficult to understand.
In Java, Javascript and other imperative languages
if...else
is a flow-control statement.This means you can do this
But you cannot do this
Because
if...else
is a statement and not an expression. To accomplish this you need to use the ternary operator (strictly speaking the "conditional operator"), something like:In Scala, this is different. The
if...else
construct is an expression, so more akin to the conditional operator in the languages you are used to. So, in fact your code is better written as:Further, the last expression in a function is automatically returned, so the
return
is redundant. In fact, as the code only has single expressions, the curly brackets are redundant too:So, to answer your question: this is discouraged because it is a misinterpretation of the nature if the
if...else
construct in Scala.But this is all a little besides the point, you should really be using pattern matching
This is much more idiomatic Scala. Learn how to use (and abuse) pattern matching and you will save yourself a huge number of lines of code.
I think another answer for the question why
is that return when used in a closure will return from the method not from the closure itself.
For example, consider this code:
It can be easily missed that when this code is invoked with
sumElements(List(1, 2, 3, 4))
the result will be2
and not10
. This is becausereturn
withinmap
will return fromsumElements
and not from themap
call.