I have a piece of code that can fail at many points. With each additional action (that can also fail), the readability worsens. Is there a better way to structure this? Is there a design pattern for this?
int32_t InitializeMyThingy(void) {
int32_t retcode;
retcode = DoA();
if (retcode != 0) return retcode;
retcode = DoB();
if (retcode != 0) return retcode;
retcode = DoC();
if (retcode != 0) return retcode;
retcode = DoD();
return retcode;
}
Alternatively (more readable?):
int32_t InitializeMyThingy(void) {
int32_t retcode;
retcode = DoA();
if (0 == retcode) {
retcode = DoB();
if (0 == retcode) {
retcode = DoC();
if (0 == retcode) {
retcode = DoD();
}
}
}
return retcode;
}