Code of the Day
BeginnerMental Models

Decomposition

Breaking a big problem into named, testable pieces — the skill AI agents amplify most.

FundamentalsBeginner8 min read
Recommended first
By the end of this lesson you will be able to:
  • Split a large problem into small parts with clear contracts
  • Recognise what makes a good function (one job, clear name, clear inputs/outputs)
  • Explain why decomposition makes you more effective with AI agents

No one solves a big problem all at once. They break it into pieces small enough to hold in their head, solve each, and combine the results. In code, the unit of decomposition is usually the function: a named piece of logic with inputs and an output. Decomposition is arguably the core skill of programming — and the one that pays off most when working with AI agents.

A good function does one job

The test of a well-decomposed piece is that you can describe it in a single sentence without using "and." Compare:

  • ✅ "Calculate the total price of a cart."
  • ❌ "Calculate the total price and apply the discount and email the receipt."

The second is really three jobs. Splitting them makes each easier to name, test, reuse, and reason about — and lets you fix one without endangering the others.

Name the parts, define the contract

Two things turn a vague blob into a clean piece:

  • A clear name. calculateTax tells you what it does; doStuff hides it. If you struggle to name something, it's often doing too much.
  • A clear . What does it take in, and what does it give back? "Give me a cart, I give you a number" is a contract you can rely on without reading the insides.

This is the same idea as the program triad from earlier: inputs (data) → instructions → output. A function is that triad, named and reusable.

Composition: small reliable pieces, combined

Once you have trustworthy small pieces, you build big things by composing them — the output of one feeding the next:

total      = calculateSubtotal(cart)
withTax    = applyTax(total)
finalPrice = applyDiscount(withTax, coupon)

Each line is boring and obviously correct. The power is in the arrangement, not any single step — exactly like the CPU's simple instructions.

Why this matters with AI agents

An AI agent is brilliant at filling in a well-defined piece — "write a function that takes a cart and returns the subtotal." It is far less reliable when handed a giant, fuzzy goal. You do the decomposition: carve the problem into clear, named pieces with contracts, and let the agent implement them while you verify each fits. Good decomposition is what turns an agent from a gamble into a tool.

A practical rhythm: decompose until each piece is something you could confidently ask an agent to write and check in one sitting. If a piece is too big to review, it's too big to delegate.

Where to go next

Even well-decomposed code breaks. Next: reading error messages — turning failures into directions instead of dead ends.

Finished reading? Mark it complete to track your progress.

On this page