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

Edwin Fine <>
Fri May 16 08:23:13 CEST 2008


Agreed... It's Bad Style. I was merely pointing out the syntactic
possibilities.

That being said, a bad programmer can screw up a program while using the
best programming language... and a good programmer can make something that
is usually Bad Style look better than the efforts of a bad programmer using
Good Style :)

On Thu, May 15, 2008 at 11:15 PM, Richard A. O'Keefe <>
wrote:

>
> 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.)
>
>
>
>
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080516/8bfb3ff3/attachment.html>


More information about the erlang-questions mailing list