开发者

how to simplify/improve an Erlang code?

How a good Erlang programmer would write this code ?

loop(expr0) ->  
    case expr1 of   
    true ->  
       A = case expr2 of  
               true -> ...;  
        开发者_开发问答       false -> ...  
           end;
    false->  
       A = case expr3 of  
               true -> ...;  
               false -> ...  
           end  
  end,  
loop(expr4(A)).


Generally speaking, you want to make your code more readable. It's usually a good idea extracting bits of code to functions to avoid long or deeply nested functions, and provide self-explained names that clarify the purpose of a piece of code:

loop(expr0) ->  
    case expr1 of   
    true ->  
       A = do_something(expr2);
    false->  
       A = do_something_else(expr3)
  end,  
  loop(expr4(A)).

do_something(E) ->
    case E of  
       true -> ...;  
       false -> ...  
    end

do_something_else(E) ->
    case E of  
       true -> ...;  
       false -> ...  
    end

Now, a casual reader knows that your function does something if expr1 is true and something else if expr1 is false. Good naming conventions help a lot here. You can also do that with comments, but code is never outdated, and thus easier to maintain. I also find short functions rather easier to read than really loooong functions. Even if those long functions have coments inlined.

Once you've stated clearly what your function does, you may want to shorten the code. Short code is easier to read and maintain, but don't shorten too much using "clever" constructions, or you'll obscure it, which is the opposite of what you want. You can start by using pattern matching in function heads:

loop(expr0) ->  
    case expr1 of   
    true ->  
       A = do_something(expr2);
    false->  
       A = do_something_else(expr3)
  end,  
  loop(expr4(A)).

do_something(true) -> ...;
do_something(false) -> ....

do_something_else(true) -> ...;
do_something_else(false) -> ....

Then, you can avoid repeating A in the main function (aside, variables scoped out of nested statements is a feature I always disliked)

loop(expr0) ->  
  A = case expr1 of   
        true -> do_something(expr2);
        false-> do_something_else(expr3)
      end,  
  loop(expr4(A)).

do_something(true) -> ...;
do_something(false) -> ....

do_something_else(true) -> ...;
do_something_else(false) -> ....

And I think that's it for this piece of code. With more context you can also go for some abstractions to reduce duplicity, but take care when abstracting, if you overdo it you'll also obscure the code again, losing the maintenance benefit you'd expected to get by removing similar code.


The code, as it is currently written, is hard to make simpler. The problem are the ExprX entries are unknown, so there is no way to simplify the code without knowing that it is beneficial to do so. If you have a more full example, we will have a much better time at attempting to do an optimization of it.

The concrete problem is that we don't know how Expr2 and Expr3 depends on Expr1 for instance. And we don't know what the purpose of Expr0 is, and neither about Expr4's dependence other than it uses the returned A.


Why need expr0 in loop function?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜