Exercises — YAGNI — You Aren't Gonna Need It
Parent: YAGNI — You Aren't Gonna Need It. This page is a graded workout. Every problem has a fully worked solution hidden in a collapsible callout — try first, then reveal.
Before we start, one small piece of math machinery we lean on repeatedly. The parent note gave you the build-now inequality:
Everything below is either applying this rule, or applying the workflow from the parent (trigger check → build simplest → refactor on the Rule of Three).

The figure above is our mental picture for the whole page. The horizontal amber dashed line is the certain cost — here fixed at hours; it does not move because you pay it no matter what. The straight cyan line is the expected payoff (with hours): it starts at when and climbs as your guess becomes more likely. They cross at the white dot, the break-even probability . Read the axes literally: the horizontal axis is , the vertical axis is value in hours. The shaded region on the left is where sits below the amber line — "don't build". Almost all real features live in that shaded region because their is small.
Level 1 — Recognition
L1-Q1
Which of these is the correct expansion of the acronym YAGNI? (a) You Always Get New Ideas (b) You Aren't Gonna Need It (c) Yield A General Non-Interface
Recall Solution
(b) You Aren't Gonna Need It. It is a principle from Extreme Programming: don't add functionality until it is actually needed now.
L1-Q2
Classify each item as SPECULATIVE (YAGNI says defer) or PRESENT NEED (build it well now):
- A
backend="postgres"parameter with comments listing"mongo","s3"for "someday". - Input validation on a public API endpoint receiving untrusted data.
- A generic
send(template, locale, channel, retries, priority, schedule)when the only ticket is "send a welcome email". - Authentication on an admin-only route.
Recall Solution
- Speculative — no ticket asks for Mongo/S3. Defer.
- Present need — untrusted input is a current security/correctness requirement. Build it.
- Speculative — only one channel is needed today; the extra parameters are untested surface area.
- Present need — admin routes require auth now to be correct.
Rule of thumb: YAGNI targets speculative future features, never non-functional requirements of the present.
Level 2 — Application
L2-Q1 (plug into the inequality)
A speculative "multi-currency" feature has , hours, hours, hours. Should you build it now?
Recall Solution
Compute both sides.
- Expected saving: hours.
- Certain cost: hours.
Is ? No. The inequality fails → do not build now. You would pay 18 hours for certain to chase an 8-hour expected reward.
L2-Q2 (find the break-even probability)
Keep , , . What is the smallest (call it ) that would just justify building now?
Recall Solution
Set the two sides equal and solve for : You'd need to be at least 45% sure the guess is right just to break even. Since real speculative is usually far below that, YAGNI wins.
L2-Q3 (apply the workflow, not the algebra)
Ticket: "Save user records to PostgreSQL." A teammate writes:
class UserRepo:
def __init__(self, backend="postgres"):
self.backend = backend # mongo, mysql, s3 ... somedayRewrite it the YAGNI way and justify in one sentence.
Recall Solution
class UserRepo:
def __init__(self, pg_conn):
self.pg = pg_connJustification: No ticket asks for another database, so the backend abstraction has tiny yet real (read/tested/maintained forever); if a real switch ever comes you refactor then, informed by the actual interface. This mirrors KISS — Keep It Simple: build the present thing, simply.
Level 3 — Analysis
L3-Q1 (why "later" is often cheaper)
The parent claims (build it when truly needed) is often less than (build it speculatively now). Give the mechanism, then decide: with , , , , and if you wait you'd pay only in the world — which path is better?
Recall Solution
Mechanism: building later, you have the real requirement in hand, so you avoid guessing the wrong interface and the rework that follows. Guessing now risks building the wrong shape → wasted build + a rebuild.
Expected cost of "build now". You always pay for certain. Then, with probability , that spend pays off and saves you ; with probability it saves nothing. The expected saving is therefore . Why do we simply subtract from the certain 19? Because expectation is linear: the expected value of (certain cost minus an uncertain saving) equals (the certain cost) minus (the expected saving) — the two pieces can be averaged separately and combined. So Expected cost of "wait". You pay nothing up front, and only in the world do you pay — again by linearity the expected cost is : → waiting wins by 7 expected hours.
L3-Q2 (the "free hook" fallacy)
A hook takes only 5 minutes to write ( min). Over a year, 20 people read the codebase, each losing 2 minutes understanding the unused hook, and 3 refactors each spend 15 minutes preserving it. If and minutes, was the hook worth it?
Recall Solution
The trap is counting only . Compute the hidden carry cost:
- reading: min
- refactors: min
- min.
Certain cost min. Expected saving min. Is ? No — the "5-minute" hook actually costs 90 minutes for a 12-minute expected reward. Not worth it.
Lesson: , not , is the real price of speculation.
Level 4 — Synthesis
L4-Q1 (YAGNI vs DRY vs Rule of Three)
You spot two functions with the same 6 lines of code. A colleague immediately extracts a shared helper "to be DRY". Is this correct under YAGNI? At what point should you extract?
Recall Solution
Not yet. DRY — Don't Repeat Yourself removes existing duplication, but abstracting on the first repeat is speculative: you're guessing the two cases will stay identical and that a third will come. That guess (small , real ) is a YAGNI violation.
The correct timing is the Rule of Three: extract on the third real occurrence, when you have enough real examples to see the true shared shape. Two data points can't tell you which parts are genuinely common vs coincidentally alike.
Summary: DRY tells you what (remove duplication), YAGNI/Rule-of-Three tells you when (on the third real case).
L4-Q2 (design the forecast-then-verify log)
Design a tiny "prediction ledger" that turns YAGNI from opinion into evidence. What columns, and how does it feed back into the inequality?
Recall Solution
Columns: feature, predicted_need_date, date_logged, actually_needed? (Y/N), outcome_date.
Loop:
- Every time you want to build speculatively, log the prediction instead of coding it.
- Periodically review: count how many predictions came true.
- That hit-rate is your empirical — replace gut feeling with data in .
Most teams discover their real is tiny, which quantifies why YAGNI wins and links it to avoiding Technical Debt (speculative code is pre-paid debt with no asset) and Premature Optimization (the same speculation disease, performance-flavored).
Level 5 — Mastery
L5-Q1 (general condition + edge cases)
Starting from , derive the general break-even probability , and interpret all edge cases: , , , and .
Recall Solution
Domain assumption first. Savings cannot be negative — you can never lose value by having correctly guessed a need — so . We split on whether is zero or strictly positive, because we are only allowed to divide by when (dividing by zero is undefined).
Derivation (case ). Divide both sides by . Since , the direction of is preserved: Edge cases:
- (no saving even if right): we may not divide by ; go back to the original rule, which becomes , impossible for nonnegative costs → never build.
- (magically free to keep): . Still need above that; carry-free code lowers the bar but doesn't remove it.
- : the threshold exceeds certainty, so even a guaranteed need () wouldn't justify it → never build. Happens when .
- : only if costs are (a feature that pays you to build). Then always build — but this never happens in practice; costs are positive.
This is the complete map: for realistic positive costs, , and you build only if your evidence-based clears it.
L5-Q2 (full scenario, end to end)
You have a live app. Ticket: "Add CSV export of the current report." You're tempted to build a generic Exporter supporting CSV, XLSX, JSON, and PDF "since reports obviously need all formats eventually." Estimates: for the generic exporter h, h/year; the extra formats have combined of all being needed and h. The CSV-only version is h, . Walk the full YAGNI decision and give the recommended code shape.
Recall Solution
Step 1 — trigger check. Only CSV has a ticket. XLSX/JSON/PDF are speculative (no present requirement).
Step 2 — inequality on the speculative extra. The generic layer's cost above CSV-only is roughly h in year one. Expected saving h. Is ? No → don't build the generic exporter.
Step 3 — build the simplest present thing.
def export_report_csv(report):
... # just CSV, the one ticketStep 4 — refactor on the Rule of Three. When a second real format lands, still keep them separate; on the third real format, extract an Exporter interface shaped by the three real cases.
Present needs stay in: if this export endpoint receives untrusted parameters, validation and auth are built now — they satisfy a present requirement, and YAGNI never deletes those.
Wrap-up recall
Recall One-line takeaways (cover the right side)
Break-even probability formula? ::: . Why does the "5-minute hook" cost far more than 5 minutes? ::: Because (reading/testing/refactoring by everyone, repeatedly) dominates the one-time build cost. DRY vs YAGNI timing? ::: DRY removes existing duplication; extract only on the third real case (Rule of Three), else you violate YAGNI. Does YAGNI defer validation/auth on a live endpoint? ::: No — those are present requirements, built well now. If , should you ever build speculatively? ::: No — zero saving can never beat positive certain cost.