This content originally appeared on DEV Community and was authored by David Abbasov
🤖 FSM in my program
Back to my bot.
What I want is for the FSM to be able to connect to the whole system — basically, to conduct and coordinate everything.
To do that, I need to implement this approach into almost every package — which basically means rewriting the old version of the program under a new principle.
But I’m used to this — I’ve probably rewritten this bot already for the third time.
And every time, the bot becomes more and more like a human brain, and the code gets better and better — and that truly brings joy to a developer.
📐 What is FSM (in simple terms)
If you’re still reading, I’ll quickly explain what a finite state machine actually is, from the perspective of math and programming, and why it’s so fascinating.
A finite state machine is an abstract model that can exist in only one of a limited number of states at any given moment.
It transitions from one state to another in response to some external input.
That transition is called... well, a transition.
Now remember the elevator example — it responds to a button press and goes from “waiting on floor 1” to “doors closing” to “moving to the selected floor.”
🧮 mathematical model
Formally, a finite state machine is defined as a 5-tuple (don’t worry, just skim it for structure):
M = (Q, Σ, δ, q0, F)
Where:
Q — a finite set of states
Σ — a finite set of input symbols (events)
δ : Q × Σ → Q — the transition function (for deterministic FSM) or
δ : Q × Σ → P(Q) — for non-deterministic FSMs (where P(Q) is the power set of Q)q0 ∈ Q — the initial state
F ⊆ Q — the set of final or accepting states
⚙️ Where FSM is used
Now that you’ve seen those scary symbols — enough suffering.
Let’s talk real-life use cases.
FSMs are widely used in many areas of development. Most common ones include:
parsing formal languages (lexers, regex)
modeling object behavior (game characters, UI states)
designing communication protocols
managing business processes
bots and other systems — basically, anything where a program needs to interact with something else
🧪 How to use FSM in your own project
At some point, you might want to try FSM yourself — maybe in your own project, or propose it to your team lead.
First thing: it’s not that simple.
You’ll need to understand all the internal logic of your program — you’ll need to become the program and think like it.
You’ll need to figure out how it reacts — not just to external events, but also to its own internal actions.
You have to realize that every reaction may trigger a kind of butterfly effect — one thing leads to another.
And not all transitions are obvious.
Some program states (user states, external system responses, etc.) may be hidden deep inside.
But it’s not as hard as it sounds — and honestly, it’s the best way to fully understand your program.
Once you understand how it “thinks,” you’ll be able to change its personality.
You could make it soft and obedient — like a cat that purrs when you pet it (that’s also a kind of FSM: you repeat the same action, and the cat reacts in a predictable way).
Or you could make it aggressive — like a wild animal.
It depends on what you’re building.
🧰 Ways to implement FSM
Okay, enough theory — let’s talk code.
There are several ways to implement an FSM:
switch-case statements
transition tables (e.g. maps of maps)
state-as-class (State Pattern) — each state is represented as a class that implements a common interface; the FSM holds a reference to the current state
There are more ways, but that’s for another talk — this post is long enough already.
These basic examples should be enough to get started.
But if you want to go deeper, here are a few resources:
For basic mathematical understanding:
https://cs.stanford.edu/people/eroberts/courses/soco/projects/2004-05/automata-theory/For practical programming usage:
https://statecharts.dev/For more community examples:
https://stackoverflow.com/questions/tagged/finite-automata
P.S. What’s next
But if you’ve read this far — feel free to drop a comment or message me.
Right now I’m working on several cool projects:
one about processing data and generating advanced reports — Go + Google Ads API + Google Sheets
another is a bot that helps people track everything in their daily life: time, habits, learning, food, focus — kind of like a smart guide for personal awareness
If you're a designer or just starting to build your portfolio — I also have some great tasks for you.
Okay, this post turned out way too long.
I’m off to finish building that FSM.
It’s 7 AM — time to get to work.
This content originally appeared on DEV Community and was authored by David Abbasov

David Abbasov | Sciencx (2025-07-30T05:39:54+00:00) 🤖 FSM in my program. Retrieved from https://www.scien.cx/2025/07/30/%f0%9f%a4%96-fsm-in-my-program/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.