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, 03 Dec 2020 23:02:54 -0800
Organization: A noiseless patient Spider
Lines: 29
Message-ID: <86v9dim4td.fsf@linuxsc.com>
References: <87czzszjhs.fsf@fedora.osfans.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="bd193eaa76d2cf756f76f8149091c42d"; logging-data="30113"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ynXcmp7iF4YgGyPscb1ME39d6Re1RYZM="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:iTrDWhObCLb7BZdvUdFSYEysyWY= sha1:7hIn84sk4ZW9nXv3qTWbGW91RAo=
Xref: csiph.com comp.lang.c:156906
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.