M
myheartinamerica
Hello,
I was doing some simple database programming and encapsulating
different
queries in functions. After each library call, I check the return
value. After
some return values, it makes no sense to continue, so I returned an
error code.
Pseudocode is something like this:
int query_customer(const char* customer)
int retval = library_call_1()
if (retval == ERROR) { return -1}
retval = library_call_2()
if (retval == ERROR) { return -1}
retval = library_call_3()
if (retval == ERROR) { return -1}
/* process data */
/* cleanup handles, allocations, etc... */
return some_inportant_value
When I came in this morning, a coworker had rewritten my code. I'm
quick second
guess myself, so I assumed that his was more correct. It was something
like:
int query_customer(const char* customer)
int retval = library_call_1()
if (retval == ERROR) { some_important_value = MY_ERROR_CODE }
retval = library_call_2()
if (retval == ERROR) { some_important_value = MY_ERROR_CODE }
retval = library_call_3()
if (retval == ERROR) { some_important_value = MY_ERROR_CODE }
/* process data */
/* cleanup handles, allocations, etc... */
return some_inportant_value
So, he would continue to make the other library calls, even if the
previous
ones had failed (meaning that the later ones couldn't be successful).
It got me thinking, that maybe there was a better way to do this, but
I'm not
sure what that is. I'm thinking about using goto's and a label, like
this:
int query_customer(const char* customer)
int retval = library_call_1()
if (retval == ERROR) {
some_important_value = MY_ERROR_CODE;
goto clean_up;
}
retval = library_call_2()
if (retval == ERROR) {
some_important_value = MY_ERROR_CODE;
goto clean_up;
}
retval = library_call_3()
if (retval == ERROR) {
some_important_value = MY_ERROR_CODE;
goto clean_up;
}
/* process data */
clean_up:
/* cleanup handles, allocations, etc... */
return some_important_value
Is there some better way to do this? Is the use of goto a good example
of it's
usefulness?
I was doing some simple database programming and encapsulating
different
queries in functions. After each library call, I check the return
value. After
some return values, it makes no sense to continue, so I returned an
error code.
Pseudocode is something like this:
int query_customer(const char* customer)
int retval = library_call_1()
if (retval == ERROR) { return -1}
retval = library_call_2()
if (retval == ERROR) { return -1}
retval = library_call_3()
if (retval == ERROR) { return -1}
/* process data */
/* cleanup handles, allocations, etc... */
return some_inportant_value
When I came in this morning, a coworker had rewritten my code. I'm
quick second
guess myself, so I assumed that his was more correct. It was something
like:
int query_customer(const char* customer)
int retval = library_call_1()
if (retval == ERROR) { some_important_value = MY_ERROR_CODE }
retval = library_call_2()
if (retval == ERROR) { some_important_value = MY_ERROR_CODE }
retval = library_call_3()
if (retval == ERROR) { some_important_value = MY_ERROR_CODE }
/* process data */
/* cleanup handles, allocations, etc... */
return some_inportant_value
So, he would continue to make the other library calls, even if the
previous
ones had failed (meaning that the later ones couldn't be successful).
It got me thinking, that maybe there was a better way to do this, but
I'm not
sure what that is. I'm thinking about using goto's and a label, like
this:
int query_customer(const char* customer)
int retval = library_call_1()
if (retval == ERROR) {
some_important_value = MY_ERROR_CODE;
goto clean_up;
}
retval = library_call_2()
if (retval == ERROR) {
some_important_value = MY_ERROR_CODE;
goto clean_up;
}
retval = library_call_3()
if (retval == ERROR) {
some_important_value = MY_ERROR_CODE;
goto clean_up;
}
/* process data */
clean_up:
/* cleanup handles, allocations, etc... */
return some_important_value
Is there some better way to do this? Is the use of goto a good example
of it's
usefulness?