Consider this function:
int get_result(int *result) {
int err = 0;
int number = 0;
if (result == NULL) {
printf("error: null input\n");
return -1;
}
err = get_number(&number);
if (err != 0) {
printf("error calling get_number: err = %d\n", err);
return err;
}
err = calculate_result(number, result);
if (err != 0) {
printf("error calling get_result: err = %d\n", err);
return err;
}
return err;
}
The real work in this function requires only 3 lines (declare number variable, call get_number(), then call calculate_result()). However, the error checking/handling code bloats this function to 17 lines (give or take, depending on how you count the lines).
At a larger scale, with many calls, and multiple error checks, we bloat the function entirely and make it unreadable and very hard to understand.
What are ways to get around this bloating in C code and maintain readability of the core operation of a function (without sacrificing essential error handling code)?