3 ms·
In C with it's lack of exceptions, goto can be used to jump unconditionally to cleanup code before exiting a function. This is far cleaner than every branch and
by mrj 15y ago
In C with it's lack of exceptions, goto can be used to jump unconditionally to cleanup code before exiting a function. This is far cleaner than every branch and loop following a call needing explicit instructions to check for error statuses, sometimes multiple errors (like malloc something, check for error, use dynamic memory in a call, check for a different error).
It's also more maintainable, since the error checking is done in a single place and a single jump. Without a goto, if there were a new error condition to check, it might take several updates to ensure all error conditions are accurately checked.
Generally, gotos for handling exceptional cases inside a function: okay. Goto for implementing twisted logic: bad.