'goto', as an indicator of good language

H. S. Teoh hsteoh at qfbox.info
Thu Sep 8 21:03:04 UTC 2022


On Thu, Sep 08, 2022 at 08:39:30PM +0000, Dukc via Digitalmars-d wrote:
> On Thursday, 8 September 2022 at 16:33:58 UTC, AnimusPEXUS wrote:
> > There was a discussion sometime ago in a Russian Dlang Telegram chat
> > about 'goto' keyword. And there was some empiric theory, stating
> > 'all good languages support goto statement'. Nor Rust, nor Carbon,
> > nor Zig will never going to overcome D or C++ because of this.
> > Language should give possibilities, not take them away. And in some
> > cases, goto able to make code more readable, more sane and more
> > understandable. Also, goto is much safer in language with GC.
> > 
> > [corresponding section in
> > wikipedia](https://en.wikipedia.org/wiki/Goto#Criticism)
> 
> My personal attitude to `goto` is that it's overhated. While it's true
> that conditional and loop statements are mostly better, an occasional
> `goto` is not necessarily a code smell. Also I'd be much more worried
> about mutable `static`/global variables and oversized functions/types
> than a few needless `goto`s in a module.
> 
> I suspect it's bad reputation is from the times when some
> (non-assembly) languages did not have structured programming and
> everything was done with `goto`s. Going back to that sure would suck,
> but there's quite a difference between an occasional `goto` and
> replacing all your loops with it.
[...]

Also keep in mind that `goto` in modern-day languages is a very
different thing from `goto` in the old days.  In the old days, it was
the equivalent of a `jmp` instruction in assembly language; it could
literally jump *anywhere* in the program, including jump from the middle
of function A into the middle of function B where B isn't even on the
call stack. It would skip variable cleanups and initializations, stack
adjustments, etc., and generally produce *really* unreadable code. (See,
for example, GOTO in old versions of BASIC, that lets you jump to any
arbitrary line in your program.) It's even more powerful (and dangerous)
than C's longjmp(), because the destination of the goto doesn't even
have to be on the call stack or anywhere near where the history of
execution has passed.

The `goto` of today's languages is a much safer, de-fanged version of
the original goto. In D, for example, you cannot `goto` out of a
function (or into another). The compiler also doesn't let you skip
variable initializations / destructions. So it's subject to pretty much
the same constraints as "structured" constructs like if, while, for,
switch, etc., except it's more low-level and can do a few more things
that the other constructs can't do.

So yes, you can overuse `goto` in your function and make it hard to
follow, but then nested if-else blocks and loops that span 5000 lines
are also hard to read[1], so it's not saying very much.  And today's
goto is nowhere near as wild as the original `goto` in the old days.

[1] I'm not making this up, I've actually seen 5000+ line functions in
real-world "enterprise" code. They are one of those things you wish you
never have to debug, because you just can't fully understand what they
do, goto or not.


T

-- 
Always remember that you are unique. Just like everybody else. -- despair.com


More information about the Digitalmars-d mailing list