[erlang-questions] Re: atoms used as status info, 'constants' etc

Witold Baryluk baryluk@REDACTED
Tue Aug 18 10:54:12 CEST 2009


Dnia 2009-08-18, wto o godzinie 09:29 +0100, Dave Pawson pisze:
> Hi Steve.
> 
> 2009/8/18 Steve Davis <steven.charles.davis@REDACTED>:
> > Dave,
> > Here's my 2c on this one.
> >
> > 1) reuse atoms that are also used as return values from other modules
> > in the OTP platform where it makes sense,
> 
> Makes sense. I'm using ok etc as I've seen it used.
> 
> 
> > 2) try to name the atoms cleverly for best grok-ability
> :-) OK. by the reader I guess.
> 
> 
> > 3) if you return an unpredictable number of status values, then use
> > "string" status values
> 
> See below*
> 
> > 3a) and... by "string" then probably it is "better" to use a binary
> > representation of the text, e.g. <<"my unpredictable return value">>
> 
> Never even seen that way of building a string!
> 
> 
> My problem is a function that should return, say, a record, yet
> the sought record does not exist?
>   Return a 'dummy' / null record or
>   Just a status value?
> Groking in the case would be by the Erlang code rather
> than the user.
> 
> suggestions on how to handle this please?
> 
> 
> regards
> 

Generally it depends. If there is good default value, you can use
"dummy" record, but mostly you want to distinguish beetween existing and
not existing data.

If you know that you always return record, anything which isn't record,
like atom 'notfound', or 'error', or {error, not_found} will be
sufficient, because in somewhere on calle side you will match returned
value to the record, and this will crash (which is good).



There are 4 main conventions in case of returning general values:

1. something which is distinguishable, because of type:
   return Tuple | false  (like list:keyfind/3)
   or     {ok, Value} | error   (like dict:find/2)
   or     {value, Value} | none (like gb_trees:lookup/2)
2. something which can be confused with value:
   return Value | undefined  (erlang:get/1)
   or     none | Value  (like proplists:lookup/2)
3. throw exception if record is not found (like dict:fetch/2,
   orddict:fetch/2)
4. provide argument for default return value, (like proplists:lookup/3).


-- 
Witold Baryluk <baryluk@REDACTED>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 197 bytes
Desc: To jest cz??? wiadomo?ci podpisana cyfrowo
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20090818/0117a32e/attachment.bin>


More information about the erlang-questions mailing list