Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c++
Subject: Re: two's complement idea
Date: Mon, 27 Jan 2020 07:48:34 -0800
Organization: A noiseless patient Spider
Lines: 53
Message-ID: <86mua8hpd9.fsf@linuxsc.com>
References:
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="7c306968cf7022673062d49c54d79376"; logging-data="12315"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19e3gCMBu51G/h3Z9deTZqyv219p/CPWKw="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:jOGzVfRcaG+QZs6UPU/vFcGfJ3U= sha1:n6s/0QKw+9ly77ZGVijsntH0Xek=
Xref: csiph.com comp.lang.c++:71624
Christian Gollwitzer writes:
> Am 22.01.20 um 12:26 schrieb boltar@nowhere.org:
>
>> I have paint to watch drying instead. Nice idiom
>>
>> No doubt it comes from the same ivory
>> tower that claims gotos should never be used.
>
> Ivory tower, for sure? We have while, for, break, continue, return,
> and exceptions. All these compile to some sort of goto in machine
> code.
>
> What good reasons are left for a literal goto? I can see it in C for
> error handling (because it lacks exceptions) - in C++ there is hardly
> a good reason IMHO.
I won't make any arguments for a good use case for goto, but I
have some facts to report that might be of interest.
I looked for uses of goto (using grep -w) in a convenient corpus
of C and C++ code - mostly open source software packages, about
250 million lines. By coincidence there were close to the same
number of lines of C source as lines of C++ source.
The number of goto's in C code was about five times the number of
goto's in C++ code. However, considering just those files that
had at least one 'goto' in them, the rate of using goto was about
the same in C++ as in C: just under 13 per 1000 lines in C++,
just over 14 per 1000 lines in C. (Remember, that is looking
only at files that use 'goto' at least once.) In the corpus as a
whole, the size of a source file in lines (measured by 'wc') was
on average about 20% higher for C code than for C++ code.
Considering just those files that had at least one 'goto' in
them, the sizes of source files was on average about one third
more for C++ than it was for C.
Switching now to personal experience, I am not dogmatic about
never using goto but it almost never comes up in code that I
write. That applies to C as well as C++. I do remember a case
that came up several years ago (I don't remember if it was C or
C++ but the same considerations would apply in both languages).
I had an awkward loop of the "loop and half" variety. I tried
perhaps half a dozen different ways of writing it, but all those
I tried seemed unsatisfactory in some way. The alternative that
seemed the best of the different ways I looked at rolled the loop
half way down so the exit was at the bottom but had to be entered
somewhere in the middle. Of course that meant using a goto. The
result still wasn't something I was really happy about; it just
seemed better than all the other ways that I tried. And that I
think is a good principle for goto generally: explore as many
alternatives as reasonably feasible, and use goto only when all
the alternatives look worse. That is my sense anyway, fwiw.