A Small State Machine

A tiny experiment: model a simple decision flow as a diagram, then encode it directly in code. Nothing ambitious. Just clarity.

I’ve been thinking about how often I build systems that are more complicated than they need to be.

Sometimes the cleanest thing is just a state machine. A few states. A few transitions. No abstractions pretending to be frameworks.

Here’s a small one. Nothing dramatic. Just a post that can be in draft, published, or archived.

Rendering Mermaid...

That’s it. Three states. Three transitions. No hidden logic.

Now the code version:

typescript
type PostState = "draft" | "published" | "archived"

function transition(current: PostState, action: string): PostState {
  switch (current) {
    case "draft":
      if (action === "publish") return "published"
      break
    case "published":
      if (action === "archive") return "archived"
      break
    case "archived":
      if (action === "restore") return "draft"
      break
  }
  return current
}

There’s something calming about this.

The diagram shows shape. The code shows constraint.

They’re the same idea expressed differently. One is spatial, the other procedural.

Most of the time when something feels messy in a system, it’s because the states aren’t clear.