Erlang style - case vs function pattern matching

后端 未结 6 1623
眼角桃花
眼角桃花 2020-12-31 02:39

I\'ve got to the stage where I\'ve written quite a bit of Erlang code now, and I can see some style (bad or good) creeping into the way I\'ve been writing it. This particula

6条回答
  •  长情又很酷
    2020-12-31 02:44

    The second is the preferred way especially if you can keep the clauses to a single line:

    maybeCngStor(Sz, ets)  when Sz > 10000 -> something;
    maybeCngStor(Sz, dets) when Sz < 10000 -> somethingelse;
    maybeCngStor(_,_)                      -> ignoreit.
    

    Makes it very easy to read and reason about. Always choose the style that will be easiest to read in the future. Often you find a set of clauses where one is a 10 liner and the rest are one lines only - break out the long one to a function:

    maybeCngStor(Sz, ets)  when Sz > 10000 -> something;
    maybeCngStor(Sz, dets) when Sz < 10000 -> somethingelse();
    maybeCngStor(_,_)                      -> ignoreit.
    
    somethingelse() ->
       (...)
       Return.
    

    Little things like laying out the clauses to align them and using short variable names matter - but don't fall into the trap of changing everything to P, Q, R.

    A good trick if you use records a lot is to match out the records to short variables:

    #record{foo = F, bar = B, baz = Bz} = Parameter
    

    This gives you short variable names that make sense when you parachute into the function from 10,000 feet looking for a bug next Christmas. F obviously is a Foo, etc, etc...

提交回复
热议问题