A programming convention

Chris Pressey cpressey@REDACTED
Thu Jun 13 00:52:58 CEST 2002


On Tue, 11 Jun 2002 11:34:57 +0200
Thomas Arts <thomas@REDACTED> wrote:

> 
> Why dthen not introducing a higher order function. After
> all, we use a functional language ;0)
> 
> >    lookupQ(Key, Dict) ->
> >       case (catch lookup(Key, Dict)) of
> >          {'EXIT', Why} ->
> >             {error, Why};
> >          Other ->
> >             {ok, Other}
> >       end.
> 
> In the Erlang style:
> 
> query(F,Args) ->
>   case (catch F(Args)) of
>        {'EXIT',Why}
>           {error,Why};
>        Other ->
>           {ok,Other}
>   end.

Since often in the case of failure one might just want to supply a
reasonable default value, I would even go so far as to propose

  attempt(F,Args,Default) ->
    case (catch F(Args)) of
         {'EXIT',_} -> Default;
         Other      -> Other
    end.

> This makes is very easy to find the places in you code
> where you use this design pattern. The Q as addition is
> more add-hoc and not as clear from a program analysis
> point of view.

Totally agreed.  To me there is not much sense establishing a convention,
if there is some stronger way to enforce it, like adding a language
feature (or in this case even just a higher-order function).

-Chris



More information about the erlang-questions mailing list