Replace for Goto

Daniel Fackrell newsgroups.NOSPAM at dfackrell.mailshell.com
Fri May 30 01:40:41 EDT 2003


"John Pote" <john at jptechnical.demon.co.uk> wrote in message
news:KnXvXIAv$o1+EwL3 at jptechnical.demon.co.uk...
> Some interesting replies on this topic. Despite protests from many
> fellow programmers I still think the GOTO can be used appropriately,
> though not very often - spaghetti logic is bad/hard/bug ridden to debug
> and impossible to maintain. The main use to my mind is in a part of a
> program that models a state machine. Finite State Machines are widely
> used in many embedded control systems (my own area) because they model
> how external events cause changes in the system. All states are at the
> same structural 'level' in the program, they are not subroutines. In a
> state called 'motor off' an event (operator presses button) causes the
> FSM to perform an action (turn on the motor) and then move to the next
> state, for example 'motor on'. This change of state is semantically a
> goto. In this case a goto statement exactly fits the real world object
> the program is modelling. My thinking remains the same whether it is the
> real system  or the program that I am thinking about.
>
> That said it is quite easy to construct a framework using a loop with a
> state variable representing the current state and ifs or a dictionary to
> call subroutines implementing each state, only a little overhead has
> been added. This approach also means that at the end of each pass
> through the state machine the program can 'do' something else before
> looping back for another pass through the state machine.


As an example, I ran across this recently while looking for a reference
implementation of a state machine-based parser:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/146262

--
Daniel Fackrell (newsgroups.NOSPAM at dfackrell.mailshell.com)
When we attempt the impossible, we can experience true growth.






More information about the Python-list mailing list