int do_work(int stuff)
{
int res1 = 0, res2 = 0, res3 = 0;
int success = 0;
res1 = acquire_one_resource(stuff);
if (!res1) goto cleanup;
res2 = acquire_another_resource(stuff);
if (!res2) goto cleanup;
res3 = acquire_more_resources(stuff);
if (!res3) goto cleanup;
/* etc. */
success = 1;
cleanup:
/* here the only requirement is that the cleanup process
* ignores unacquired resources, perhaps with some minor
* logic if you have pointers to resources (unusual) */
free_resource(res3);
free_resource(res2); // would you look at that, all the cleanup code
free_resource(res1); // in one single spot, no code duplication
return success;
}
Considering a GOTO can invalidate reused code or functions, and forces a reader who did not write it (or forgot about it) to search all gotos of the label that refernce it where code redirects to the label. Like "oh there are only three gotos of the label good- oh- they happen to be luckily close and does not break the local scope, good, oh they redirect only from 3 different conditions, oh, hopefully all variations in runtime will work, oh hopefully cleanup is dull-clean safe in case second resource fail with third not allocated, oh...."
I like that if a function encounters an error, it correctly hands back the runtime to the calling scope back up, without this possible ot not viable, some goto will not help as well. (that is why try catch was implemented, so even not correctly returning functions will not couse upper scope to fall, and can prepare walking back to upper upper scope)
Many languages does not implement goto (porting and machine expresebility of instruction gone), and some does but in different way.
The talk of using goto as an optimization to save clock cycles is a bit ironic,
So much this Hodgeman. If we omit invalidating codeflow production at the moment of using it, there is no comploted situation where goto would outperform continue, return or break alternative of code. And the last argument to support goto, making code more readable at some situation, is self negating, since it involves runtime danger combined with need to analyze the label around entire project.
You can use goto only in your own project, that only you are writing, if you happen to remember all you have ever written in sufficient details.
Then of course there's the fact that when it comes to being in the workforce, you'll be a lot more likely to get a job and keep it if you don't use them.
I would stress this.