4 ms·
I am a very weak C programmer, but one thing I find myself doing a fair bit is wrapping the intermediate data from sequences of operations like this into a sing
by thom 5y ago
I am a very weak C programmer, but one thing I find myself doing a fair bit is wrapping the intermediate data from sequences of operations like this into a single structure representing the whole task or unit of work or whatever. That structure then has a single 'valid' and/or 'error' field that each step checks to see if it should actually proceed, and if so, it will fill its field in the structure, and the next step will pick up from there.
That way you get to write something like:
typedef struct cookie_batch {
bool valid;
bool baked;
unsigned short ingredient_count;
char **ingredients;
} cookie_batch;
.
.
.
void bake(cookie_batch *batch) {
if(!batch->valid) return;
batch->baked = true;
}
.
.
.
cookie_batch batch = { .valid = true };
add_ingredients(&batch);
bake(&batch);
serve(&batch);
cleanup(&batch);
So you don't need any complex error handling in client code.
- AlbertoGP 5y agoYes, that’s a very clean and robust approach. In the “Deferred resource release → Related work” section, one of the links is to a video from the ACCU 2021 conference by Luca Sas titled “Modern C and What We Can Learn From It” that explains modern C techniques, and your one is mentioned at 23′15″: https://youtu.be/QpAhX-gsHMs?t=23m15s https://youtu.be/QpAhX-gsHMs?t=23m15s If you haven’t watched it, I can recommend it.