r/functionalprogramming • u/effinsky • Sep 29 '23
Question How to construct/compose functions so as to never have to "return early"?
I am a toddler when it comes to FP but I am intrigued (mostly thru Rust, Elixir, now OCaml) -- it's a common thread in FP that a fn should have a single exit point, I think, and it think this is one of the things that really sets it apart from programming in a procedural style. You know, in Go, we do early returns, in fact make returns as early as possible, ALL THE TIME. They really are procedures, not functions. Now, in OCaml and almost everywhere else in FP you have no `return` keyword so you have to get around without it. I'm wondering how to structure my funcs in Rust specifically, so I don't rely on the `return` keyword, which they have, and instead embrace the more FP, declarative way of doing things. Is there any advice you can give? I can imagine pattern matching is fundamental here etc. We can throw around some simple examples as well, of course.
I feel like wrapping my head around this can kind of push me in the right direction with FP.
Thanks a bunch!
13
u/jhartikainen Sep 29 '23
Composing smaller functions to perform things could potentially allow you to reduce the amount of returns.
But frankly, in a language like Rust, I don't really see why you should avoid it. If it makes the code flow or structure easier to understand, I don't see why you wouldn't want to make use of early returns. Even in something like Haskell the do-notation can be quite useful even if it is sometimes quite "procedural".
5
u/mbuhot Sep 29 '23
My advice would be:
7
u/Swordfish418 Sep 29 '23 edited Sep 29 '23
In FP you can create monads and lift all computations into those monads to emulate "early exit" while still formally having a single exit point. Check Haskell's Maybe & Either monads to understand this idea better (here for example). In Rust you can use similar ideas by using monadic types similar to Maybe & Either from Haskell (for example Result type and its combinators, like then, and_then, map, etc).
5
u/LanguidShale Sep 29 '23
Don't worry about it too much, early returns are just syntactic sugar for nested if blocks.
4
u/InstaLurker Sep 29 '23 edited Sep 29 '23
well, through pattern matching you can easily return early
f 0 _ _ _ = 0
f a b c d = a + b + c + d
in some imperative pseudocode
def f ( a, b, c, d )
if a == 0 then return 0
return a + b + c + d
3
u/aaaaargZombies Sep 29 '23
I'm guessing that your early returns are effectively
ifelsestatements but theelsepart is implicit. So you can still make very similar constructs in functional programming but it can be nice to be explicit.If you look at the
viewCardfunction at the bottom it's pattern matching on the type variants but in Go you could write it as a series ofifstatements. If you hit anAceyou effectively return early as there is no need to check the other cases.https://elm-lang.org/examples/cards
3
u/mckahz Sep 30 '23
As others have said, language constructs like it or pattern matching are effectively early returns, and Monads can be used to simulate early returns.
There is the case of other control flow constructs like continue/break. Continue can be modelled in all sorts of ways depending on what you're trying to implement, but it's pretty straightforward. However, if you're trying to simulate breaking behaviour, it's a little trickier. Keep in mind though, FP is quite good in parallel computing and in that context short circuiting a loop can be slower than just performing the operation.
3
u/everything-narrative Sep 30 '23 edited Sep 30 '23
How do you avoid early returns?
Else.
That's how.
Remember
else? It comes free with yourifstatement.Early return is a GOTO in disguise. It has all the same problems as GOTO, and is anathema to structured procedural programming, because it violates the refactorability principle.
Early return requires analyzing a procedure body based on both state and control flow, whereas properly structured programming only needs control flow analysis. It is simpler to reason about, when you don't have to manage what state the program is in to reach a certain point in the code, you can simply refer to the if-statement condition and know exactly why you are where you are. It has locality.
In structured procedural programming, there is no early return. The block in a function body must return, and all execution paths within must return.
This has the effect that any one block in a procedure body can itself be extracted into a procedure that accepts local variables as arguments. This is an extremely powerful property.
(Exceptions actually obey this principle, because they don't respect the call-stack!)
C#:
It is impossible to extract the conditional as a function, because it returns. Contrast:
(This is a contrived example, but it gets the point across.)
Languages like Rust and Ruby and ALGOL 68 (yes, this philosophy has been around for 55 years!!! and we still have people clamoring for early return. My IDE even suggests it!) allows eliding the return statement entirely, and encourages proper structured programming.
You should stop using early returns, because it makes refactoring difficult.
Most functional programming is actually procedural programming, and has been around since Algol 68 -- they had anonymous procedures, first-class procedures, elided returns, and all sorts of goodies.
2
u/jeffstyr Sep 30 '23
To build off another response, if you have code after a
return, then the return must have been conditional (else the subsequent code would be dead code), so if you make theelseexplicit then you can avoid the early return.Also, in a language like Haskell, you only have expressions, so a function always results on a value (there are no statements-for-side-effects). This helps to drive the action.
Also, just to be clear, even a “single exit point” can look like multiple:
if(c) { 3 } else { 4 }is fine; even though you could read it as “two returns” it’s really just one (the value of theif).1
u/Inconstant_Moo Oct 04 '23
But in a functional style, the body of a function is an expression and so all it does it return a value. Here's a function in my lang:
If
nis even, is that an "early return" from my function?(Note that in fact it totally is an early return, in that when this bit of code gets interpreted, we ignore the other branch. But considered as a piece of functional code, it's a single expression being evaluated.)