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

饕餮 249505968@REDACTED
Tue Mar 26 04:24:31 CET 2013

Something I just don't agree.
I use Something == false. 
Because I do not name the variable like Is_Something_Right (Apologize for my mike).
!Is_Something_Right could be a better code style.
!Something , in my opinion, is not better than Something == false.

I have see this code style in my project as you write.
example(Foo, Bar, Ugh) ->
    case {Foo, Bar, Ugh}
      of {false, _, _} -> . . .
       ; {true, false, _} -> . . .
       ; {true, true, false} -> . . .
       ; {true, true, true} -> . . .
But when look at the {true,true,false}.
I still need to check which one is true , and which is false (I'm just stupid as a duck).
And There could be 8 different status.
That means I should write 8 entry for 3 judgement
But if three are nest(Although I don't like it).Only need 3 entry. 
So I don't think this a good coding style.

I just wonder is there any way like return in other language could just return and ignore the step below.
And Now I know there are no way to do like that.
More function could be a good choice I think

------------------ 原始邮件 ------------------
发件人: "Richard A. O'Keefe"<ok@REDACTED>;
发送时间: 2013年3月26日(星期二) 上午10:50
收件人: "饕餮"<249505968@REDACTED>; 
抄送: "erlang-questions"<erlang-questions@REDACTED>; 
主题: Re: [erlang-questions] How to make a short circuit in erlang ?

On 26/03/2013, at 3:31 PM, 饕餮 wrote:

> When in other language.We can use like:
> example(Something){
>     if( Something == false)
>         return;
>     //do something here;
> }

Brian Marick of www.exampler.com has two great stickers.
One I have fixed to my door is "To be less wrong than yesterday."
The other is "An example would be useful about now" (from memory).

Even in C and Java, "if (Something == false)" is bad style.
You should write "if (!Something)".  The only time that
==false or ==true make sense is when there is a third possibility.

> But in Erlang.I have to write like:
> example(Something) ->
>     if
>         Something == false ->
>             false;
>         true ->
>             %do something here
>     end.

I personally prefer

    example(Something) ->
        case Something
          of false -> false
           ; true  -> . . .

and then go looking for the third case that probably exists.
There is pattern matching in the head.
There are guards to be used.
In fact, "early-exit" (as it's known in the Smalltalk world)
is often an ersatz for guard tests.

So the chances are that you should either
 - be rewriting for clarity, or
 - using guards.

I think a *real* example would be illuminating.
> That's could make the multiple nested like:
> example(Something) ->
>     if
>         Something == false ->
>             false;
>         true ->
>             if
>                 Otherthing == false ->
>                     false
>                 true ->
>                     %do something here
>     end.
> Some code could make 5 or more nested in it.

No.  SHOW US A REAL EXAMPLE.  And nothing stops you writing

example(Foo, Bar, Ugh) ->
    case {Foo, Bar, Ugh}
      of {false, _, _} -> . . .
       ; {true, false, _} -> . . .
       ; {true, true, false} -> . . .
       ; {true, true, true} -> . . .

We need a real example.  (Did I already say that?)

> Is there any idea to make short circuit to reduce of the nested?

Give us a real example and *then* we can have an illuminating

It's a funny thing.  I use early exit a lot in Smalltalk, rather
less so in C, and in ML and Haskell it never occurs to me that I
don't actually have it.  Erlang is definitely close to the ML end
of things.

I just spent a day turning 63 SLOC of code I wanted to use for a
class example of the importance of comments into 21 lines, which
annoyingly needs a lot fewer comments.  Part of it was to not be
scared of introducing auxiliary functions (in Java, yet!).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130326/79d428da/attachment.htm>

More information about the erlang-questions mailing list