[erlang-questions] On the usage of tests in guards

Richard O'Keefe ok@REDACTED
Tue Mar 3 03:39:09 CET 2009


Recently I was involved in an e-mail argument about whether
it was true that 'when' was a design mistake in Erlang (I
say NO) and whether it was true that most uses of 'when'
could be replaced by type tests inside patterns (I say no,
and even if they could be, they shouldn't be).

It occurred to me to get some numbers to see what other
people were doing, and I ran some fairly crude tools over
the R12B-5 distribution, finding that about 50% of the tests
in guards were _not_ type tests, and that this also seemed
to be true of 'receive' guards.

Wondering whether this would generalise, I just took a look
at the MochiWeb sources.  This looked at all 'when' guards
(not at 'if', sadly, but then, we hadn't been arguing about
'if').  Here's the summary.

  19     is_atom
  26     is_binary
  19     is_integer
   7     is_float
  38     is_list
   3     is_number
   1     is_pid
   6     is_tuple
---     --------
119     Total type tests

  58     =:= (really "member")
   4     =/=
  10     ==
  11     >
  61     >=
  20     <
  48     =<
---     ---------
212     Total comparisons

So comparisons form about 2/3 of the guard tests in MochiWeb,
an even higher proportion than OTP.

A lot of the >= and =< tests were character ranges,
and many of the =:= tests were in guards like
	X =:= c1 ; X =:= c2 ; X =:= c3
checking for specific characters (or occasionally other literals).








More information about the erlang-questions mailing list