[erlang-questions] DRY principle and the syntax inconsistency in fun vs. vanilla functions

Richard O'Keefe ok@REDACTED
Fri May 20 11:09:32 CEST 2011


On 19/05/2011, at 8:48 PM, Steve Davis wrote:

> Out of interest, I tried it on one of my own modules.
> 
> Your way: https://gist.github.com/980420
> The highway: https://github.com/komone/qrcode/blob/master/src/base32.erl
> 
> In order to make your way less confusing, as the pattern matching entry points to the function were extremely un-obvious, and in the spirit of making it work I tried "double indent".
> Double indented: https://gist.github.com/980412
> 
> For me, either way, the intention is harder to read, and/or forces me to focus on/put in a bunch of white space.

If instead of
encode0(<<>>, Acc) ->
		Acc;
	(<<A:5, B:3>>, Acc) ->
		<<Acc/binary, (b32e(A)), (b32e(B bsl 2)), "======">>;
	(<<A:5, B:5, C:5, D:1>>, Acc) ->
		<<Acc/binary, (b32e(A)), (b32e(B)), (b32e(C)), (b32e(D bsl 4)), "====">>;
	(<<A:5, B:5, C:5, D:5, E:4>>, Acc) ->
		<<Acc/binary, (b32e(A)), (b32e(B)), (b32e(C)), (b32e(D)), (b32e(E bsl 1)), "===">>;
	(<<A:5, B:5, C:5, D:5, E:5, F:5, G:2>>, Acc) ->
		<<Acc/binary, (b32e(A)), (b32e(B)), (b32e(C)), (b32e(D)), (b32e(E)), (b32e(F)), (b32e(G bsl 3)), "=">>.

you wrote

encode0
  (<<>>, Acc) ->
    Acc
; (<<A:5, B:3>>, Acc) ->
    <<Acc/binary, (b32e(A)), (b32e(B bsl 2)), "======">>
; (<<A:5, B:5, C:5, D:1>>, Acc) ->
    <<Acc/binary, (b32e(A)), (b32e(B)), (b32e(C)), (b32e(D bsl 4)), "====">>
; (<<A:5, B:5, C:5, D:5, E:4>>, Acc) ->
    <<Acc/binary, (b32e(A)), (b32e(B)), (b32e(C)), (b32e(D)), (b32e(E bsl 1)), "===">>
; (<<A:5, B:5, C:5, D:5, E:5, F:5, G:2>>, Acc) ->
    <<Acc/binary, (b32e(A)), (b32e(B)), (b32e(C)), (b32e(D)), (b32e(E)), (b32e(F)), (b32e(G bsl 3)), "=">>.

it might almost be readable.  You would then have a visual clue (; in the
first column) where each clause began and that these were clauses of a
top-level plain function.

But only "almost".  I've seen too much code like that in other languages and
never want to see it again.

The problem is not that adding this misbegotten teratoma of a feature to the
language would force me to *write* like that, but that if other people picked
it up, it would force me to *read* code like that.

Life is too short.




More information about the erlang-questions mailing list