Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Yes. And not only that, but GOTOs are the normal recommended way of implementing an FSM.


What? Where? Most sane people write FSM as switches inside an infinite loop


Right here, by me. Or perhaps I'm insane.

Using goto ensures that all of a particular state's logic is nearby in code. It also eliminates the need to check an exhaustive list of irrelevant switch cases at runtime.


Relevant example case is in a branching interpreter dispatch loop vs compiling threaded code, and all the hybrid concepts in between: https://en.m.wikipedia.org/wiki/Threaded_code

It's a very old CS construct where right answers depend on the environment. In many modern structured languages you don't have a goto at hand so you're ushered towards switch statements, but then you can opt to do weird things like throw an exception and use the handler to jump.


Makes perfect sense. Languages with first class function values may shine here, ie. Go.


Using a direct goto should be faster because of fewer conditionals.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

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

Search: