Re: Why leave the error handling to the caller?
- From: Johan Bengtsson <qwerty_42@xxxxxxxxxxx>
- Date: Thu, 21 Jun 2007 17:26:31 GMT
Richard Heathfield wrote:
Malcolm McLean said:There is no pleasing some people.
You wanted it to compile, so now it compiles.
Excuse me? I didn't even mention the fact that it didn't compile. What I said was that "I looked at your code, and found it lacking in any significant evidence that you are a regular clc subscriber."
The second lot of code did nothing to change my mind.
Nevertheless, I can use the code you posted to illustrate my point. Let's just pretend that makestrings is well-written, shall we? And if it fails, it returns NULL. Your loop for handling makestrings assumes it succeeded, which is unwise, but let's fix that:
Your code:
str = makestrings(argv[1]);
for(i=0;i<str->N;i++)
printf("***%s***\n", str->str[i]);
would be better written as:
str = makestrings(argv[1]);
if(str != NULL)
{
for(i = 0; i < str->N; i++)
{
printf("***%s***\n", str->str[i]);
}
}
else
{
char *p = argv[1];
char *q;
for(q = strchr(p, ','); q != NULL; q = strchr(p, ','))
{
printf("***%.*s***\n", q - p, p);
p = q + 1;
}
printf("***%s***\n", p);
}
And thus we have a recovery strategy which achieves the correct program output even in the face of a malloc failure. And that's why library routines shouldn't bomb out.
QED.
Ummm, am I missing something here?
I do agree that the library functions should not make the decision and kill the program, and (depending on the program) returning NULL from makestrings would be the the best behaivour.
I do however not really agree to this solution of error handling. If the intended behavior of the program is to simply output all the strings with no further processing there would never be any reason for storing them (and thereby not having any trouble to begin with) and whatever is in the error recovery part of the program would better be the *entire* program. I assume the purpose of allocating the strings in some structure to begin with is that there really is some further processing to be done before outputting them (for example sorting or whatever). That would make the error handling function output data that would be wrong. Isn't it (in most cases) then better to not get any data than to get the wrong data?
The answer to that does of course depend on the purpose of the application - and can only be answered by the ones ordering the program to be written in the first place and the intended customers.
Assume, for example, that the strings should be sorted: I would rather have the program fail completely than output the strings unsorted. If the purpose is to just output the strings in order of appearance - then it would be very bad programming to allocate memory for them anyway since that isn't necessary to complete the task.
.
- 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
- Why leave the error handling to the caller?
- Prev by Date: Re: about time and space complexities
- Next by Date: Binary mode???
- 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
|
|