State Machines

Complete Developer Podcast - A podcast by BJ Burns and Will Gant - Thursdays

Categories:

Often, you’ll run into situations in your career where part of a system will become difficult. Occasionally, an older, more experienced developer will suggest using a state machine to more cleanly organize the code. While this approach is admittedly uncommon, there are times that it can work really well. State machines are an excellent way to handle certain situations that occasionally come up in code. {Will note about these coming up in UI situations}. While state machines are seldom used, they work very well in very specific circumstances where a system has a finite (and small) number of possible states, with clear transitions between them. While this does not happen all the time, you’ll often see situations, especially with complex UI workflows, where a state machine is the best way to organize things. While you won’t often use a state machine, it’s helpful to know when they are useful and when they are not, so that you can use them when they will simplify things. State machines are not always the right solution to complex software problems, but they perform remarkably well when they are the right solution. However, if you didn’t have formal computer science course, you may be a little confused by them when you first look at them. That’s ok. We hope that this breakdown is enough to let you discuss them intelligently with the compsci nerds on your team. They can be very useful at times, but they have their limits. It’s important to understand these limits when attempting to use state machines to simplify your code. Furthermore, if you haven’t had a formal computer science education, you are actually MORE likely to understand these limits. Episode Breakdown Definitions State Not cleanly defined. However, per wikipedia, “a system is described as stateful if it is designed to remember preceding events or user interactions; the remembered information is called the ‘state’ of the system. The set of states in a system is known as its state space”. State transition when a state changes in a state machine, the system is said to be going through a state transition. A state transition is the set of operations that occurs between one state and another. Finite state machine “A finite-state machine (FSM) or finite-state automaton (FSA, plural: automata), finite automaton, or simply a state machine, is a mathematical model of computation. It is an abstract machine that can be in exactly one of a finite number of states at any given time. The FSM can change from one state to another in response to some inputs; the change from one state to another is called a transition. An FSM is defined by a list of its states, its initial state, and the inputs that trigger each transition.” Initial State the default (or beginning) state of a finite state machine. Final State the state of a state machine at the end of computation. Essentially the end of the flowchart. Event A callback raised when a state transition occurs. Turing machine “a mathematical model of computation that defines an abstract machine that manipulates symbols on a strip of tape according to a table of rules.” This is the mathematical basis of computing. This was defined (in 1936) by Alan Turing. Turing Completeness the ability of a system of instructions to simulate a Turing machine. A programming language that is Turing complete is theoretically capable of expressing all tasks accomplishable by computers. Nearly all programming languages are Turing complete if the limitations of finite memory are ignored (aka, Microsoft Teams…?). Understanding State Machines

Visit the podcast's native language site