Introduction to State Machine and Use Cases

State machines model explicit states to control the transition from one value to another in response to inputs.

Digital Analytics
10 min
Digital Analytics
Introduction to State Machine and Use Cases

We are in the age of digital transformation and hyperautomation. Many companies are relying on a popular computer science concept, state machines, to build better software to gain a competitive advantage. State machines are models that represent system behavior.

These models represent an easy way to visualize complex flows through a system. Developers use them to design software that involves multiple steps based on various triggers or actions.

This quick tutorial discusses the concept of state machines, how they are used, and why they are useful. 

What is a State Machine?

State machines model system behavior. Specifically, systems with a finite number of states. Thus, they are also called Finite-State Machines (FSMs). Think of a state machine as a set of signifiers (active/inactive, open/closed, on/off, etc.).

FSMs have a start state, end state, and any number of intermediate states. The system transitions from one state to another based on a condition or triggered event.

The term "state" is defined as the condition or status of a system that is ready to execute a transition.

In response to some inputs, the FSM can transition from one state to another, this change is referred to as a transition. 

simple state machine example

There are two types of basic state machines:

1. Deterministic Finite State Machine  

Deterministic state machines have a fixed order of events. They are also called chronological state machines or order-dependent systems. Consider a traffic light. The light has three stages: red, yellow, and green.

The light changes from green to yellow to red in a set order. The light is in only one state at any given time (the light can not be both green and red).

2. Non-Deterministic State Machines

Non-deterministic state machines allow flexibility. The order of events changes each time the machine executes. Take a real-world example of an elevator.

The elevator has no fixed order to reach a final state. It determines where it goes each time a person presses the button for a specific floor. Non-deterministic state machines are useful to model concurrency, parallelism, or shared state.

Representing system behavior with state transition diagrams

State diagrams (also called state-charts) show the entry action, exit action, and possible inputs for the system. They also show the current state of the system, information about the possible states, and what causes the state to change.

In a state transition diagram, each state is represented as a rectangular box with a label. An arrow shows the trigger for a state change.

A state transition diagram is a valuable tool for visualizing how application inputs, such as user actions, trigger different state changes in the form of outputs, such as API calls or database updates.

Types of Finite State Machines

Mealy machines are a type of simple state machine where the next state is determined by the current state and the given input.

The next state is determined by checking which input generates the next state with the current state. Imagine a button that only works when you’re logged in. That button is a state machine with login as the current state.

The new state is determined by logging in. Once you’re logged in, the next state is determined by the current state which is logged in.

Moore Machines

A Moore State Machine is a type of state machine for modeling systems with a high degree of uncertainty. In these systems, predicting the exact sequence of future events is difficult. As a result, it is difficult to determine the next event.

A Moore model captures this uncertainty by allowing the system to move from one state to another based on the outcome of a random event. 

A Moore model has many applications in both industry and academia. For example, it can be used to predict when a system will fail or when certain events will occur with a high probability (e.g., when there will be an earthquake).

It can also be used as part of an optimization algorithm when dealing with uncertain inputs (e.g., produce only 1% more product than standard).

In addition, Moore models are often used as rules for automatic control systems (e.g., medical equipment) that need to respond quickly and accurately without human intervention.

Turing Machine

The Turing Machine consists of an input tape (with symbols on it), an internal tape (which corresponds to memory), and an output tape (which contains the result).

A Turing Machine operates through a series of steps: it scans its input tape, reads out one symbol at a time from its internal tape, and then applies this symbol as a command (or decision) to its output tape. For example: “If you see ‘X’ on the input tape, then print ‘Y’ on the output tape.”

The input tape can be considered a finite set of symbols, while the internal and output tapes are infinite. The Turing Machine must read an entire symbol from its internal tape before it can move its head to the next symbol on the input tape.

Once it has moved its head to the next symbol, it can read that symbol out of its internal tape and then move to the next symbol on its input tape.

This process continues until no more input or output symbols are left in the Turing Machine’s internal or external tapes (at which point, it stops).

State Machine Use Cases

State machines are helpful for a variety of purposes. They can be used to model the flow of logic within a program, represent the states of a system, or for modeling the flow of events in a business process.

There are many different types of state machines, ranging from simple to highly complex. A few common use cases include:

Modeling Business Workflow Processes

State machines are ideal for modeling business workflows. This includes account setup flows, order completion, or a hiring process.

These things have a beginning and an end and follow some sort of sequential order. State machines are also great for modeling tasks that involve conditional logic. 

Business Decision-Making

Companies pair FSMs with their data strategy to explore the cause and effect of business scenarios to make informed business decisions

Business scenarios are often complex and unpredictable. There are many possible outcomes, and each one impacts the business differently.

A simulation allows you to try different business scenarios and see how each plays out. You can then assess the risk and determine the best course of action.

Software Design

Developers use state machines to model how a user interacts with the application. These models are also helpful in modeling business logic flow in the system.

In both cases, state machines help visualize the flow of the application and organize the code in a maintainable fashion. 

State machines are often used to improve the user experience, especially in apps with lots of steps or where there’s a risk of user abandonment.

For example, a booking app might use a state machine to track the progress of a booking and display a different set of fields based on the stage of the booking.

While the booking process might be linear, the user experience can be improved by offering relevant fields upfront and removing them as soon as they are no longer required.

You can think about this concept as having a conversation with your users: you have a set of questions you need to ask, but the order in which you ask them, and the timing with which you ask them, can have a significant impact on the user experience, and ultimately on your company’s bottom line.

State machines help developers ensure their software is efficient, scalable, and maintainable

Are State Machines right for your organization?

State machines are vital to engineering reliable systems necessary to remain relevant amidst the digital transformation.

FSMs model complex processes in a way that makes them easy for developers to understand. This helps the developer build a system that accurately reflects the requirements and provides a positive user experience. 

If you are rethinking how you build software by using state machines, our team of professionals and our long expertise, can help you figure out the right direction.

Published on
July 6, 2022

Industry insights you won’t delete. Delivered to your inbox weekly.

Other posts