Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Function design: make errors optional?

8 views
Skip to first unread message

Matthieu Villeneuve

unread,
Mar 20, 2002, 8:06:04 PM3/20/02
to
I was wondering if it could be accepted as "good style" to define a
function in a way that makes the caller able to decide whether, if an
error happens during its evaluation, the function should throw an error,
or produce a default result.

For example, an imaginary function that adds an edge between two
vertices of a graph could, depending on the value of a keyword parameter
NO-ERRORS, either throw an error if one of the vertices doesn't exist,
or silently add the missing vertices:

(defun add-edge (graph source dest &key (no-errors nil))
...)

This way, the caller could decide either to be responsible for all the
error handling (which could require a good knowledge of the underlying
mechanisms), or to let the function maintain the integrity of the data
involved (but that might not be always possible or desirable).

Any thougths? Are there any commonly accepted idioms about that issue?
Thanks,


--Matthieu

Barry Margolin

unread,
Mar 20, 2002, 8:13:16 PM3/20/02
to
In article <3C99320B...@tumbleweed.com>,

Matthieu Villeneuve <matthieu....@tumbleweed.com> wrote:
>I was wondering if it could be accepted as "good style" to define a
>function in a way that makes the caller able to decide whether, if an
>error happens during its evaluation, the function should throw an error,
>or produce a default result.
...

>Any thougths? Are there any commonly accepted idioms about that issue?
>Thanks,

Take a look at most of the standard input functions, which take EOF-ERROR-P
and EOF-VALUE arguments.

However, in many cases, the caller can simply use IGNORE-ERRORS when he's
not interested in the error details.

--
Barry Margolin, bar...@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

Thomas F. Burdick

unread,
Mar 20, 2002, 8:27:26 PM3/20/02
to
Matthieu Villeneuve <matthieu....@tumbleweed.com> writes:

> I was wondering if it could be accepted as "good style" to define a
> function in a way that makes the caller able to decide whether, if an
> error happens during its evaluation, the function should throw an error,
> or produce a default result.

Sure, see READ as an example of that.

> For example, an imaginary function that adds an edge between two
> vertices of a graph could, depending on the value of a keyword parameter
> NO-ERRORS, either throw an error if one of the vertices doesn't exist,
> or silently add the missing vertices:
>
> (defun add-edge (graph source dest &key (no-errors nil))
> ...)

I'd call the keyword arg ERROR-P to avoid the weird double-negative.

--
/|_ .-----------------------.
,' .\ / | No to Imperialist war |
,--' _,' | Wage class war! |
/ / `-----------------------'
( -. |
| ) |
(`-. '--.)
`. )----'

Kent M Pitman

unread,
Mar 20, 2002, 9:18:31 PM3/20/02
to
t...@famine.OCF.Berkeley.EDU (Thomas F. Burdick) writes:

> Matthieu Villeneuve <matthieu....@tumbleweed.com> writes:
>
> > I was wondering if it could be accepted as "good style" to define a
> > function in a way that makes the caller able to decide whether, if an
> > error happens during its evaluation, the function should throw an error,
> > or produce a default result.
>
> Sure, see READ as an example of that.

And see also my paper
http://world.std.com/~pitman/Papers/Exceptional-Situations-1990.html
for some conceptual foundation in this area.



> > For example, an imaginary function that adds an edge between two
> > vertices of a graph could, depending on the value of a keyword parameter
> > NO-ERRORS, either throw an error if one of the vertices doesn't exist,
> > or silently add the missing vertices:
> >
> > (defun add-edge (graph source dest &key (no-errors nil))
> > ...)
>
> I'd call the keyword arg ERROR-P to avoid the weird double-negative.

Some call it ERRORP heh.

Erik Naggum

unread,
Mar 21, 2002, 5:23:47 AM3/21/02
to
* Matthieu Villeneuve

| I was wondering if it could be accepted as "good style" to define a
| function in a way that makes the caller able to decide whether, if an
| error happens during its evaluation, the function should throw an error,
| or produce a default result.

You might find warn and signal useful in this regard, instead of error.
That way, your caller may set up a condition handler and decline to
handle a particular condition, or not set up any condition handlers and
effectively ignore the conditions, such that you can return a useful
default value instead.

| For example, an imaginary function that adds an edge between two
| vertices of a graph could, depending on the value of a keyword parameter
| NO-ERRORS, either throw an error if one of the vertices doesn't exist,
| or silently add the missing vertices:

I think a keyword argument like :if-does-not-exist with a :create or
:error argument would be a good design choice here. See open.

| Any thougths? Are there any commonly accepted idioms about that issue?

Well, there are several. read has already been mentioned.

///
--
In a fight against something, the fight has value, victory has none.
In a fight for something, the fight is a loss, victory merely relief.

Thomas F. Burdick

unread,
Mar 21, 2002, 2:29:16 PM3/21/02
to

Yeah ... I was actually lying, *I* would call it ERROR?, which
probably contributes to the difficulty I have reading things like
ERRORP (which my brain unfortunatey parses as ER RORP)

Barry Margolin

unread,
Mar 21, 2002, 2:42:56 PM3/21/02
to
In article <xcvpu1x...@apocalypse.OCF.Berkeley.EDU>,

Thomas F. Burdick <t...@apocalypse.OCF.Berkeley.EDU> wrote:
>Kent M Pitman <pit...@world.std.com> writes:
>
>> t...@famine.OCF.Berkeley.EDU (Thomas F. Burdick) writes:
>>
>> > Matthieu Villeneuve <matthieu....@tumbleweed.com> writes:
>>
>> > > (defun add-edge (graph source dest &key (no-errors nil))
>> > > ...)
>> >
>> > I'd call the keyword arg ERROR-P to avoid the weird double-negative.
>>
>> Some call it ERRORP heh.
>
>Yeah ... I was actually lying, *I* would call it ERROR?, which
>probably contributes to the difficulty I have reading things like
>ERRORP (which my brain unfortunatey parses as ER RORP)

As you program in Common Lisp some more, you'll probably get used to
parsing XXXP as XXX-P and interpreting -P as '?'.

Thomas F. Burdick

unread,
Mar 21, 2002, 4:30:28 PM3/21/02
to
Barry Margolin <bar...@genuity.net> writes:

> In article <xcvpu1x...@apocalypse.OCF.Berkeley.EDU>,
> Thomas F. Burdick <t...@apocalypse.OCF.Berkeley.EDU> wrote:
> >Kent M Pitman <pit...@world.std.com> writes:
> >
> >> t...@famine.OCF.Berkeley.EDU (Thomas F. Burdick) writes:
> >>
> >> > Matthieu Villeneuve <matthieu....@tumbleweed.com> writes:
> >>
> >> > > (defun add-edge (graph source dest &key (no-errors nil))
> >> > > ...)
> >> >
> >> > I'd call the keyword arg ERROR-P to avoid the weird double-negative.
> >>
> >> Some call it ERRORP heh.
> >
> >Yeah ... I was actually lying, *I* would call it ERROR?, which
> >probably contributes to the difficulty I have reading things like
> >ERRORP (which my brain unfortunatey parses as ER RORP)
>
> As you program in Common Lisp some more, you'll probably get used to
> parsing XXXP as XXX-P and interpreting -P as '?'.

Well, two and a half years so far, and no sign of my reading errorp
any better, yet. I have no problem with -P, just with the no-hyphen
version. In my own code, I'm quite consistent: ERROR? and READ-ERROR?
(signal an error on read?), but READER-ERROR-P (is this a reader-error?).

0 new messages