Live data from Hacker News

GOTOphobia considered harmful in C

blog.joren.ga

131–140 of 319 posts

Re: GOTOphobia considered harmful in C

#131
The best analogy I ever heard on this is that using a goto is like knocking a hole into a wall: it can be very useful, or even essential, in some specific circumstances, but you should give it a bit of thought. Also, it would be foolish to swear off ever allowing either.

Re: GOTOphobia considered harmful in C

#132
post #20

If you’ve ever actually written more than toy C, you’ll know gotos are essential for resource cleanup and error handling. Even the famous goto fail was not a goto error, it was a block error (named because a cleanup statement `goto fail;` always executed because of a brace-less if). goto can be abused like any other language construct, but it’s uniquely useful and makes code more simple and easy to reason about when…

of course nobody should be writing C any more, for exactly the reason you're demonstrating in this comment

What language do you think Linux and Arduino should be programmed in?

Re: GOTOphobia considered harmful in C

#135

Exceptions are GOTO's. Sure, it's more likely to be safe in terms of memory/resource leaks, but in terms of program logic it is no different.

Returns are also gotos then.

If they are anywhere except once at the very end of the function, then yes they are logically like GOTO's.

Re: GOTOphobia considered harmful in C

#136
post #86

Earlier quoted context omitted.

This. To find an example, I did a search for “Commodore PET Basic programs”. Here’s a book from 1979 that shows what spaghetti code looks like, in my opinion: http://www.1000bit.it/support/manuali/commodore/32_BASIC_Pro... The first program listing is on page 24 of the PDF. Try to follow the logic of the program. Why does line 400 go to 280? What paths can lead to line 400? Who knows! And this is high-quality BASIC b…

Wow, that takes me back. I learned to program on a PET and would have devoured this book had it been available. As it was one of our math teachers was tasked with teaching the computer classes bu didn't have any programming knowledge beyond input/output, loops, and simple calculations. Books and manuals were hard to come by.

Also learned to program on a PET in BASIC writing code that was much messier than this because I was probably 10 years old.

Might be why I'm good at restructuring horrible to read spaghetti code.

Re: GOTOphobia considered harmful in C

#137
post #77
post #50

I always encourage people to go read Dijkstra's GOTO paper, instead of just its title. It's a short and easy read, almost like a blog post. If you pay attention, you can see that the he was talking about spaghetti code vs structured code. It's better when the lexical structure of the source code maps to the execution structure. That is, if you know what is the current line being executed, you have a good idea of what…

The thing is that many people today have never encountered the sort of spaghetti code that Dijkstra was talking about in 1968. There's plenty of confusing and messy code around, but true spaghetti code that GOTOs all over the place and is nigh-impossible to follow has been extremely rare for a long time. I can't recall encountering it in the last 30 years. It easy to misunderstand what he was even talking about becau…

I used to work at Microsoft on the windows team. There it was very common to have a “goto cleanup” for all early exits. It was clean and readable. Otoh, I once was assigned to investigate an assertion error in the bowels of IE layout code. It was hundreds of stack frames deep in a recursive function that was over a thousand lines long and had multiple gotos that went forwards or backwards. That was an absolute mess and would have been impossible to debug without an recording (“time travel”) debugger.

Re: GOTOphobia considered harmful in C

#138

Earlier quoted context omitted.

> but true spaghetti code that GOTOs all over the place and is nigh-impossible to follow has been extremely rare for a long time. Exactly! Recently I had the "pleasure" to work with some FORTRAN IV code from the early 60s, so I know what you mean. No functions/subroutines, only GOTOs. Even loops were done with labels. There is also a weird feature called "arithmetic IF statements" ( https://en.wikipedia.org/wiki/Arit…

Hmm, it is time to live Cunningham's Law I think: There’s no good way to do a do…while loop in Fortran, other than a goto.

Early Fortran already had loops, but with labels:

         INTEGER A(4,4), C, R 
         ... 
         DO 10 WHILE ( C .NE. R ) 
                A(C,R) = A(C,R) + 1 
         C = C+1 
  10     CONTINUE

Note that Fortran has evolved significantly over time. This is how you would write the same in Fortran 77:

       INTEGER A(4,4), C, R 
       ... 
       C = 4 
       R = 1 
       DO WHILE ( C .GT. R ) 
              A(C,R) = 1 
              C = C - 1 
       END DO

Re: GOTOphobia considered harmful in C

#139
post #50

I always encourage people to go read Dijkstra's GOTO paper, instead of just its title. It's a short and easy read, almost like a blog post. If you pay attention, you can see that the he was talking about spaghetti code vs structured code. It's better when the lexical structure of the source code maps to the execution structure. That is, if you know what is the current line being executed, you have a good idea of what…

[1] on structured concurrency gives good context for Dijkstra's GOTO paper and how it is applicable today.

[1] https://vorpus.org/blog/notes-on-structured-concurrency-or-g...

Re: GOTOphobia considered harmful in C

#140
post #77
post #50

I always encourage people to go read Dijkstra's GOTO paper, instead of just its title. It's a short and easy read, almost like a blog post. If you pay attention, you can see that the he was talking about spaghetti code vs structured code. It's better when the lexical structure of the source code maps to the execution structure. That is, if you know what is the current line being executed, you have a good idea of what…

The thing is that many people today have never encountered the sort of spaghetti code that Dijkstra was talking about in 1968. There's plenty of confusing and messy code around, but true spaghetti code that GOTOs all over the place and is nigh-impossible to follow has been extremely rare for a long time. I can't recall encountering it in the last 30 years. It easy to misunderstand what he was even talking about becau…

To give people some kind of an idea of what it was created in response to: Imagine writing an entire program in one single main function. The only thing you're allowed to do for flow control is goto. You can do 'goto somelabel;' for an unconditional goto, or you can do 'if (somecondition) goto somelabel;' for a conditional goto. Here's some examples of how it would look if if translated to something C-like:

Loops would look like:

    int i = 0;
    loop_start:
    print i;
    i = i + 1;
    if (i 
Fizzbuzz would look something like:

    int i = 0;
    loop_start:
    if (i % 5 != 0) goto not_fizzbuzz;
    print "fizzbuzz";
    goto done;
    not_fizzbuzz:
    if (i % 3 != 0) goto not_fizz;
    print "fizz";
    goto done;
    not_fizz:
    if (i % 5 != 0) goto not_buzz;
    print "buzz";
    goto done;
    not_buzz:
    print i;
    done:
    i += 1;
    if (i 
Often, this wasn't just constrained to a single function; the whole program would be constructed like this, with gotos which jump back and forth across pages and pages of code. Languages wouldn't even have a call stack with subroutines (which is why "procedural" languages -- languages with procedures -- were important enough to be given a special name).

At least that's my understanding of it. I haven't lived through this, and my only experience with this kind of stuff is writing assembly, where we always make use of a call stack, so even that is in practice a procedural language. If I have gotten anything wrong, please correct me.

Post reply on HN