What Online Game Type Systems Reveal About Preventing Bad States
Every seasoned developer has chased a bug that should have been impossible: a player both alive and dead, an item equipped and sold at once, a match in two states that cannot coexist.

Every seasoned developer has chased a bug that should have been impossible: a player both alive and dead, an item equipped and sold at once, a match in two states that cannot coexist. These are not logic errors in the usual sense; they are states the program never should have been able to represent in the first place. Game code, with its tangle of overlapping conditions, is where this problem bites hardest, and it is also where the functional idea of making illegal states unrepresentable proves its worth most clearly. A strong type system does not catch these bugs; it makes writing them refuse to compile at all.
The Bug That Should Not Exist
Picture a player record with a boolean for alive, a number for health, and a flag for whether they are respawning. Nothing stops the code from producing a player who is alive with zero health while also respawning, a nonsensical combination that no designer intended. Somewhere deep in the game a line of logic assumed these fields agree, and when they drift apart it crashes or glitches. The traditional fix is to add checks everywhere and hope none are forgotten. The functional insight is more radical: the real mistake was defining the data so that an impossible player could be described at all. The bug exists because the type permitted it to exist.
Making Illegal States Unrepresentable
The alternative is to design the data so that invalid combinations simply cannot be written down. Instead of three loose fields that can contradict each other, you model a player as being in exactly one of a few clearly named states, alive with some health, dead, or respawning, each carrying only the data that makes sense for it. A dead player has no health field to contradict; a respawning player has a timer and nothing else. Now the impossible combination is not caught at runtime, it is impossible to express in the first place. The compiler rejects any attempt to build a contradictory player, turning a whole category of bugs into code that never compiles.
Why Games Expose This So Clearly
Games are a uniquely good teacher here because they overflow with mutually exclusive states and overlapping conditions. A character is grounded or airborne, a match is in lobby or in progress or ended, an ability is ready or cooling down. Each of these invites the sloppy approach of independent flags that can contradict, and each punishes it with glitches that players notice instantly. The sheer density of state machines in a game means the cost of modeling states loosely shows up fast and often. A developer who learns to model one game state properly, as a single value that can only hold valid shapes, quickly wants to model all of them that way, because the payoff in vanished bugs is immediate.
The Compiler as a Design Partner
When illegal states are unrepresentable, the compiler stops being a nuisance that complains about syntax and becomes a partner that enforces the rules of your game world. Add a new player state and the compiler will point you to every place that must now handle it, refusing to let you forget one. Try to read a health value from a dead player and it stops you, because that field does not exist in that state. This turns the type system into living documentation of what is and is not possible in your game, checked on every build. The design intent stops living in comments and conventions that drift and starts living in types the machine actually enforces.
Pushing Validation to the Boundary
A natural partner to this idea is parsing input into precise types at the edge of the system rather than validating loose data deep inside it. A game receiving a move from the network should turn that raw message into a well-typed, already-validated value the moment it arrives, so the rest of the code can trust it completely. Once past the boundary, no function needs to re-check whether the data is sane, because its type guarantees it. This shrinks the surface area where bad data can cause trouble to a single thin layer. The interior of the game becomes a place where, by construction, every value is already valid, which is a profoundly calming way to write complex logic.
What You Give Up and What You Gain
Modeling states this strictly is not free. It takes more thought up front to enumerate the valid shapes and more ceremony to transform between them, and developers used to loose flags often find it tedious at first. The payoff arrives later and compounds. Whole classes of bugs never happen, refactors become safe because the compiler guards every change, and new team members can read the types to learn what the game actually permits. For a small throwaway project the overhead may not pay off, but for anything that must stay correct as it grows, the trade strongly favors precision. The discipline feels slow on day one and feels like a superpower by month three.
A Habit Worth Carrying Everywhere
The deepest value of learning this in game code is that it transfers to everything. Once a developer has felt the relief of a bug that cannot be written, they start seeing loose, contradiction-prone data structures everywhere in ordinary software, and they start tightening them. Payment states, user permissions, form data, order lifecycles, all of it benefits from the same move: model the valid shapes precisely and let the type system forbid the rest. Games simply make the lesson vivid because their states are so numerous and their glitches so immediate. The principle is general, and it is one of the most durable gifts functional thinking offers to any programmer willing to adopt it.
Common questions from players
What does making illegal states unrepresentable actually mean? It means designing your data so invalid combinations cannot be written at all, rather than catching them at runtime. A dead player, for instance, simply has no health field that could contradict its state.
Do I need a functional language to use this idea? It is easiest in languages with rich type systems, but the principle applies anywhere. Even modest support for distinct state variants lets you replace contradiction-prone flags with a single value that only holds valid shapes.
Is this worth the extra effort for a small game? For a quick throwaway, maybe not. For anything that must stay correct as it grows, modeling states precisely pays off fast by eliminating whole categories of bugs and making every later change safer.
Designing Bugs Out of Existence
The most reliable bug is the one the compiler never lets you write, and game code is where that truth lands hardest. By modeling each state as a single value that can only hold valid shapes, you turn impossible combinations into code that refuses to build. Learn the habit on a game's dense state machines, then carry it into every system you touch, because preventing bad states beats hunting them every time.
More in Games
Games
How Online Number Games Turn Probability Into Clean Code
Online number games look simple from the outside. A player picks a few digits, a draw happens, and a result appears.
Games
How Online Game Platforms Reveal Their Inner Systems to Developers
To most players an online game platform is a black box, but to a developer it is a system that leaves evidence everywhere, in...
Games
How an Online Chess Engine Uses Immutable Game State
Watch an online chess engine think and it looks like magic: in a second it considers millions of positions, picks a move, and...