⏱️ Reading time: 11 min

A code generator more than ten years old, responsible for translating AWS service definitions into Haskell packages, had become so tangled that any change risked breaking one of the dozens of APIs it generates code for. That was the signal that pushed developer Jack Kelly to apply a move that cleanly separates the part of a program that decides what to do from the part that does it, a pattern known as making the plan a value [source: Make the Plan a Value, Jack Kelly].

📑 En este artículo
  1. What “Plan as Value” Means
  2. Why Separating Deciding from Doing Matters
  3. A Simple Example: Pure Decision, Separate Execution
  4. When It Pays Off: Testing, Logging, Dry-Run, and Idempotency
  5. Comparison with Related Patterns
  6. Limitations and When It’s Not Worth It
  7. What This Means for You If You Maintain Deployment Scripts or Integrations
  8. Frequently Asked Questions
    1. Do I need algebraic data types like Haskell’s to apply this?
    2. Is this the same as the Command pattern?
    3. How much does it cost to apply this to a script that already exists?
    4. Does this only apply to infrastructure and backend?
  9. References

The context was concrete. Kelly was finishing up support for idempotency tokens in amazonka, the unofficial Haskell SDK for AWS, building on the client token support that AWS itself offers. To do that, he had to modify the generator, the program that converts botocore definitions into Haskell packages, which had grown “gnarly” because every AWS service returns its responses differently: XML, JSON, raw bytes, or extra data in HTTP headers [source: Make the Plan a Value, Jack Kelly].

What “Plan as Value” Means

The move doesn’t depend on Haskell, AWS, or any particular language. It consists of introducing an intermediate data structure, a “plan” or a value of some “plan type”, that describes what is going to happen before any action runs. In practice, a program splits into two functions: one examines the input state and returns that value; the other receives it and only then acts, whether by calling an API, writing to disk, or triggering a migration.

Neither function needs to know the internal details of the other. They share only the plan’s type, which acts as an explicit contract between deciding and doing. Kelly sums up the idea in a simple phrase: decide what to do, then do it [source: Make the Plan a Value, Jack Kelly].

Why Separating Deciding from Doing Matters

The part of a program that decides is usually the most complicated: it has to read the current state, apply business rules, and produce the correct decision for every case. Separating it from execution turns it into a pure function, with no network calls or side effects, which makes writing test cases simpler because it’s enough to compare the returned plan against the expected plan, without mocking any API [source: Make the Plan a Value, Jack Kelly].

If the “doing” phase is also complex, due to remote calls, retries, rollbacks, or parallel execution, separating it helps in the other direction: those problems stay isolated from the decision logic, which doesn’t have to worry about them. And there’s an almost free benefit: adding a dry-run mode. If the decision already lives in an explicit value, it’s enough to write a second consumer that, instead of executing the plan, prints it [source: Make the Plan a Value, Jack Kelly].

Kelly mentions a real problem in passing: a colleague, Henrik, pointed out that checking a dryRun variable scattered across the codebase makes the code messy [source: Make the Plan a Value, Jack Kelly]. Kelly’s proposed alternative is exactly to separate the planning phase from the execution phase, and feed that second phase with either a dry-run printer or a real executor as needed, so the dryRun checks disappear from the middle of the business logic.

Separating decision from execution lets you test the plan without calling any real API. Foto de Maxim Hopman en Unsplash

A Simple Example: Pure Decision, Separate Execution

Consider a typical deployment script: it creates a new table if missing, migrates data from an old column, and notifies a channel when done. The most common version mixes deciding with doing: at each step, the code checks whether action is needed and, if so, acts right there. Adding a dry-run forces wrapping every call in a condition, and adding logging forces repeating the same message in several different places.

The alternative splits this into two steps. First, a pure function receives the state (which tables exist, which columns have data) and returns a plan: an ordered list of steps, each described as a value (“create table X”, “migrate column Y to Z”, “notify channel W”). Then, an executor receives that plan and walks through it. In production, the real executor calls the corresponding APIs. For dry-run, a second executor prints each step. For testing, the test only needs to check that the decision function returns the correct plan, without touching any real API.

A typical test case would be: input state with the table already created and the column not yet migrated, expected plan with a single step (“migrate column Y to Z”), without recreating the table. None of that requires spinning up a test database or intercepting HTTP calls, it’s just comparing one value against another.

flowchart TD
  A["Current state"] --> B["Decision function (pure)"]
  B --> C["Plan (value)"]
  C --> D["Real executor"]
  C --> E["Dry-run executor"]

The diagram sums up the contract: the plan is the only point of contact between deciding and executing. Anything that consumes that plan, a real executor, a dry-run printer, or a logger that just records the plan before running it, can be added without touching the decision function.

When It Pays Off: Testing, Logging, Dry-Run, and Idempotency

Kelly names four concrete benefits of this move, and all four show up again and again in infrastructure code, deployment scripts, and integrations with external APIs.

  • Testing: the decision function is pure, so a test feeds it an input state and compares the returned plan against the expected one, with no network mocks or test databases [source: Make the Plan a Value, Jack Kelly].
  • Logging: instead of logging scattered messages as the program acts, you can log the full plan value before running anything, leaving an audit trail of what was decided. An SRE team can even require that plan to be approved by someone else before running a production migration, something impractical when decision and execution are mixed together.
  • Dry-run: an alternative executor that prints the plan instead of applying it is enough, with no need to repeat dryRun conditionals throughout the code [source: Make the Plan a Value, Jack Kelly].
  • Idempotency: separating decision from execution leaves a natural place to inject a unique identifier per operation, like AWS’s client token, right in the “doing” phase, without touching the logic that decides what to request. According to Kelly, once the amazonka generator already separated that configuration from code generation, adding idempotency tokens was “straightforward” [source: Make the Plan a Value, Jack Kelly].

Kelly himself notes this isn’t a new or obscure technique: he’s seen it called the plan-execute pattern, and the enterprise integration pattern known as the routing slip is literally about centralizing decision logic before executing anything [source: Make the Plan a Value, Jack Kelly]. The table below compares three ways of solving the same problem.

ApproachWhere the decision livesWhat you gainCost
Plan as value / plan-execute patternA pure function that returns an explicit valueTests without mocks, near-free dry-run, a natural place for idempotencyYou have to design the plan type
Routing slip (enterprise integration pattern)A document or message that travels with the request and centralizes the decisionDecouples producers and consumers across several servicesDesigned for messaging between services, not for a local function
dryRun conditionals scattered through the codeAt every point where an action runsZero new structure, quick to addClutters the code with repeated checks, per the critique Kelly cites

Searching for this pattern by name on GitHub, as if it were a library to install, doesn’t help much: a search for those keywords returns all kinds of results, from a list of programming exercises [source: ManojKumarPatnaik/Major-project-list] to a repository with no real connection to the topic [source: Sfedfcv/redesigned-pancake]. That indirectly confirms something useful for anyone who wants to apply it: there’s no package to install, it’s a design technique each team reimplements with its own types.

The routing slip pattern centralizes the decision in a document that travels with the message. Foto de Walls.io en Unsplash

Limitations and When It’s Not Worth It

The indirection added by a plan type isn’t always worth it. For a one-off script of just a few lines, wrapping the decision in an intermediate value is extra work: it’s better reserved for code that will be maintained, tested, or run repeatedly.

Choosing how detailed the plan should be is also a design decision: a plan that’s too coarse, with a single step like “deploy everything”, doesn’t improve testability, and one that’s too fine-grained multiplies types without real need.

Migrating old code to this pattern all at once isn’t realistic in a large codebase either. Kelly did it incrementally, using a technique known as the strangler fig pattern: he identified the least tangled code path, added a temporary “do things the old way” constructor for everything not yet migrated, and replaced one path at a time, regenerating the generated AWS bindings at each step to confirm nothing broke before deleting the old code [source: Make the Plan a Value, Jack Kelly].

That temporary constructor is also the main risk: if the “old way” path sticks around forever because no one finishes the migration, the result is worse than not having started, two parallel paths to maintain instead of one.

What This Means for You If You Maintain Deployment Scripts or Integrations

If you work in backend, on an SRE team, or on integrations with external APIs (payments, AWS, any service that charges per operation), this pattern has a concrete entry signal: count how many dryRun, verbose, or force checks your deployment script or data migration job has. Three or more scattered through the code is the signal that it’s worth extracting a decision function that returns a plan, and writing two consumers (one real, one that just prints) instead of piling on more conditionals.

The same applies if your next task includes adding an idempotency identifier to a call to an external API: before inserting the token in the middle of the logic that decides what to request, separate that logic into a value and add the token in the execution phase. It’s the same change that let Kelly add idempotency tokens to amazonka without rewriting the whole generator [source: Make the Plan a Value, Jack Kelly].

📬 Get new articles by email

We only email about big articles (1-2 a month).

Frequently Asked Questions

Do I need algebraic data types like Haskell’s to apply this?

No. Any language with records, structs, dataclasses, or enums is enough: what matters is the intermediate value that describes the plan, not the type system used to represent it.

Is this the same as the Command pattern?

They’re related but not the same. Command usually encapsulates a single executable action; a plan, on the other hand, is usually a list or structure with several decisions interpreted together, closer to what Kelly calls a “tiny DSL” [source: Make the Plan a Value, Jack Kelly].

How much does it cost to apply this to a script that already exists?

It depends on the size. For migrating something large, the incremental strangler fig technique Kelly used, leaving an old path behind a temporary constructor while migrating piece by piece, avoids having to stop production code while refactoring [source: Make the Plan a Value, Jack Kelly].

Does this only apply to infrastructure and backend?

Kelly’s example is from infrastructure, but the same separation applies to any logic with side effects: a form that validates data and builds an “actions to send” object before calling an API also benefits from the same explicit contract between deciding and doing.

References

Featured image: Foto de MARIOLA GROBELSKA en Unsplash

How each article is made (sources, testing and editorial criteria): How we work.

Did it work for you? Got a different error? Say so below: questions get answered and help the next reader.

Leave a comment
Categories: Tech NewsTutorials

El Solitario Editorial

Article prepared by the El Solitario editorial team from cited sources, under the editorial responsibility of Luis Navarro. More details in "How we work".

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

You can include code inside <code>…</code> or, for several lines, <pre><code>…</code></pre>.

This site uses Akismet to reduce spam. Learn how your comment data is processed.