"GOTO Considered Harmful Considered Harmful" Considered Harmful

I had to use a goto in some code I wrote last night, and it is a cleaner solution to bowing out of a nested loop than anything else I tried.

I tried a couple different ways of working a check into the flow of the program. I was firm in my long-held lessons from school: "don't use gotos", "all gotos are bad", "if you kiss a goto you'll get it pregnant". That kind of thing.

Every approach was ugly. I ended up with a simple need to break out of multiple loops at once, and it dawned on me that this was one of the few places where a goto is apropos. I think the real answer here is not to forbid all gotos everywhere, but to abstain from using them until you have built up enough skill and experience to recognize when you might craft a program that is funky and lopsided because you refused to consider that it could use a judicious goto.

No comments: