Re:Get to No as fast as possible
by oakenshield on Thursday June 17, 2010 @12:38PM
(#1439456)
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.
[ Parent
| on HN ]
But 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.