[TUHS] I can't drive 55: "GOTO considered harmful" 55th anniversary

Larry McVoy lm at mcvoy.com
Sat Mar 11 01:46:02 AEST 2023


I've used gotos for decades but in a "structured" way.  My functions 
have a pattern:

	<allocation section>
	<work section>
	<deallocation section>

and there are a series of labels in the deallocation section.  Then while
you are wandering through the allocation section, you can jump to the 
right spot in the deallocation section.

And yes, this is a simplification because I initialize all my pointers to
NULL so the deallocation is all 

	if (p) free(p);

but the idea is the same.  You wind up some state and wind down some state
and the gotos are used to jump to the right place to unwind.

On Fri, Mar 10, 2023 at 10:37:02AM -0500, Noel Chiappa wrote:
>     > From: "Ronald Natalie"
> 
>     > Multilevel breaks are as bad as goto with regard to structure violation.
> 
> In a way, you are right. There isn't really much difference between:
> 
> 	for (mumble) {
> 		for (foobar) {
> 			do some stuff
> 			break-2;
> 			}
> 		}
> 
> and:
> 
> 	for (mumble) {
> 		for (foobar) {
> 			do some stuff
> 			goto all_loops_done;
> 			}
> 		}
> 	all_loops_done:
> 
> 
> The former is basically just 'syntactic sugar' for the latter.
> 
> I think the point is that goto's aren't necessarily _always_ bad, in and of
> themselves; it's _how_, _where_ and _why_ one uses them. If one uses goto's
> in a _structured_ way (oxymoronic as that sounds), to get around things that
> are lacking in the language's flow-control, they're probably fine.
> 
> Then, of course, one gets into the usual shrubbery of 'but suppose someone
> uses them in a way that's _not_ structured?' There's no fixing stupid, is my
> response. Nested 'if/then/else' can be used to write comletely
> incomprehensible code (I have an amusing story about that) - but that's not
> an argument against nested 'if/then/else'.
> 
> As I've said before, the best sculpting tools in the world won't make a great
> sculptor out of a ham-handed bozo.
> 
> 	Noel

-- 
---
Larry McVoy           Retired to fishing          http://www.mcvoy.com/lm/boat


More information about the TUHS mailing list