M
Michael B Allen
This is a highly opinionated question but I'd like to know what people
think in this matter.
When initializing / creating a group of objects together I usually
consolidate the error handling like the following:
if (foo_init(&f) == -1 ||
(b = bar_new()) == NULL ||
some_function(a, b, c) == -1) {
ERR(errno);
bar_del(b);
return -1;
}
With small conditionals this isn't too bad but sometimes it can get
pretty ugly. Would you rather see this expanded like:
if (foo_init(&f) == -1) {
ERR(errno);
return -1;
}
if ((b = bar_new()) == NULL) {
ERR(errno);
return -1;
}
if (some_function(a, b, c) == -1) {
ERR(errno);
bar_del(b);
return -1;
}
Or how would you write this and why?
Mike
Note: In this listing I use _new to indicate a function that allocates
resources that must be released as opposed to _init which does not. Also
ERR can be considered a macro that tracks error state for debugging
purposes.
think in this matter.
When initializing / creating a group of objects together I usually
consolidate the error handling like the following:
if (foo_init(&f) == -1 ||
(b = bar_new()) == NULL ||
some_function(a, b, c) == -1) {
ERR(errno);
bar_del(b);
return -1;
}
With small conditionals this isn't too bad but sometimes it can get
pretty ugly. Would you rather see this expanded like:
if (foo_init(&f) == -1) {
ERR(errno);
return -1;
}
if ((b = bar_new()) == NULL) {
ERR(errno);
return -1;
}
if (some_function(a, b, c) == -1) {
ERR(errno);
bar_del(b);
return -1;
}
Or how would you write this and why?
Mike
Note: In this listing I use _new to indicate a function that allocates
resources that must be released as opposed to _init which does not. Also
ERR can be considered a macro that tracks error state for debugging
purposes.