Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #22547

Re: Design decisions

Message-ID <4FE4A5BF.5080509@loria.fr> (permalink)
Date 2012-06-22 19:05 +0200
From Jens Gustedt <jens.gustedt@loria.fr>
Newsgroups comp.lang.c
Subject Re: Design decisions
References <jrvvlb$8k1$1@speranza.aioe.org>
Organization Guest of ProXad - France

Show all headers | View raw


Hello,

Am 21.06.2012 22:19, schrieb jacob navia:
> In the new "templated" form of the containers library I have now that
> all data retrieving functions return the data directly:
> 
> int (*GetElement)(intList *l,size_t idx);
> 
> The problem is that it is not possible to return an error
> with this configuration: Since the return value is an
> int, any error code could be actually some data!!
> 
> The solution is to return a pointer to the data and NULL
> if there is an error, as the other functions of the library do.
> 
> But this doesn't look great with the syntax viewed from the
> user's side:
> 
> Instead of:
>     int m = iintList.GetElement(mylist,23);
> you should write:
>     int m = *iintList.GetElement(myList,23);
> 
> risking a core dump if there is a NULL return.
> 
> Obviously if the list has only 20 elements, the core dump
> is actually a good thing since it will show you the error immediately.
> 
> Before returning, the "GetElement" API calls the error function
> and will return ONLY IF the error function returns.
> 
> I was relying in that as error handling, and just returning zero...
> 
> After some reflection I am convinced now that this would be a bad design
> decision.
> 
> What do you think?

I would go for a solution similar to what some (already) standard
functions do when they could return optional information. Have an
additional pointer argument to store that information. If that pointer
argument is 0, the user isn't interested.

(You could combine that with techniques that provide the feature of
optional arguments to C functions/macros in a way that most users
wouldn't actually have to know that this optional parameter exists.)

Jens

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Design decisions jacob navia <jacob@spamsink.net> - 2012-06-21 22:19 +0200
  Re: Design decisions Andrew Cooper <amc96@cam.ac.uk> - 2012-06-21 21:43 +0100
  Re: Design decisions "BartC" <bc@freeuk.com> - 2012-06-21 22:02 +0100
    Re: Design decisions Gareth Owen <gwowen@gmail.com> - 2012-06-21 23:23 +0100
      Re: Design decisions jacob navia <jacob@spamsink.net> - 2012-06-22 09:12 +0200
        Re: Design decisions Ian Collins <ian-news@hotmail.com> - 2012-06-22 19:44 +1200
          Re: Design decisions "Joe." <antelope3@news.net> - 2012-06-25 00:32 -0600
        Re: Design decisions gwowen <gwowen@gmail.com> - 2012-06-22 01:00 -0700
          Re: Design decisions jacob navia <jacob@spamsink.net> - 2012-06-22 10:08 +0200
            Re: Design decisions Gareth Owen <gwowen@gmail.com> - 2012-06-22 17:18 +0100
              Re: Design decisions jacob navia <jacob@spamsink.net> - 2012-06-22 18:27 +0200
                Re: Design decisions Ian Collins <ian-news@hotmail.com> - 2012-06-23 08:44 +1200
                Re: Design decisions jacob navia <jacob@spamsink.net> - 2012-06-22 23:58 +0200
                Re: Design decisions Ian Collins <ian-news@hotmail.com> - 2012-06-23 10:21 +1200
                Re: Design decisions jacob navia <jacob@spamsink.net> - 2012-06-23 02:13 +0200
                Re: Design decisions Ian Collins <ian-news@hotmail.com> - 2012-06-23 12:59 +1200
                Re: Design decisions jacob navia <jacob@spamsink.net> - 2012-06-23 11:03 +0200
                Re: Design decisions Ian Collins <ian-news@hotmail.com> - 2012-06-23 21:52 +1200
                Re: Design decisions jacob navia <jacob@spamsink.net> - 2012-06-23 12:32 +0200
                Re: Design decisions Gareth Owen <gwowen@gmail.com> - 2012-06-23 13:54 +0100
            Re: Design decisions "Joe." <antelope3@news.net> - 2012-06-25 00:36 -0600
          Re: Design decisions jacob navia <jacob@spamsink.net> - 2012-06-22 10:42 +0200
            Re: Design decisions Gareth Owen <gwowen@gmail.com> - 2012-06-22 17:18 +0100
            Re: Design decisions jacob navia <jacob@spamsink.net> - 2012-06-24 18:32 +0200
    Re: Design decisions James Kuyper <jameskuyper@verizon.net> - 2012-06-21 18:43 -0400
      Re: Design decisions "Joe." <antelope3@news.net> - 2012-06-25 00:40 -0600
    Re: Design decisions "Joe." <antelope3@news.net> - 2012-06-25 00:28 -0600
  Re: Design decisions "io_x" <a@b.c.invalid> - 2012-06-22 08:35 +0200
  Re: Design decisions Neil Cerutti <neilc@norwich.edu> - 2012-06-22 12:40 +0000
    Re: Design decisions gwowen <gwowen@gmail.com> - 2012-06-22 06:01 -0700
      Re: Design decisions Neil Cerutti <neilc@norwich.edu> - 2012-06-22 14:26 +0000
    Re: Design decisions Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-22 14:01 +0100
    Re: Design decisions David Resnick <lndresnick@gmail.com> - 2012-06-22 06:13 -0700
  Re: Design decisions Jens Gustedt <jens.gustedt@loria.fr> - 2012-06-22 19:05 +0200
  Re: Design decisions "Joe." <antelope3@news.net> - 2012-06-25 00:27 -0600

csiph-web