A programming convention

Thomas Lindgren thomas.lindgren@REDACTED
Thu Jun 13 09:55:12 CEST 2002


I think the Right Way is to collect these "coding patterns"
in an OTP library, just like 'lists'.

That way, they will really become a part of the programming
practice. (OTP defines the de facto practice, I'd say :-)

Best,
					Thomas

-----Original Message-----
From: owner-erlang-questions@REDACTED
[mailto:owner-erlang-questions@REDACTED]On Behalf Of Chris Pressey
Sent: den 13 juni 2002 00:53
To: erlang-questions@REDACTED
Subject: Re: A programming convention


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