3 ms·
Goto is often the cleanest way to jump to error handling at the end of a function. I always smile at contorted efforts to avoid goto at any cost. Recently I saw
by sfk 17y ago
Goto is often the cleanest way to jump to error handling at the end of a function. I always smile at contorted efforts to avoid goto at any cost. Recently I saw something like this:
do {
/* deeply nested code */
if (error) {
break;
}
} while (0);
/* error handling */
And no, it was not the do/while macro trick. It was advertised as structured programming.
- scott_s 17y agoThat's the first thing that comes to mind for me when the discussion comes up; the Linux kernel uses the technique extensively. To my surprise, Dijkstra actually said the same thing. He called them "alarm exits."
- fhub 17y agoRobert Love ended an opinionated discussion on goto in the linux kernel in '03. His pseudo code demonstrates a neat error stack unwind technique for c. http://kerneltrap.org/node/553/2131 http://kerneltrap.org/node/553/2131 (Scroll to bottom for pseudo code)
- spectre 17y agoSome people claim this evil too because a break is almost a goto.