A programming convention

Ulf Wiger etxuwig@REDACTED
Tue Jun 11 10:04:47 CEST 2002

On Tue, 11 Jun 2002, Samuel Tardieu wrote:

>I prefer Joe's solution: an exception should stay exceptional.
>The reason for that the compiler designers may choose to make an
>exception handler as cheap as possible ("zero cost") when no
>exception is raised, while an exception may take a lot of time
>to be processed when it is raised.

The only thing I did was to change the names around so that the
semantics of the code were consistent with Joe's actual

|    lookupQ(Key, Dict) ->
|       case (catch lookup(Key, Dict)) of
|          {'EXIT', Why} ->
|             {error, Why};
|          Other ->
|             {ok, Other}
|       end.

I guess what you're proposing is that the following example would
be more appropriate:

   lookup(Key, Dict) ->
      case lookupQ(Key, Dict) of
         {error, Why} ->
         {ok, Value} ->

which, of course, could be used as a general pattern:

  lookup(Key, Dict) ->
     no_maybe(lookupQ(Key, Dict)).
  lookupQ(Key, Dict) ->
     %% assuming that dict is a Key-Value list
     case lists:keysearch(Key, 1, Dict) of
        {value, {_Key, Value}} ->
           {ok, Value};
        false ->
           {error, not_found}

   %% the general utility function, which should be inlined
   no_maybe({ok, Value}) -> Value;
   no_maybe({error, Why}) -> exit(Why).

Ulf Wiger, Senior Specialist,
   / / /   Architecture & Design of Carrier-Class Software
  / / /    Strategic Product & System Management
 / / /     Ericsson Telecom AB, ATM Multiservice Networks

More information about the erlang-questions mailing list