Why do some people shun `if`?
Benjamin Scherrey
scherrey@REDACTED
Sun Aug 15 11:28:55 CEST 2021
Good question. It turns out that abuses of 'if' as flow control, especially
synchronized flow control, is the single largest cause of defects in
software development for all time. In fact, a strong case can be made (and
is made in this outstanding video ( https://youtu.be/z43bmaMwagI ) that
'goto' was the fall guy for 'if' in Dykstra's famous letter to the ACM.
Mainly the issue is the use of 'if' as a statement for alternative program
control flow more than using 'if' as an expression (as is typically the
case in Erlang). It generally manifests itself in imperative languages and
you see it much improved in languages that provide better structural flow
controls like pattern matching and aspects typical of functional languages,
again like what Erlang offers.
In all my coding I restrain 'if' to assertions or guards that are either
immediately passed so the program continues having passed a logic filter
and never use it to decide amongst two (or more) alternative flow paths
within the same code block/function.
In Erlang I only use 'if' as guard statements (and prefer 'when'). It makes
me far more confident in the quality of my code when I have no 'if'
statements at all.
- - Ben Scherrey
On Sun, Aug 15, 2021, 4:45 AM Michael P. <empro2@REDACTED> wrote:
> I once stumbled over someone's Erlang style guide,
> they wanted no `if`.
> It was not very well explained why,
> and there was no obvious e-mail address,
> or I wanted to first do
> some thorough thinking about it.
> Now I have no more than this:
>
> Is if not merely "syntactic sugar" for
> a fun/0 with guarded clauses?
> ```
> Now_this_is = (fun If() when Guard_seq_1 -> Expr_seq_1
> %% On and on and on
> ; If() when Guard_seq_N -> Expr_seq_N
> end)(),
> % or:
> Worst = case true
> of true when Guard_seq_1 -> Expr_seq_1
> ; true when Guard_seq_2 -> Expr_seq_2
> end.
> ```
> What could be perceived as wrong with `if`?
>
> ~M
>
> --
>
> But who will test the tests?
>
>
>
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20210815/6c0d16ee/attachment.htm>
More information about the erlang-questions
mailing list