[erlang-questions] Newbie question: avoiding massive 'case' statement
Michael McDaniel
erlangy@REDACTED
Wed Mar 5 22:30:32 CET 2008
maybe something like or a variation of ...
TransactionType = "ZABC" , %% or however it gets loaded
Request = "some appropriate request for TransactionType" ,
TT = string:to_lower(TransactionType) , %% no problem if already lower
erlang:apply( erlang:list_to_atom(TT), process_request, [Request] ) ,
%% and catch the exception from the apply
~Michael
On Thu, Mar 06, 2008 at 07:59:05AM +1100, David Mitchell wrote:
> Hello everyone,
>
> Newbie alert...
>
> I've got a block of code that looks like:
>
> case TransactionType of
> "ZABC" ->
> zabc:process_request(Request);
> "Z123" ->
> z123:process_request(Request);
> "YPQX" ->
> ypqx:process_request(Request);
> ...
> TransactionType ->
> error_logger:info_msg("Unknown transaction type")
> end
>
> This is all fine and dandy, but I'm expecting the 'case' statement to
> grow to handle several hundred different values of TransactionType
> over time. The code is going to get unwieldy in size, and the
> likelihood of mistakes in cut/paste updating of the 'case' block is
> going to get pretty high.
>
> As an alternative to the above, is there a way to do something like
>
> try
> &&&TransactionType&&&:process_request(Request)
> catch
> error_logger:info_msg("Unknown transaction type")
> end
>
> where &&&TransactionType&&&:process_request(...) is some "magic code"
> that lets me call the process_request function in the module that has
> the name of the value of TransactionType (hopefully that's clear, if
> not necessarily English)?
>
> Alternately, is there some sort of refactoring I could do to avoid the
> massive 'case' statement?
>
> There may be some obvious solution to do this, but I haven't been able
> to find it...
>
> Thanks in advance
>
> Dave Mitchell
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
--
Michael McDaniel
Portland, Oregon, USA
http://autosys.us
+1 503 283 5284
More information about the erlang-questions
mailing list