[erlang-questions] 回复: 回复: How to make a short circuit in erlang ?

Raimo Niskanen raimo+erlang-questions@REDACTED
Tue Mar 26 09:22:37 CET 2013


On Tue, Mar 26, 2013 at 02:36:07PM +0800, 饕餮 wrote:
> Aha!Gotcha! 
> It is a good way to flat a multiple nested.It's truly not need to write 8 branches
>  example(Foo, Bar, Ugh) ->
>      case {Foo, Bar, Ugh}
>       of {false, _, _} -> . . .
>       ;  {true, false, _} -> . . .
>       ;  {true, true, false} -> . . .
>       ;  {true, true, true} -> . . .
>      end. 
> 
> 
> Use if could be better I think,like:
> example(Foo,Bar,Ugh) ->
> if
>     Foo =:= false -> ...
>     Bar =:= false  -> ...
>     Ugh =:= false -> ...
>     true -> ...
> (change some logic could remove the "=:= false", to make the code more clean)

Excuse me for jumping in in the middle without reading up, but that
is in fact a very illustrative point of view in this case.  I will
get back to that shortly.

If Foo, Bar and Ugh are booleans, that could be written as:
    if
    	not Foo -> ...;
	not Bar -> ...;
	not Ugh -> ...;
	true -> ...
    end

But since your code uses "Foo =:= false", I can not immediately
know if Foo could have a non-boolean value.  Therefore my code suggestion
may be incorrect.  In fact using "Foo =:= false" instead of just "if Foo ->"
_suggests_ that Foo is not just a boolean.  At least for someone
just jumping in, like me now, which is the situation for anyone reading
the code including the writer in the future.

And that is one reason why using "Foo =:= false" is bad coding practice
in any coding language.

> 
:
> 


-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB



More information about the erlang-questions mailing list