Type Safety, Go vs. Chess, and State-Space Explosion

Almost every programmer experiences a recurring temptation:

You are designing a new function or data structure. You need to represent an email address, a user ID, or a temperature. You pause for a second, wondering if you should define a dedicated EmailAddress type or a Kelvin struct.

Then you tell yourself: "Nah, it's just a string. I'll just pass a string and make sure I use it properly."

This pattern is so pervasive that Martin Fowler and the refactoring community gave it a formal name: Primitive Obsession.

On the surface, relying on raw primitives (string, int, float) feels faster and more flexible. But over time, it turns codebases into fragile minefields riddled with silent runtime bugs.

Why does using primitive types make software so incomprehensibly complex?

The answer lies in the profound contrast between two ancient board games: Go (围棋) and Chess (国际象棋).

The Paradox of Simplicity: Chess vs. Go

Consider the fundamental difference in how Chess and Go are designed:

Go: Minimalist Rules, Astronomical Complexity

Go has almost no rules:

  • The board is an open $19 \times 19$ grid with 361 intersections.
  • There is only one type of piece: a stone. A black stone is identical to every other black stone.
  • You can place a stone on almost any open intersection on the entire board.
  • On the very first move, Black has 361 legal moves.
  • Because the pieces have no specialized constraints, the number of possible legal board states explodes to approximately $10^{170}$ (far more than the total number of atoms in the observable universe).

Because the state space is nearly infinite, computers could not defeat human Go grandmasters for decades. Deep Blue defeated world chess champion Garry Kasparov in 1997, but it took until 2016 for AlphaGo to conquer Go—and it required deep neural networks and Monte Carlo tree search just to navigate the unconstrained ocean of possibilities.

Chess: Complex Pieces, Constrained State Space

Chess has far more rules:

  • Six distinct types of pieces: King, Queen, Rook, Bishop, Knight, and Pawn.
  • Each piece has rigid, non-negotiable movement constraints:
    • The Bishop can move any distance, but only diagonally, forever locked onto its starting square color.
    • The Knight can only jump in an L-shape (two squares in one direction, one square perpendicular).
    • The Pawn can only walk forward one square, but captures diagonally.
  • On the first move, White has only 20 possible legal moves (16 pawn moves and 4 knight hops).
  • The total game-tree complexity of Chess is around $10^{123}$—astronomically vast, but orders of magnitude smaller and more structured than Go.
Rule Constraints vs. State-Space Freedom:

Go (围棋):
[ Untyped Stones ] ──► Any intersection ──► Massive State Space (~10^170)
                                            (High bug surface, infinite edge cases)

Chess (国际象棋):
[ Strongly-Typed Pieces ] ──► Rigid Move Rules ──► Constrained State Space (~10^123)
                                                   (Illegal moves forbidden at compile time)

The Mental Model: Primitives are Go Stones, Types are Chess Pieces

Now map this game-theoretic dynamic directly to software architecture:

1. Primitive Obsession is Playing Go

When you declare a function like this:

func RegisterUser(name, email, role, phone string, age, zip int) error

You are playing Go.

A string is an untyped black stone. It can be placed anywhere on the board:

  • It can be a valid email: "[email protected]".
  • It can be an empty string: "".
  • It can be a 10MB SQL injection payload: "'; DROP TABLE users; --".
  • It can be the user's phone number accidentally passed into the name argument because both parameters are plain strings!

Because a standard 64-bit integer or a dynamic string can hold billions of arbitrary permutations, the legal state space of your application explodes.

Every single function that accepts that string must defensively check:

  • Is it null?
  • Is it empty?
  • Does it contain an @ symbol?
  • Is the age negative?
  • Is the zip code 5 digits?

If one function forgets to validate, an illegal state slips into the database. You spend your career writing defensive if conditions, writing hundreds of unit tests to catch edge cases that should never have existed in the first place.

2. Custom Types are Chess Pieces

Now observe what happens when you introduce strong, domain-specific types:

type EmailAddress struct {
    value string
}

type Age struct {
    years int
}

func NewEmailAddress(s string) (EmailAddress, error) {
    if !strings.Contains(s, "@") {
        return EmailAddress{}, errors.New("invalid email")
    }
    return EmailAddress{value: s}, nil
}

func RegisterUser(name string, email EmailAddress, age Age) error

By defining an EmailAddress type whose constructor enforces validation, you turn an untyped Go stone into a Chess Knight.

It cannot move in arbitrary directions. It cannot hold a phone number. It cannot hold an empty string. It cannot hold SQL injection text.

The compiler itself physically prevents you from passing an EmailAddress where an Age is expected:

// Compiler Error! Cannot use age (variable of type Age) as EmailAddress
RegisterUser("Alice", age, email) 

"Make Illegal States Unrepresentable"

The greatest power of a type system is not catching typos—it is shrinking the state space.

In functional programming (Haskell, OCaml, Rust, Swift), this philosophy is formalized as: Make illegal states unrepresentable.

If your system can never mathematically be in an invalid state, you don't need to write runtime tests for that invalid state, you don't need to write error recovery code for it, and your users will never encounter it in production.

Real-World Disasters Caused by Primitive Obsession

  1. The Mars Climate Orbiter ($327 Million Disaster): In 1999, NASA’s spacecraft crashed into the Martian atmosphere and disintegrated. The root cause? One software module computed thruster data using English imperial units (pound-seconds), while another expected metric units (newton-seconds). Both modules stored the values as raw float64 primitives. If they had used distinct types (type PoundForce float64 and type Newton float64), the code would have failed to compile on day one.
  2. Security Vulnerabilities: Almost all Cross-Site Scripting (XSS) and SQL Injections occur because developers treat raw, unescaped user input as a plain string, rather than distinguishing between RawUserInput and SanitizedHTML.

Types are Pruning Shears

Developers often resist creating types because they think: "More types = more boilerplate code."

This is an illusion.

Every time you replace a loose primitive with a constrained, validated type, you are cutting away millions of invalid branches from your application’s state space tree. You are turning a chaotic game of Go into an orderly, predictable game of Chess.

So go ahead: define the type, validate it once at the boundary, and let the compiler do the heavy lifting for you.