3 ms·
...since it avoids duplication of the cleanup code (and the last thing you want is to have multiple similar blocks of code that need to be maintained/modified i
by palish 18y ago
...since it avoids duplication of the cleanup code (and the last thing you want is to have multiple similar blocks of code that need to be maintained/modified in tandem).
Isn't that the definition of "when to extract code into its own function"?
- thwarted 18y agoThat may be hard to do if it's cleanup code, as the scope changes. Plus, it divorces, visually, the cleanup code from the main body. And there may be additional function call overhead (which may be avoidable by having the compiler in-line it). One reason to use goto is the style that all functions should have one entry and one exit. Even if the cleanup code was put into its own function, you'd still have to ensure that the function was called in all the right places before returning. Without gotos, the logic can become harder to follow through a series of if-else blocks.
- pmorici 18y agoIn the case of clean up code where the duplicated code is usually specific to the current scope putting it in a function instead of a goto doesn't buy you anything really.