What's the purpose of using variable list arguments in error handling?

C

Chad

This might be a bit vague and poorly worded.....


In my program, I handle function failures using fprintf() and exit()
like:

fprintf(stderr, "malloc failed");
exit(EXIT_FAILURE);

There are 5 of these. Since each one has two lines, the total lines of
code would be 10. Now if I would use variable argument lists for my
error functions, I would use something like

#include <stdarg.h>

void err_exit(const char *fmt, ...){
va_list ap;
va_start(ap, fmt);
err_doit(1,errno,fmt, ap);
exit(1);
}

static void err_doit(int errnoflag, int error, const char *fmt,
va_list *ap){
/*more code here*/
}

Then all 2 line I used for error handling
fprintf(stderr, "malloc failed\n");
exit(EXIT_FAILURE);

would be replaced with 1 line of error handling.
err_exit("malloc failed);

This means I would have only 5 lines of code to do the error handling.
But an additional 7 plus lines of code for the error handling function
itself. Or a total of 12 lines of code. This is NOT a net savings
since my original code only had a total of 10 lines.

So is there some kind of magic when using variable argument lists for
error handling?

Chad
 
R

robertwessel2

This might be a bit vague and poorly worded.....

In my program, I handle function failures using fprintf() and exit()
like:

fprintf(stderr, "malloc failed");
exit(EXIT_FAILURE);

There are 5 of these. Since each one has two lines, the total lines of
code would be 10. Now if I would use variable argument lists for my
error functions, I would use something like

#include <stdarg.h>

void err_exit(const char *fmt, ...){
va_list ap;
va_start(ap, fmt);
err_doit(1,errno,fmt, ap);
exit(1);

}

static void err_doit(int errnoflag, int error, const char *fmt,
va_list *ap){
/*more code here*/

}

Then all 2 line I used for error handling
fprintf(stderr, "malloc failed\n");
exit(EXIT_FAILURE);

would be replaced with 1 line of error handling.
err_exit("malloc failed);

This means I would have only 5 lines of code to do the error handling.
But an additional 7 plus lines of code for the error handling function
itself. Or a total of 12 lines of code. This is NOT a net savings
since my original code only had a total of 10 lines.

So is there some kind of magic when using variable argument lists for
error handling?


My error handling usually involves a lot more than a single printf().
Not least writing to the log server, or send some notification back to
the user. And of course the common log routines often add context
information (who, when, where) to the logged item. In that case, you
may be executing dozens or hundreds of lines of code. And being able
to pass a variable argument list into that is certainly helpful.

Plus, even if all you're doing is a printf() and exit(), is a good
idea to centralize the code in case you want to improve that in the
future...
 
O

Old Wolf

This might be a bit vague and poorly worded.....

In my program, I handle function failures using fprintf() and exit()
like:

fprintf(stderr, "malloc failed");
exit(EXIT_FAILURE);

There are 5 of these. Since each one has two lines, the total lines of
code would be 10. Now if I would use variable argument lists for my
error functions, I would use something like

#include <stdarg.h>

void err_exit(const char *fmt, ...){
va_list ap;
va_start(ap, fmt);
err_doit(1,errno,fmt, ap);
exit(1);
}

static void err_doit(int errnoflag, int error, const char *fmt,
va_list *ap){
/*more code here*/

}

This code does a lot more than the original, so you are
comparing apples and oranges.

An apples-to-apples comparison might be:
void err_exit( char const *fmt, ... )
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
exit(EXIT_FAILURE);
}
This means I would have only 5 lines of code to do the error handling.
But an additional 7 plus lines of code for the error handling function
itself. Or a total of 12 lines of code. This is NOT a net savings
since my original code only had a total of 10 lines.

If you delete all the newlines then you could get
it all on one line!

(Hint: why are you using line count as a measure of code quality).
So is there some kind of magic when using variable argument lists for
error handling?

What was wrong with your original code exactly?
 
C

Chad

This code does a lot more than the original, so you are
comparing apples and oranges.

An apples-to-apples comparison might be:
void err_exit( char const *fmt, ... )
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
exit(EXIT_FAILURE);
}


If you delete all the newlines then you could get
it all on one line!

(Hint: why are you using line count as a measure of code quality).


What was wrong with your original code exactly?


I don't think anything is really wrong witht he code. I'm trying to
clean some large code that I wrote a while back. I on the three step
minimum wage clerk approach.

1)Just get the program to solve the given problem.
2)Add more error handling as you start to find various ways to crash
the program before releasing the program. Example. Inputting junk
data, inputting data that exceeds the buffer length, etch.
3)Clean up the code. This is the stage I'm at now.

Actually, when I think about this. I think it was the former CTO of
netgensis that told me he used this three step approach when he coded
stuff.
 
R

Richard Tobin

Chad said:
void err_exit(const char *fmt, ...){

The point of this is so that you can do, for example,

if(year < 1960 || year > 2100)
err_exit("date %d out of range", year);

-- Richard
 
C

Chad

The point of this is so that you can do, for example,

if(year < 1960 || year > 2100)
err_exit("date %d out of range", year);

-- Richard


Okay. I wasn't aware of that.
 
G

Gordon Burditt

This might be a bit vague and poorly worded.....
In my program, I handle function failures using fprintf() and exit()
like:

fprintf(stderr, "malloc failed");
exit(EXIT_FAILURE);

There are 5 of these. Since each one has two lines, the total lines of
code would be 10.

Using a "line of code" as a unit of measurement is a mistake. It's
worse than measuring air or gasoline by the "heaping bagful" (any
size bag). Also, management might see it and think it means
something.

The above code could appear on one line. Or it could be written:

fprintf
(
stderr
,
"malloc failed"
)
;
exit
(
EXIT_FAILURE
)
;

and now it's 12 lines.

Now if I would use variable argument lists for my
error functions, I would use something like

#include <stdarg.h>

void err_exit(const char *fmt, ...){
va_list ap;
va_start(ap, fmt);
err_doit(1,errno,fmt, ap);
exit(1);
}

static void err_doit(int errnoflag, int error, const char *fmt,
va_list *ap){
/*more code here*/
}

All of the code above could fit on 1 line except the preprocessor
directive.
Then all 2 line I used for error handling
fprintf(stderr, "malloc failed\n");
exit(EXIT_FAILURE);

would be replaced with 1 line of error handling.
err_exit("malloc failed);
Missing double quote above.

Or it could be replaced with 5 lines of code:
err_exit
(
"malloc failed"
)
;
This means I would have only 5 lines of code to do the error handling.
But an additional 7 plus lines of code for the error handling function
itself. Or a total of 12 lines of code. This is NOT a net savings
since my original code only had a total of 10 lines.

Counting lines of code is a waste of time.
So is there some kind of magic when using variable argument lists for
error handling?

My advice is to stop counting lines of code.
 
C

CBFalconer

Gordon said:
Using a "line of code" as a unit of measurement is a mistake.
It's worse than measuring air or gasoline by the "heaping bagful"
(any size bag). Also, management might see it and think it means
something.

The above code could appear on one line. Or it could be written:

fprintf
(
stderr
,
"malloc failed"
)
;
exit
(
EXIT_FAILURE
)
;

and now it's 12 lines.
.... snip ...

All of the code above could fit on 1 line except the preprocessor
directive.

And you think this doesn't happen in commercial code? Especially
where the management counts lines.
 
F

Flash Gordon

CBFalconer wrote, On 17/06/07 01:03:
And you think this doesn't happen in commercial code? Especially
where the management counts lines.

Sometimes it might, sometimes it does not. Where I used to work it did
not happen, at least not enough to have a noticeable effect. This was
probably because it was only used as a very rough measure of comparative
size/complexity by people with enough knowledge to know the problems in
such a measure. Also the code underwent reviews, so things like shown
above would be changed to a more sane layout :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top