Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!nbires!ico!ism780c!sdcrdcf!burdvax!bigburd!weiss From: weiss@bigburd.PRC.Unisys.COM (Tom Weiss) Newsgroups: comp.lang.c Subject: goto's in C: an opinion... Message-ID: <3289@bigburd.PRC.Unisys.COM> Date: Wed, 15-Jul-87 12:08:00 EDT Article-I.D.: bigburd.3289 Posted: Wed Jul 15 12:08:00 1987 Date-Received: Sat, 18-Jul-87 11:25:58 EDT Sender: news@bigburd.PRC.Unisys.COM Organization: Unisys/Paoli Research Center, Paoli, PA Lines: 41 Keywords: C, goto, style Since now appears to be the time to express opinions about style/clarity, I would like to address my own pet peeve: the goto statement. There are very few legitimate uses for the goto statement, and C provides alternates for most of them. While it is true that these alternates are no less evil than goto viewed from a structured programming perspective, at least they are more readable. Since C provides the break statement to break out of a loop, the continue statement to jump back to the top of a loop, the return statement to immediately exit a function, and access to system calls (e.g. exit) that can terminate execution in error handling functions, there are few 'legitimate' uses for goto. The only one that immediately comes to mind is breaking directly out of a multiply-nested control structure. Once I was involved in rewriting a piece of code which contained a goto. The code consisted of a large loop containing switch and if/else statements. When I first rewrote it, I eliminated the original goto, but introduced two of my own! I then took a close look at what was really going on. The original goto was used to jump forward into the middle of an if/else construct. When I rewrote the code, I basically added features, without changing the overall control flow. Though the original goto disappeared, I introduced more goto's to keep in tune with the original structure. When I saw what I had done, I knew something was very wrong, and I decided to see what would happen if I rewrote the code to remove the goto's. I originally expected that I would wind up making some new functions to be called from the loop. As it turned out, all I had to do was change the order of tests in the loop (this involved eliminating the switch). In short, poor logical design had necessitated the original use of goto, and encouraged more goto's in expanded versions. My point is that 99% of the time I see a goto in a C program, it is not there for any good reason. Usually goto's are used to make loops or jump around within a chunk of code that has poorly structured control flow. I do not mean to say that programmers who use goto's are lousy programmers, merely that they have gotten into a bad habit that should be broken.