[erlang-questions] Side-effects in "if" guards

Richard A. O'Keefe ok@REDACTED
Fri May 16 05:15:20 CEST 2008


On 16 May 2008, at 9:42 am, Edwin Fine wrote:
> I saw something like this in some Erlang code. It is possible to  
> achieve a similar effect without using "if" as follows:
>
> is_list(X) andalso string:to_lower(X) == X andalso length(X) == 5  
> and also my:no_duplicates(X) andalso do_something(X).

I respectfully suggest that it is Bad Style to put something with a side
effect in an 'andalso' or 'orelse'.  They are for evaluating conditions,
and if you put side effects, you are guaranteed to confuse people who
expect you to use them for that purpose.

	case is_list(X) andalso
	     string:to_lower(X) == X andalso
	     length(X) == 5 andalso
	     my:no_duplicates(X)
	  of true  -> do_something(X)
	   ; false -> ok
	end
is longer, but far far clearer.

--
"I don't want to discuss evidence." -- Richard Dawkins, in an
interview with Rupert Sheldrake.  (Fortean times 232, p55.)









More information about the erlang-questions mailing list