Re: Why do some people shun `if`?
"Michael P." <[email protected]>
| Newsgroups | gmane.comp.lang.erlang.general |
|---|---|
| Message-ID | <20210817151244.131fd446@aesop> |
On Mon, 16 Aug 2021 00:39:21 -0700 Michael Truog <[email protected]> wrote: > If it is possible to use an "if" expression, > it can result in better > source code due to utilizing only guard expressions. Which could also be done as a module function, a `fun`, a `case`. And others argue that 'it can result in worse' too. > It is possible you > may need to create an extra variable to use an "if" expression but that > can help source code be more descriptive. I might always use `if` as a match-operator expression-operand (RHS of `=`); the name of the result telling what the decision is about. If that is what you mean. > A "case" expression [...] can result in more complex beam source code I consider this premature optimisation. Did I not read, once upon a time, that both `if` and `case` were compiled to `fun` anyway ... > Why prefer guard expressions only? But `if` is not required for "guard expressions only"? ``` f(). A = 1. B = 2. Max = if A >= B -> A ; A < B -> B end. Best = case true of true when A >= B -> A ; true when A < B -> B end. What_if = (fun If() when A >= B -> A ; If() when A < B -> B end)(). Now_this_is = (fun () when A >= B -> A ; () when A < B -> B end)(). ``` (These are months ... years old syntax experiments: what if I dumped `if`? Later found out I had also re-invented Kiwi-format.) > "if" expressions [...] it is best to use them as much as possible. Not at the statement, not at all at you, merely about the collision with the thread read before: that made me laugh myself off the chair. And it made me remember how wrong I was: 20 years ago I assumed that all such questions had already been finally resolved in the 20 years before _then_. ~Michael -- The name "Michael" is, more or less, a Hebrew version of Arabic 'allahu akbar'.