|
|
|
|
|
by klibertp
321 days ago
|
|
> It should also be noted that handling state like that is not really idiomatic Erlang. It's not about the state but about intermediate results. When you have a value that you pass to one function, and then you need to pass the result to another function, you're not dealing with a "state" as OTP defines it, unless the calls are asynchronous. Often, they're not, and that's where variable rebinding comes in. Worth noting: `|>` macro operator in Elixir serves a similar purpose, as long as you don't need pattern matching between calls. In that case, you don't have to name intermediate results at all, resulting in cleaner code. > State variables are what I think corpos call a "code smell". Having to call multiple functions in a sequence is the most natural thing to do, and Erlang code is littered with "X1 = ..., X2 = ...(X1), X3 = ...(X2)" kind of code everywhere. |
|
Also, the latest versions of OTP have introduced the `maybe` expression: https://www.erlang.org/eeps/eep-0049