Path: csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: Effective uses of c `goto' statement Date: Thu, 10 Dec 2020 10:45:50 -0800 Organization: A noiseless patient Spider Lines: 38 Message-ID: <86o8j133fl.fsf@linuxsc.com> References: <87czzszjhs.fsf@fedora.osfans.org> <86v9dim4td.fsf@linuxsc.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="e123793f93e194ce9596f9b3807e7692"; logging-data="27062"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ie+QMH1eOYByP+0chsrW1NC/t5IDut7o=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:6NRZBCw6g/mzruXl6vdlkFJQmJs= sha1:qPX3HqLd86uYaysdfMmd7rMIHBA= Xref: csiph.com comp.lang.c:157202 kegs@provalid.com (Kent Dickey) writes: > In article <86v9dim4td.fsf@linuxsc.com>, > Tim Rentsch wrote: > >> kevin shell writes: >> >>> Hello C hackers/masters. :-) >>> >>> A lot of C textbooks say not to use `goto' statement, >>> but the fact is I find lots of Unix/Linux >>> C code often use `goto' statement. >>> >>> My question is how to effectively use `goto' statement with C code, >>> how to use `goto' to jump forward and jump back, >>> how to avoid multiple `goto' statements with both jump forward and >>> jump backward in the same function from messing up? >> >> I have a different kind of suggestion than some of the others you >> have gotten. >> >> Don't use goto's at all, under any circumstances. If you see a >> piece of code that has a goto in it, try to re-write it without >> using goto, as clearly as you can. You might try three or four >> ways of writing a patch of code, and see which one(s) seem >> easier to read or easier to understand. Do this for a period >> of three years (yes that is a serious suggestion). >> >> Like many other aspects of writing, before you learn how and when >> to bend or break the rules, it's important to learn how to follow >> them. Often a situation where goto seems natural means there is >> something else more significantly wrong with the code in some >> other way. Being able to see and fix those other things is much >> more important than learning a few rules about using goto. > > I agree with the above advice. [elaboration] It's nice to hear someone finds merit in my suggestion. :)