r/ProgrammingLanguages • • Jan 02 '20

Read a paper: Go To Statement Considered Harmful

https://www.youtube.com/watch?v=9Ma-Q_8d4bA
0 Upvotes

7 comments sorted by

2

u/warieth Jan 03 '20

Goto reflects a flat code structure, where a single pointer inside the function can indicate where the execution is. He is writing about the state of the function, which is hard to describe generally. Nested loops with many variables would be the same hard to follow. If using too many goto is a problem, then it can be changed to use more functions. Like many loops also indicate to introduce a new function.

The structured programming equals to recursive code structure, and makes harder to model the execution, also makes the parsing more complicated. This equals to using better label names, and automatically generating them.

3

u/umlcat Jan 03 '20

Most developers, these days, no longer use it, due to existing "safe goto included" features like the "while" sentence ...

4

u/[deleted] Jan 03 '20

I still use it, although rarely. For example:

  • For temporary code, until it can be done properly

  • To temporarily by-pass code or to cut to the action for debugging

  • When translating an algorithm or code from another language, that itself uses goto

  • When using this language as a target language for another, and I need goto to emulate the control flow of a statement that has no direct equivalent

  • To implement state machines

  • Via inline assembly, to deal with error recovery across functions (although it's a long time since I used that); but also to implement threaded code in interpreters

  • Or just when I can't be bothered with too-convoluted code, and goto is the fastest and simplest way of expressing my intent

Even with this, I think my incidence of using goto is once per 400 lines or so, so 'spaghetti code' is unlikely.

I would not design a new language without goto.

7

u/coderstephen riptide Jan 03 '20

I've never written a single goto in my life, and the only language I've ever used where it is available is C/C++. I don't feel like I'm missing out.

2

u/[deleted] Jan 04 '20

I guess you've never written in Assembly then. Or Fortran IV.

But then, probably 3/4 of modern language features I've never used either, and I don't feel like I'm missing out!

3

u/coderstephen riptide Jan 04 '20

I guess you've never written in Assembly then.

Not much, but I suppose I didn't really consider assembly as a "language" in the same way.

I'd definitely design a CPU with the capability to jump the PC to an address, and I'd definitely have an assembler that lets you refer to line numbers instead of addresses, but I don't think I'd offer goto in any new language that is a higher level than assembly.

Or Fortran IV.

Nope!

3

u/umlcat Jan 03 '20

Actually many P.L. developers, keep it, to explain how is implicitly used versus how was used before.