3 ms·
I remember back in a Paradigms of Programming class in uni, our professor who lived by Dijkstra's "Gotos considered harmful" mantra tried to get us all to write
by oakenshield 16y ago
I remember back in a Paradigms of Programming class in uni, our professor who lived by Dijkstra's "Gotos considered harmful" mantra tried to get us all to write programs with a single exit point from each function. I argued that short-circuiting errors before going to the logic made sense both nesting-wise and maintainability-wise, but was ridiculed. I wish I'd known about coding styles in the Linux/BSD kernels back then so I could throw it back at him.
- scott_s 16y agoBut the coding styles in the Linux kernel usually maintain a single exit point from the function. The format they generally have is: syscall(params) { if (easy check on params fails) goto done; allocate data for work if (further check on params and data fails) goto cleanup; perform real work cleanup: deallocate data done: set return value } There's often still a single exit, but it does use gotos to implement simple exceptions. The only times when there are multiple exits is before any data has been allocated and there's no cleanup to be done.
- walkon 16y agoI agree with your preference, but just curious - is a return considered a goto? I've not thought so, but I could be wrong.