ok or true?

Ulf Wiger ulf.wiger@REDACTED
Mon Feb 23 00:24:48 CET 2004

On Sun, 22 Feb 2004 15:23:03 -0500, Shawn Pearce <spearce@REDACTED> 

> Very good point.  I find it interesting the port BIFs return true only.
> If they have an error to signal, they exit(Error) instead.  Which means
> its unnecessary to actually check their return values.
> I'll try to keep your point in mind, if I return true, the only other
> value should be false, making it a perfectly valid boolean function.

I would agree with this. I would not use the return value 'true'
for anything other than a boolean function with true/false/exit()

A function that returns ok would be expected to perform a side effect
(obviously, since it would otherwise be useless.) Such a function
should, if it fails, return either {error, Reason} or exit.
If possible, the function should limit itself to one basic operation,
not the "ok: I've done lots of stuff" semantics.

The older (and hopefully wiser) I get, I try more and more to avoid
writing functions that simply return ok. gen_tcp:send/2 is a good
example of where it's appropriate.

Less appropriate uses are e.g.:

create_mnesia_tables() -> ..., ok.

(It would be better to have a function mnesia_tables() returning a
list of tables and properties, and another that creates them, e.g.
create_tables(Tabs) -> ok.)


> Vance Shipley <vances@REDACTED> wrote:
>> I would expect a function which returns true to have only
>> one other possible return value; false.  It is my observation
>> that most functions which always succeed return true.  I
>> would find a function which returned true or {error, Reason}
>> to be quite odd.

Ulf Wiger

More information about the erlang-questions mailing list