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.
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.
if (request.headers.get(session_header).isEmpty) {
BadRequest(session_not_set)
} else {
Ok(CartHelper.getCart(session, user))
}
Now, what I'm tempted to do is:
if (request.headers.get(session_header).isEmpty) {
BadRequest(session_not_set)
return;
}
Ok(CartHelper.getCart(session,user))
If you have any hint for me!