Re: Why leave the error handling to the caller?



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.
.



Relevant Pages

  • Re: [Bugme-new] [Bug 11446] New: Automate "to compile as module" strings
    ... Automate "to compile as module" strings ... Component: Configuration ... tristate "BMAC support" ...
    (Linux-Kernel)
  • Re: csc (C# Compiler) bug
    ... > I seem to have found a bug in csc. ... Here is some source that does not compile properly: ... This does not happen with all strings ... Its not a bug, its actually correct. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: STL::list trouble in VC6
    ... > | not compile, as sizeofcould be used in the definition ... // Provides some storage for small strings... ... template ... struct basic_string_helper ...
    (alt.comp.lang.learn.c-cpp)
  • Re: L macro
    ... ..cpp file, it could compile. ... "Alexander Nickolov" wrote: ... Coding everything in UNICODE wide characters ... So, if you want to use Unicode UTF-16 strings in your app, you should put ...
    (microsoft.public.vc.language)
  • Re: sets
    ... >I want to be able to form a list of strings where duplicates may occur but ... Well they are not for that purpose but they can be used for that. ... It all depends what those warnings are. ...
    (alt.comp.lang.learn.c-cpp)