Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Ever wondered why goto is considered harmful? (craftofcoding.wordpress.com)
2 points by todsacerdoti on March 28, 2022 | hide | past | favorite | 5 comments


The flowchart on the right could have had a conditional loop instead of a GOTO, and it would have the same number of possible paths! Are loops considered harmful also?

Personally I don't believe that goto is harmful in any way, and people have to resort to weird contrived arguments to make it so.

...I also take exception at the phrase "unstructured jump". What does he think happens when a GOTO is taken? The CPU just blindly jumps to an address? Maybe check out godbolt next time.


The flowchart shown does not have any loops. Furthermore, a program with goto can also be written using blocks and loops without needing goto, although the program is not always as clean if written in that way (although sometimes, it is better, sometimes worse).


If we're talking the original goto, jump to arbitrary address unrelated to current function, then we're talking whole program transformation to remove it.

Even with the local limited version, I think removing it requires proliferation of Boolean flags, so it's not performance-equivalent unless your compiler manages to undo the refactor back to the goto form.


I did mean the local limited version, but it depends on the programming language (for example, some have a break command and continue command, etc).

However, it is a good point that it is not necessarily performance-equivalent.

I think that (for this reason and for other reasons too), that most general purpose programming languages (and many ones with special purposes, too) should have a goto command. (Although, there are also some kind of programming languages for which I would think that having a goto command would be inappropriate, such as Verilog and Haskell.)


Before I knew about Catch&Throw I invented GOBACK. Logical next step would be SAFE-GOTO. It would GOBACK so much that it can safely GOTO somewhere.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: