Erlang style - case vs function pattern matching -
I have found on the forum where I have written a lot of aloud codes, and I can see some style ( Bad or good) in the way I am writing it in the organism. I want some opinion on this special idiom - is it better (more readable / faster / whatever) to change the case style statement to function pattern matching?
Example
Compare (a follower example)
Case {Size & gt; 100000, type} {true, asset} -> gt; Do something to change something to %%; {False, food} - & gt; Do something to change %% some things; _ - & gt; Unseen end;
with
... maybe changestorage (size, type) ... maybe changestorage (size, eats) when size & gt; 10000 - & gt; something; Maybe changestorage (size, disc) when the size & lt; 10000 - & gt; anything else; Changes maybe (_, _) - & gt; Do not ignore it.
I like the latter in most cases but I would be interested in other opinions.
The second is the preferred method, especially if you can place segments in one line:
Maybe CngStor (Sz, ets) when Sz> 10000 - & gt; something; Perhaps CNGstore (SJ, Diset) when SJ & lt; 10000 - & gt; anything else; Perhaps CNGstore (_, _) - & gt; Do not ignore it.
It is very easy to read and argue about it. Always choose the style that will be easy to read in the future. Often you get a set of a segment where there is a 10 liner and the remaining one is row - the function breaks a long time:
probably CngStor (Sz, ets) when Sz & gt; 10000 - & gt; something; Perhaps CNGstore (SJ, Diset) when SJ & lt; 10000 - & gt; anything else(); Perhaps CNGstore (_, _) - & gt; Do not ignore it. Some () - & gt; (...) return.
Little things like to align them and use fewer variable names - but do not get into the trap of changing everything for P, Q, R .
A good trick If you use records, there is more to match the records to lowercase letters:
#record {foo = F, bar = B, baz = Bz} = Parameter
This gives you a small variable name which parachutes in the function, when 10,000 ft for the bug is searched after Christmas F is clearly a Foo, etc., etc ...
Comments
Post a Comment