[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-users] Handling Errors
From: |
Andy Bennett |
Subject: |
Re: [Chicken-users] Handling Errors |
Date: |
Sun, 14 Dec 2014 13:43:45 +0000 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.2.0 |
Hi,
> (define (foo lis) (cdr lis))
>
> Obviously LIS should neither be an empty list nor a list with only one
> element.
> What is the recommended way to deal with such constraints? Should I
> enforce them by writing conditionals at the beginning of the
> function[1]? Or is it conventional to just let the function fail and
> catch the error somewhere else (probably in a top-level function)?
>
> [1] This quickly leads to dirty and not-easy-to-read code.
>
> What say you seasoned schemers on this?
That depends on the scope of the accessor. I often use these kinds of
things for quick and dirty data structures for passing seeds for fold
around. In that case I use the even more concise '(define foo cdr)'.
In the case where other modules are using the API then I might go to
more trouble but then you have to think about whether, in the error cases:
+ To return successfully with a sentinel value.
This is valid in some cases, depending on the data semantics. It can
be implemented using the conditionals that you mention.
+ To use assert at the head of the procedure to validate the data
structure. This doesn't stop the error but it gives a more debuggable
message.
+ To catch and re-throw the exception.
This is fraught with anti-patterns, is a lot of work and you have to
do it really carefully. I hardly ever do this.
Regards,
@ndy
--
address@hidden
http://www.ashurst.eu.org/
0x7EBA75FF
signature.asc
Description: OpenPGP digital signature