[erlang-questions] The If expression

Fredrik Svahn fredrik.svahn@REDACTED
Fri Jan 23 23:35:52 CET 2009


On Fri, Jan 23, 2009 at 3:54 AM, Richard O'Keefe <ok@REDACTED> wrote:
>
> On 2 Jan 2009, at 9:14 am, Fredrik Svahn wrote:
> I'm sorry for the late reply, but December/January is the
> long holiday in New Zealand, and I only got back yesterday.

Thanks for the reply. To be honest I had put my plans to write an EEP
on hold because I only got one reply which was mostly disagreeing.
Using expressions in 'if'-expressions is more of a nice-to-have for
me, so I will just keep on using 'case'.

A few comments below...

>  - It would give us two very *different* conditional structures
>   with the *same* keyword, where it is hard to tell which one
>   was intended until several lines later.

I agree that this is a disadvantage with the proposal. I also agree
with your statement below that if the old sense of 'if' is rare (or
even discouraged), we can probably live with the confusion.

>- 'if' isn't actually all that rare.  In some code I wrote last
>  year, I find one 'if' for every four 'case's.  The OTP sources
>  have 1 if/10 case, which is less common, but not _that_ rare.
>  If the old sense of 'if' were rare, we could probably live with
>  the confusion.

Newcomers to this mailing list asking how to use "if" are invariably
recommended to use "case" instead. Combined with the fact that you can
only use guards it is almost FAQ material. Considering this and all
the beating the 'if' expression has gotten over the years on the
mailing list my assumption was that the use of 'if' was really rather
infrequent even outside of OTP. I might be wrong.

(Hmmm, I just checked, and there actually is a chapter about 'if':s in
the FAQ. It is recommending the use of 'case' and even states that
"case is used much more frequently").

>  - The new syntax is not consistent with Erlang "style", which
>   expects multiple arms as a matter of routine (like the Fortran,
>   Ada, Eiffel, Algol 68 "block if" constructs, but without an "else").

Just out of curiosity, is there any other programming language out
there which has an 'if' and which does not have an 'else' apart from
the Commodore 64 BASIC? I am not saying Erlang should have because
everyone else does. I am just wondering out of curiosity.

Even Haskell has an if ... else construct (which like this proposal is
syntactic sugar for (or equivalent with) a case construct).

>  - As a result the new syntax is rather inconvenient.  The majority
>   of my existing uses of 'if' have more than one arm, so would need
>   nested new-'if's.  It's hard to see that as an improvement.

The proposal is backwards compatible (with the obvious exception that
'else' would need to become a keyword) so there is no need for you to
change your coding style.

> About 40 years ago when enumeration types were introduced it
> was pointed out that people over-used Boolean.  I've been guilty
> of that myself.  In Erlang, our aim is to write reliable and
> maintainable software.  We shouldn't regard it as a PROBLEM
> that Boolean expressions are hard to use, we should regard it
> as a heaven-sent OPPORTUNITY to critically, and I really do mean
> SERIOUSLY critically review all uses of Boolean to see whether
> they are really appropriate.  My own experience is that in the
> majority of cases they are not.

There is a lot of code out there to critically review, then. In fact
'true' is the second most popular atom in the OTP code, and 'false' is
the fourth most popular.

percent  count atom
7.749% 15074 ok
7.489% 14568 true
4.908% 9547 error
4.895% 9523 false
1.574% 3061 mandatory
1.025% 1994 undefined
0.912% 1774 EXIT
0.904% 1758 reply
0.870% 1693 value

> (My use of 'if' is almost entirely arithmetic comparisons.)

What else can it really be used for? Ok, there are a couple of guard
bifs, but that is basically it, isn't it?

BR /Fredrik



More information about the erlang-questions mailing list