error_code =some_func(some_params);
if (error_code!=NOERROR) {
goto cleanup;
}
error_code =other_func(some_params);
if (error_code!=NOERROR) {
goto cleanup;
}
error_code =yetanother_func(some_params);
if (error_code!=NOERROR) {
goto cleanup;
}
//... More code here
There is much code like this. At some point a well-intentioned developer who heard that goto is evil changed the code to look like this:
do {
error_code =some_func(some_params);
if (error_code!=NOERROR) {
break;
}
error_code =other_func(some_params);
if (error_code!=NOERROR) {
break;
}
error_code =yetanother_func(some_params);
if (error_code!=NOERROR) {
break;
}
//... More code here
} while (0);
In my opinion the do-break-while(0) is much uglier and harder to read than goto.
1. Here break is just a goto in disguise.
2. If you see the beginning but not the end you think there is a loop here.
3. In original version goto is used to control the flow in exceptional cases. Goto does not change the structure of the code under non-exceptional scenario.
What do you think?