4 ms·
> On the other hand, today we have the very opposite situation: programmers not using goto when it's appropriate and abusing other constructs, what ironically m
by owisd 4y ago
> On the other hand, today we have the very opposite situation: programmers not using goto when it's appropriate and abusing other constructs, what ironically makes code only less readable.
One I see all the time from beginners is creating a finite state machine using one method per state and jumping between states by calling the next state’s method from within the current state’s method. Essentially just emulating goto with the added disadvantage that you’re pushing each new state onto the stack until it overflows, so refactoring it using goto would constitute an improvement.
The fact that beginners reinvent this pattern over and over demonstrates it’s easier for people unfamiliar with programming to reason about a program that uses goto, which would explain why it was so ubiquitous in the early days of computing and given its shallower learning curve its usefulness as a teaching aid as a first step before structured programming is being overlooked.
- rmckayfleming 4y agoThis is one of those problems that Tail Call Elimination cleanly addresses. The "obvious" approach to writing an FSM does exactly what you'd expect.
- hgsgm 4y agoC does not guarantee tail call elimination. But you can do the same with an event loop that cleanly avoids goto.
- IncRnd 4y agoNowadays you can direct clang to require tail-call elimination in C. [1] In gcc you can provide the optimization flag, -foptimize-sibling-calls, which is automatically selected at -O2, -O3, or -Os. [2] [1] https://clang.llvm.org/docs/AttributeReference.html#musttail https://clang.llvm.org/docs/AttributeReference.html#musttail [2] https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
- DonHopkins 4y agoGotos are great approach for writing a Flying Spaghetti Monster!
- dcow 4y agoNot if you don't know how to write in only tail calls. Try explaining tail call elimination to one of these programmers learning how to write a state machine (=
- tester756 4y agoWhere in the world beginners use state machines instead of switch statement or if ladder? this is genuine question, because I do believe that people do not use enough state machines to model their systems.