How to early return in Scala
<p>I am learning Scala at the moment. One thing that I like to do is early returns. I'm convinced that this is far easier for everyone to read as we just remove the invalid states before. Now, as Scala is a functional language and I've read that cutting computation is bad functional style, I'm wondering if there is some trick or functional programming equivalent to early return.</p>

<p>This is code I would write, to be completely clear, this is just a dumb example, I'm not looking for the special hack of my special case, but more for a general advice on how to deal with these.</p>

<pre><code>if (request.headers.get(session_header).isEmpty) {
  BadRequest(session_not_set)
} else {
  Ok(CartHelper.getCart(session, user))
}
</code></pre>

<p>Now, what I'm tempted to do is:</p>

<pre><code>if (request.headers.get(session_header).isEmpty) {
  BadRequest(session_not_set)
  return;
}

Ok(CartHelper.getCart(session,user))
</code></pre>

<p>If you have any hint for me!</p>