[erlang-questions] Reassigning variables

Richard O'Keefe ok@REDACTED
Thu Mar 19 05:19:33 CET 2009


On 18 Mar 2009, at 11:48 pm, Attila Rajmund Nohl wrote:
>> }      if requested ad size is bad:
>> }          return {skip, bad_size}
>> }      if the game has ads disabled:
>> }          return {skip, disabled}
>> }      if the game is filtering a domain:
>> }          return {skip, domain_filtered}
>> }      return choose_ad()
> 7 lines (easily fit into one screen) vs 25 lines (probably not much
> other than this code is shown on the screen) full of no-ops like "_ ->
> ok" - a really bad idea in my opinion.

Here it is using the escape/1 function I posted earlier today.

-define(when(Clause), if Clause ; true -> ok end).

escape(fun (Return) ->
     ?when(requested_ad_size_is_bad()  -> Return({skip,bad_size})),
     ?when(the_game_has_ads_disabled() -> Return({skip,disabled})),
     ?when(it_is_filtering_a_domain()  ->  
Return({skip,domain_filtered})),
     choose_ad()
end)

Not that I endorse this, but
   + exceptions
   + higher order functions
   + macros
is an amazing combination.




More information about the erlang-questions mailing list