State Machine
In e-commerce, every item which is ordered then needs to be processed, packed and shipped to the end customer. This can be very complex and looks different for every company. State machines are essentially ‘business process models’ which create a standard way of configuring and executing business processes such as payment, shipping, and more to ensure uniformity.
What does State Machine mean?
The term state machine comes from the fields of computer science and mathematics. More specifically, state machines are studied by automata theory, a branch of theoretical computer science that focuses on abstract machines and their applications in solving computational problems.
Some basic vocabulary is needed to understand what a machine state is:
- State: A state refers to the status of a system and its characteristics.
- Transition: A transition is a series of actions that are performed when an input is received or when a condition is met.
- Input: In the realm of computer science, an input is a signal or command from outside the system.
A state machine is a helpful programming architecture that helps coders reduce the complexity of a process. They enable straightforward modeling of a process into states that depend on previous states and inputs. The programmers can concentrate on each state separately by dissecting the whole thing into little independent pieces that are easy to deal with. Then, they define the types of inputs each state accepts. The transition takes the system to a different state. There is no need to model a linear process. Inputs can move back and forth through blocks without the risk of ill-defined logic.
Although there is a formal distinction between finite and infinite state machines, in practical terms, only the former is useful in implementations.
For example, a customer purchases a product in an online store that triggers a fulfillment need. A system that efficiently deals with the fulfillment process can be difficult to implement due to the complexity of all possible influencing factors and the challenge of merging information from different sources. With state machine architecture, it is possible to describe the entire fulfillment cycle in one system through states and transitions. In this sense, well-defined states represent when an order is received, payment is confirmed, the product is packed, the logistics company has picked up the product, and the product has arrived at its destination. Each state can help automize actions like sending confirmation emails and mobilizing the logistics company.
Although there is a formal distinction between finite and infinite state machines, in practical terms, only the former is useful in implementations. Finite state machines have a limited set of possible states and transitions. Therefore, finite state machines are referred to as state machines whenever it is not otherwise specified.
State machines are represented with state diagrams or state–event tables.
- State diagrams – State diagrams represent all the possible states in a system, the transitions among them, and the inputs that trigger each transition. They represent the flow in the system. Standard notation is used to identify each type of element quickly.
- State-event tables – State-event tables show a list of all possible current states on one axis and all possible inputs on the other. If an input can act upon a state, its combined row and column position will show the state into which the system will transition.
Deterministic vs. Non-Deterministic State Machines
- Deterministic state machines: Deterministic state machines are systems that define only one possible transition for each valid input in any state. This structure determines a straightforward transition process from any part of the system, and there are no ambiguities.
- Non-deterministic state machines: In this type of state machines, there exists at least one state in which the same input can lead to different states. It is possible to transform all non-deterministic finite state machines into deterministic state machines.
Types of State Machines
Acceptor state machines
Given a valid input, acceptor state machines will transition into one of two possible states: accepting or non-accepting state. These states determine whether the input will be accepted or not.
Classifier state machines
Classifier state machines are a generalization of acceptor state machines that allow for n number of transition states (with n>2). The result is a system that classifies the input into a category.
Transducer state machines
Transducer state machines are like translators, using labeled transitions for input and output and working with two memory tapes like a Turing machine.
Sequencer state machines
Sequencer state machines are defined by states with only one possible transition. They represent an orderly process that takes the system through the same states in a non-changing order.
Benefits of a State Machine Architecture
-
Programmers can model complicated processes into an organized workflow
Thanks to their powerful approach, state machines allow dissecting processes into states and events that influence these states. After these are defined, the transitions are easier to implement. There is no need to stick to a linear way of thinking.
-
Code is easier to debug and test
When working with lots of conditionals to define workflows, the code quickly becomes very convoluted. The code is hard to read, and the number of states to test becomes unfeasible. Since programmers can analyze each state independently, any problem in the code can be isolated and solved.
-
Transparency and understandability
If choosing a deterministic state machine model, it will be easy to understand where the process is at any moment. This characteristic gives more transparency to the systems.
-
Separation of business logic and system’s functioning
If correctly implemented, state machine architecture can help customers separate the business logic from the system’s functioning.
-
State machines are great ways to enable agility and experimentation
Implementations with state machine architecture can handle the creation of new states without altering existing workflows.