[erlang-questions] case statements really required??

kiran <>
Sat Dec 5 07:53:56 CET 2009


Attila Rajmund Nohl wrote:
> 2009/12/4, kiran <>:
>   
>> hi,
>> is it good to use case statements in functional language..  according to
>> me .. pattern matching-functions could do the task.
>> any commnets??
>>     
>
> Using pattern matching leads to "function fragmentation", i.e. to
> loads of one liner function cases and it gets hard to follow the
> control flow. Let's consider this code:
>
> check_language(Module) ->
>     case catch Module:module_info(exports) of
> 	{'EXIT', _Reason} ->
> 	    not_blm;
> 	Exports ->
> 	    case {lists:member({blm_cmd, 0}, Exports),
> 		  lists:member({blm_error, 1}, Exports)} of
> 		{true, true} ->
> 		    {ok, blm_lang};
> 		_ ->
> 		    not_blm
> 	    end
>     end.
>
> 12 lines, does what it's named (returns {ok, blm_lang} if the two
> specified functions are exported from the module, otherwise returns
> not_blm. Without case the code would look like this:
>
> check_language(Module) ->
>    really_check_language(catch Module:module_info(exports)).
>
> really_check_language({'Exit', _Reason}) ->
>   not_blm;
>
> really_check_language(Exports) ->
>   check_exported_functions(
>     lists:member({blm_cmd,0},Exports),
>     lists:member({blm_error, 1}, Exports));
>
> check_exported_functions(true, true) ->
>   {ok, blm_lang};
>
> check_exported_functions(_, _) ->
>   not_blm.
>
> 16 lines of screen used (and no documentation comments, type
> specifications, etc. are written, they could add a couple of more
> lines), a stupid function name had to be invented, etc. Consider the
> equivalent in Java:
>
>     String checkLanguage(String module) {
>         try {
>             int f=0;
>             for (Method m : Class.forName(module).getMethods()) {
>                 if (m.getName().equals("blm_cmd") ||
> m.getName().equals("blm_error")) {
>                   f++;
>                 }
>             }
>             return (f==2?"{ok, blm_lang}":"not_blm");
>         }
>         catch (ClassNotFoundException e) {
>             return "not_blm";
>         }
>     }
>
> 15 lines, but 5 of them contain only "}" (the original Erlang also
> contained two lines of only "end", so it's the same length).
>
> ________________________________________________________________
> erlang-questions mailing list. See http://www.erlang.org/faq.html
> erlang-questions (at) erlang.org
>
>   
I dont think the no of lines matter "so much" (4-5 lines diff is not 
much) .. what matters to me more is whether the code is "functional" 
(following fp) or not.
In fact no of lines can be saved using pattern matching functions in 
most of the scenarios if thats the concern.
Using functions and pattern matching makes the code more functional and 
can be more optimized by erlang vm than using cases (am i right?? 
comments please ... ).

-- 
Kiran Khaladkar | Software Engineer
Geodesic Limited | www.geodesic.com
Tel: +91 22 4031 5800 -  ext -  225
Mob: 9870587508



More information about the erlang-questions mailing list