Re: Why leave the error handling to the caller?
- From: "Malcolm McLean" <regniztar@xxxxxxxxxxxxxx>
- Date: Sat, 23 Jun 2007 07:40:45 +0100
"Richard Heathfield" <rjh@xxxxxxxxxxxxxxx> wrote in message news:crOdnbKFN_t84-bbnZ2dnUVZ8vmdnZ2d@xxxxxxxxx
Malcolm McLean said:The problem is you start introducing dependencies.
<snip>
No, the specification was to produce a function that filled out the
structure giving a list of strings, from an input consisting of a line
of strings separated by commas.
Separate the concept of a list of strings from the concept of input
parsing.
Now separate the concept of a list from the concept of a string.
Now the error handling is becoming manageable (you will recall that you
originally wrote that code to demonstrate that the error handling was a
PITN to write), and the program's complexity is being reduced as more
and more work is farmed out to re-usable library modules.
What you end up with is rather elegant (if done properly), and eminently
re-usable.
The example is a little bit artificial because we haven't said what we are using our list of strings for.
However a programming interface
STRINGS *list = makestrings("One,two,three,four");
if(!list)
/* that old problem again - a 2GB computer is more likely to break
than to execute this */
exit(EXIT_FAILURE);
/* use strings */
killstrings(list);
is perfectly reasonable; you might want to pass a series of strings in one go and one parameter rather than mess about with add_string functions. Certainly an object constructor very often needs to take a list.
The question is the internals. High-level languages achieve their neatness by using dynamic arrays. We can do that in C, but only by seriously messing about with the language.
/*
add an item to a dynamic list.
Params: ptr - pointer allocated with malloc(), can be null
N - number of items pointer currently holds
item - item to add to list
sz - element size
Returns: pointer to new array on success, 0 on fail.
*/
void *dlist_add(void *ptr, int N, void *item, size_t sz)
{
unsigned char *temp;
temp = realloc(ptr, (N + 1) * sz);
if(!temp)
return 0;
memcpy(temp + N * sz, item, sz);
return temp;
}
Now we've got the equivalent of an STL-style vector, but it is horrid, horrid. No one is going to use that.
Then if we put dlist_add into a separate file, suddenly we got a dependency, and the old problem of you want a list of strings and that requires pulling in a whole hierarchy of files.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
.
- Follow-Ups:
- Re: Why leave the error handling to the caller?
- From: Richard Heathfield
- Re: Why leave the error handling to the caller?
- References:
- Why leave the error handling to the caller?
- From: Chad
- Re: Why leave the error handling to the caller?
- From: Malcolm McLean
- Re: Why leave the error handling to the caller?
- From: Richard Heathfield
- Re: Why leave the error handling to the caller?
- From: Malcolm McLean
- Re: Why leave the error handling to the caller?
- From: Richard Heathfield
- Re: Why leave the error handling to the caller?
- From: Malcolm McLean
- Re: Why leave the error handling to the caller?
- From: Flash Gordon
- Re: Why leave the error handling to the caller?
- From: Richard Heathfield
- Re: Why leave the error handling to the caller?
- From: Malcolm McLean
- Re: Why leave the error handling to the caller?
- From: Richard Heathfield
- Re: Why leave the error handling to the caller?
- From: Johan Bengtsson
- Re: Why leave the error handling to the caller?
- From: Richard Heathfield
- Re: Why leave the error handling to the caller?
- From: Malcolm McLean
- Re: Why leave the error handling to the caller?
- From: Richard Heathfield
- Why leave the error handling to the caller?
- Prev by Date: Re: How widely supported is variable type 'long long int' ?
- Next by Date: Re: Why leave the error handling to the caller?
- Previous by thread: Re: Why leave the error handling to the caller?
- Next by thread: Re: Why leave the error handling to the caller?
- Index(es):
Relevant Pages
|
|