# Old style vs. new style boolean expressions

James Hague jamesh@REDACTED
Tue Oct 8 17:50:41 CEST 2002

```Here is a function written in classic Erlang style:

is_alphanum(N) when N >= \$A, N =< \$Z -> true;
is_alphanum(N) when N >= \$a, N =< \$z -> true;
is_alphanum(N) when N >= \$0, N =< \$9 -> true;
is_alphanum(\$_) -> true;
is_alphanum(_) -> false.

This can also be written using boolean expression guards, which were
introduced in (I think) R8:

is_alphanum(N) ->
((N >= \$A) and (N =< \$Z)) or
((N >= \$a) and (N =< \$z)) or
((N >= \$0) and (N =< \$9)) or
(N == \$_).

This isn't as efficient as the first method, because the boolean expression
in the second code snippet is always fully evaluated.  To fix this, use
"andalso" and "orelse":

is_alphanum(N) ->
((N >= \$A) andalso (N =< \$Z)) orelse
((N >= \$a) andalso (N =< \$z)) orelse
((N >= \$0) andalso (N =< \$9)) orelse
(N == \$_).

Now the point:  The first vesion generates by far the smallest beam file,
the second is somewhat larger, and the third is still larger.  When fiddling
with a module containing many similar functions, switching from first to the
third method increased the beam file size by 40%.

Is this just because boolean expressions are a relatively new addition, and
so the R8 compiler doesn't deal with them well, or is it deeper than that?

I'm still not sure whether I think the first or third method is cleaner :)

```