{"id":"6b09a4c1ff996234422a276a","slug":"yagni","title":"YAGNI: don't build ahead – what a feature nobody ordered really costs","summary":"The YAGNI principle through the online shop of Halina's pierogi place: its Extreme Programming origins, the four costs of building ahead, YAGNI vs extension points and feature flags, and removing premature abstraction from payment code in C#, TypeScript, Java, Go and Python.","servedLang":"en","availableLangs":["pl","en"],"topics":["yagni","kiss","modular-monolith"],"techs":["dotnet","typescript","java","go","python"],"tags":["yagni","principles","extreme-programming","speculative-generality","feature-flags","refactoring"],"level":"beginner","kind":"article","access":"members","status":"published","cover":null,"author":{"id":"6b08bd2e80884893657cc8cf","name":"Redakcja Art of Devs","avatar":null,"handle":"redakcja","community":false},"readingMinutes":18,"stats":{"views":1,"likes":0,"bookmarks":0,"comments":0},"publishedAt":"2026-11-27T15:41:53.238Z","updatedAt":"2026-11-27T17:24:30.655Z","requestedLang":"en","theme":{"hero":"classic","heroMedia":null,"accent":"amber","background":"hex","weaveResources":true,"toc":"right"},"seoDescription":"YAGNI (You Aren't Gonna Need It) in practice: origins in XP, the cost of carry of speculative features, YAGNI vs extensibility and OCP, feature flags, examples of premature abstraction and how to refactor them in C#, TypeScript, Java, Go and Python.","wordCount":3438,"doc":{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"When Halina's pierogi place went online, the developer prepared – \"just in case\" – support for euros and Czech korunas, a payment-operator factory, and multi-tenancy for a future franchise. A year later the shop has taken zero orders in euros, there is one payment operator, and still just one restaurant. The code, however, stayed, and every change to the cart has to pass through layers that serve no purpose. "},{"type":"text","text":"YAGNI","marks":[{"type":"bold"}]},{"type":"text","text":" ("},{"type":"text","text":"You Aren't Gonna Need It","marks":[{"type":"italic"}]},{"type":"text","text":") is the principle of building features when you need them, not when you foresee them."}]},{"type":"callout","attrs":{"variant":"tip","title":"In short"},"content":[{"type":"bulletList","content":[{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"Build what you need now; put predictions in the backlog, not in the code."}]}]},{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"A feature built ahead costs more than its construction: it slows down every later change and often has to be reworked once the real requirements arrive."}]}]},{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"YAGNI does not apply to what makes change easier – tests, refactoring, readable code. Those you always do."}]}]}]}]},{"type":"stats","attrs":{"items":[{"value":"1999","label":"\"Extreme Programming Explained\"","hint":"Kent Beck describes XP and simple design"},{"value":"4","label":"costs of building ahead","hint":"build, delay, carry and repair – according to Martin Fowler"},{"value":"0","label":"orders in euros after a year","hint":"yet currency handling touches every cart change"}]}},{"type":"heading","attrs":{"level":2,"id":"where-yagni-comes-from"},"content":[{"type":"text","text":"Where YAGNI comes from"}]},{"type":"paragraph","content":[{"type":"text","text":"YAGNI is one of the mantras of Extreme Programming (XP) – a method that, instead of designing everything up front, delivers small increments and keeps improving the design."}]},{"type":"timeline","attrs":{"items":[{"date":"1996","title":"The C3 project at Chrysler","text":"Kent Beck takes over the project and, with his team (including Ron Jeffries), shapes the practices that become Extreme Programming."},{"date":"1999","title":"\"Extreme Programming Explained\"","text":"Kent Beck describes XP; simple design is one of its practices."},{"date":"2001","title":"The Agile Manifesto","text":"Among its principles: simplicity – the art of maximizing the amount of work not done – is essential."},{"date":"2015","title":"Martin Fowler, \"Yagni\"","text":"Lays out the four costs of building a feature ahead and the limits of the principle."}]}},{"type":"pullQuote","attrs":{"text":"Always implement things when you actually need them, never when you just foresee that you need them.","author":"Ron Jeffries","role":"co-founder of Extreme Programming"}},{"type":"heading","attrs":{"level":2,"id":"the-problem-a-shop-built-for-a-future-that-never-came"},"content":[{"type":"text","text":"The problem: a shop built for a future that never came"}]},{"type":"paragraph","content":[{"type":"text","text":"Here is the payment code after a year in production. Every extension point was created on day one, and none of them has been used:"}]},{"type":"codeGroup","attrs":{"title":"Before – payments with extension points built ahead"},"content":[{"type":"codeBlock","attrs":{"language":"csharp","filename":"Payments.cs"},"content":[{"type":"text","text":"// Before: built \"for the future\" – one operator, one currency, three extension points.\nvar factory = new PaymentGatewayFactory(new PlnOnlyRates());\nvar gateway = factory.Create(GatewayKind.CardOperator);\nConsole.WriteLine(gateway.Charge(\"A-1042\", new Money(45.90m, \"PLN\")));\n\npublic sealed record Money(decimal Amount, string Currency);\n\npublic interface IExchangeRates { decimal ToPln(string currency); }\n\npublic sealed class PlnOnlyRates : IExchangeRates\n{\n    // The only currency we have seen in two years.\n    public decimal ToPln(string currency) =>\n        currency == \"PLN\" ? 1m : throw new NotSupportedException($\"No rates for {currency} yet\");\n}\n\npublic enum GatewayKind { CardOperator } // \"we will add more providers later\"\n\npublic interface IPaymentGateway { string Charge(string orderId, Money amount); }\n\npublic sealed class CardOperatorGateway(IExchangeRates rates) : IPaymentGateway\n{\n    public string Charge(string orderId, Money amount)\n    {\n        var pln = amount.Amount * rates.ToPln(amount.Currency);\n        return $\"Charged {pln} PLN for order {orderId}\"; // the real HTTP call goes here\n    }\n}\n\npublic sealed class PaymentGatewayFactory(IExchangeRates rates)\n{\n    public IPaymentGateway Create(GatewayKind kind) => kind switch\n    {\n        GatewayKind.CardOperator => new CardOperatorGateway(rates),\n        _ => throw new NotSupportedException($\"Unknown gateway {kind}\"),\n    };\n}"}]},{"type":"codeBlock","attrs":{"language":"typescript","filename":"payments.ts"},"content":[{"type":"text","text":"// Before: built \"for the future\" – one operator, one currency, three extension points.\ninterface Money { amount: number; currency: string } // amount in minor units\ninterface ExchangeRates { toPln(currency: string): number }\n\nconst plnOnlyRates: ExchangeRates = {\n  toPln(currency) {\n    if (currency !== 'PLN') throw new Error(`No rates for ${currency} yet`); // the only currency in two years\n    return 1;\n  },\n};\n\ntype GatewayKind = 'card-operator'; // \"we will add more providers later\"\n\ninterface PaymentGateway { charge(orderId: string, amount: Money): string }\n\nclass CardOperatorGateway implements PaymentGateway {\n  constructor(private readonly rates: ExchangeRates) {}\n  charge(orderId: string, amount: Money): string {\n    const grosze = amount.amount * this.rates.toPln(amount.currency);\n    return `Charged ${(grosze / 100).toFixed(2)} PLN for order ${orderId}`; // the real HTTP call goes here\n  }\n}\n\nclass PaymentGatewayFactory {\n  constructor(private readonly rates: ExchangeRates) {}\n  create(kind: GatewayKind): PaymentGateway {\n    switch (kind) {\n      case 'card-operator':\n        return new CardOperatorGateway(this.rates);\n      default:\n        throw new Error(`Unknown gateway ${String(kind)}`);\n    }\n  }\n}\n\nconst gateway = new PaymentGatewayFactory(plnOnlyRates).create('card-operator');\nconsole.log(gateway.charge('A-1042', { amount: 4_590, currency: 'PLN' }));"}]},{"type":"codeBlock","attrs":{"language":"java","filename":"Payments.java"},"content":[{"type":"text","text":"import java.math.BigDecimal;\n\n// Before: built \"for the future\" – one operator, one currency, three extension points.\npublic class Payments {\n    public static void main(String[] args) {\n        var gateway = new PaymentGatewayFactory(new PlnOnlyRates()).create(GatewayKind.CARD_OPERATOR);\n        System.out.println(gateway.charge(\"A-1042\", new Money(new BigDecimal(\"45.90\"), \"PLN\")));\n    }\n}\n\nrecord Money(BigDecimal amount, String currency) {}\n\ninterface ExchangeRates { BigDecimal toPln(String currency); }\n\nclass PlnOnlyRates implements ExchangeRates {\n    // The only currency we have seen in two years.\n    public BigDecimal toPln(String currency) {\n        if (!currency.equals(\"PLN\")) throw new UnsupportedOperationException(\"No rates for \" + currency + \" yet\");\n        return BigDecimal.ONE;\n    }\n}\n\nenum GatewayKind { CARD_OPERATOR } // \"we will add more providers later\"\n\ninterface PaymentGateway { String charge(String orderId, Money amount); }\n\nrecord CardOperatorGateway(ExchangeRates rates) implements PaymentGateway {\n    public String charge(String orderId, Money amount) {\n        var pln = amount.amount().multiply(rates.toPln(amount.currency()));\n        return \"Charged \" + pln + \" PLN for order \" + orderId; // the real HTTP call goes here\n    }\n}\n\nrecord PaymentGatewayFactory(ExchangeRates rates) {\n    PaymentGateway create(GatewayKind kind) {\n        return switch (kind) {\n            case CARD_OPERATOR -> new CardOperatorGateway(rates);\n        };\n    }\n}"}]},{"type":"codeBlock","attrs":{"language":"go","filename":"payments.go"},"content":[{"type":"text","text":"package main\n\nimport \"fmt\"\n\n// Before: built \"for the future\" – one operator, one currency, three extension points.\ntype Money struct {\n\tAmount   int64 // minor units\n\tCurrency string\n}\n\ntype ExchangeRates interface{ ToPLN(currency string) (int64, error) }\n\ntype PLNOnlyRates struct{}\n\n// The only currency we have seen in two years.\nfunc (PLNOnlyRates) ToPLN(currency string) (int64, error) {\n\tif currency != \"PLN\" {\n\t\treturn 0, fmt.Errorf(\"no rates for %s yet\", currency)\n\t}\n\treturn 1, nil\n}\n\ntype GatewayKind int\n\nconst CardOperator GatewayKind = iota // \"we will add more providers later\"\n\ntype PaymentGateway interface {\n\tCharge(orderID string, amount Money) (string, error)\n}\n\ntype CardOperatorGateway struct{ rates ExchangeRates }\n\nfunc (g CardOperatorGateway) Charge(orderID string, amount Money) (string, error) {\n\trate, err := g.rates.ToPLN(amount.Currency)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tgrosze := amount.Amount * rate // the real HTTP call goes here\n\treturn fmt.Sprintf(\"Charged %d.%02d PLN for order %s\", grosze/100, grosze%100, orderID), nil\n}\n\nfunc NewGateway(kind GatewayKind, rates ExchangeRates) (PaymentGateway, error) {\n\tswitch kind {\n\tcase CardOperator:\n\t\treturn CardOperatorGateway{rates: rates}, nil\n\tdefault:\n\t\treturn nil, fmt.Errorf(\"unknown gateway %d\", kind)\n\t}\n}\n\nfunc main() {\n\tgateway, _ := NewGateway(CardOperator, PLNOnlyRates{})\n\tfmt.Println(gateway.Charge(\"A-1042\", Money{Amount: 4590, Currency: \"PLN\"}))\n}"}]},{"type":"codeBlock","attrs":{"language":"python","filename":"payments.py"},"content":[{"type":"text","text":"from dataclasses import dataclass\nfrom decimal import Decimal\nfrom enum import Enum\nfrom typing import Protocol\n\n\n# Before: built \"for the future\" – one operator, one currency, three extension points.\n@dataclass(frozen=True)\nclass Money:\n    amount: Decimal\n    currency: str\n\n\nclass ExchangeRates(Protocol):\n    def to_pln(self, currency: str) -> Decimal: ...\n\n\nclass PlnOnlyRates:\n    def to_pln(self, currency: str) -> Decimal:\n        if currency != \"PLN\":  # the only currency we have seen in two years\n            raise NotImplementedError(f\"No rates for {currency} yet\")\n        return Decimal(\"1\")\n\n\nclass GatewayKind(Enum):\n    CARD_OPERATOR = \"card-operator\"  # \"we will add more providers later\"\n\n\nclass PaymentGateway(Protocol):\n    def charge(self, order_id: str, amount: Money) -> str: ...\n\n\nclass CardOperatorGateway:\n    def __init__(self, rates: ExchangeRates) -> None:\n        self._rates = rates\n\n    def charge(self, order_id: str, amount: Money) -> str:\n        pln = amount.amount * self._rates.to_pln(amount.currency)\n        return f\"Charged {pln} PLN for order {order_id}\"  # the real HTTP call goes here\n\n\nclass PaymentGatewayFactory:\n    def __init__(self, rates: ExchangeRates) -> None:\n        self._rates = rates\n\n    def create(self, kind: GatewayKind) -> PaymentGateway:\n        match kind:\n            case GatewayKind.CARD_OPERATOR:\n                return CardOperatorGateway(self._rates)\n        raise ValueError(f\"Unknown gateway {kind}\")\n\n\ngateway = PaymentGatewayFactory(PlnOnlyRates()).create(GatewayKind.CARD_OPERATOR)\nprint(gateway.charge(\"A-1042\", Money(Decimal(\"45.90\"), \"PLN\")))"}]}]}]},"toc":[{"id":"where-yagni-comes-from","text":"Where YAGNI comes from","level":2,"locked":false},{"id":"the-problem-a-shop-built-for-a-future-that-never-came","text":"The problem: a shop built for a future that never came","level":2,"locked":false},{"id":"what-building-ahead-costs","text":"What building ahead costs","level":2,"locked":true},{"id":"the-solution-remove-the-speculative-generality","text":"The solution: remove the speculative generality","level":2,"locked":true},{"id":"yagni-vs-extension-points","text":"YAGNI vs extension points","level":2,"locked":true},{"id":"feature-flags","text":"Feature flags","level":2,"locked":true},{"id":"examples-of-premature-abstraction","text":"Examples of premature abstraction","level":2,"locked":true},{"id":"when-to-use-it-and-when-not-to","text":"When to use it and when not to","level":2,"locked":true},{"id":"in-practice","text":"In practice","level":2,"locked":true},{"id":"related-topics","text":"Related topics","level":2,"locked":true},{"id":"test-yourself","text":"Test yourself","level":2,"locked":true}],"locked":true,"paywall":null,"previewRatio":0.3,"totalBlocks":51,"visibleBlocks":10,"resources":[{"id":"6b0857b53c96f2d34d0b38fd","type":"article","provider":"web","url":"https://martinfowler.com/bliki/Yagni.html","embedUrl":null,"title":"Yagni","description":"Yagni (\"You Aren't Gonna Need It\") is the principle that we should not build presumptive features. It should not be used as a justification for neglecting internal quality.","author":"martinfowler.com","thumbnailUrl":"https://martinfowler.com/bliki/images/yagni/sketch.png","durationSec":null,"lang":"en","topics":["yagni"],"techs":["general"],"tags":["hacker-news"],"price":"free","featured":false,"featuredOrder":0,"packageName":null,"source":"hn","publishedAt":"2015-05-26T16:17:12.000Z","popularity":70,"verifiedAt":"2026-11-26T11:07:40.000Z","curated":false,"addedBy":"6b0857a15a2625e44ecaabe6","createdAt":"2026-11-26T16:00:52.228Z","updatedAt":"2026-11-26T16:00:52.228Z"},{"id":"6b0857b53c96f2d34d0b3a38","type":"article","provider":"web","url":"https://newsletter.kentbeck.com/p/the-cost-yagni-was-never-about","embedUrl":null,"title":"The cost YAGNI was never about","description":"If you think YAGNI is about saving effort, cheap generation should retire it. It doesn't. Here's why.","author":"Kent Beck","thumbnailUrl":"https://substackcdn.com/image/fetch/$s_!glPN!,f_auto,q_auto:best,fl_progressive:steep/https%3A%2F%2Ftidyfirst.substack.com%2Ftwitter%2Fsubscribe-card.jpg%3Fv%3D1077263058%26version%3D9","durationSec":null,"lang":"en","topics":["yagni"],"techs":["general"],"tags":["hacker-news"],"price":"free","featured":false,"featuredOrder":0,"packageName":null,"source":"hn","publishedAt":"2026-06-28T18:31:54.000Z","popularity":61,"verifiedAt":"2026-11-26T11:07:40.000Z","curated":false,"addedBy":"6b0857a15a2625e44ecaabe6","createdAt":"2026-11-26T16:00:52.228Z","updatedAt":"2026-11-26T16:00:52.228Z"},{"id":"6b0857b53c96f2d34d0b38da","type":"article","provider":"web","url":"https://lukeplant.me.uk/blog/posts/yagni-exceptions","embedUrl":null,"title":"Yagni Exceptions (2021)","description":"A list of some exceptions to the principle of “You Ain't Gonna Need It” – that is, times when doing a bit more up front usually pays off.","author":"Luke Plant","thumbnailUrl":null,"durationSec":null,"lang":"en","topics":["yagni"],"techs":["general"],"tags":["hacker-news"],"price":"free","featured":false,"featuredOrder":0,"packageName":null,"source":"hn","publishedAt":"2022-10-17T06:03:48.000Z","popularity":71,"verifiedAt":"2026-11-26T11:07:40.000Z","curated":false,"addedBy":"6b0857a15a2625e44ecaabe6","createdAt":"2026-11-26T16:00:52.228Z","updatedAt":"2026-11-26T16:00:52.228Z"},{"id":"6b0857c33c96f2d34d0b4670","type":"book","provider":"openlibrary","url":"https://openlibrary.org/works/OL1961693W","embedUrl":null,"title":"Extreme programming explained","description":"","author":"Kent Beck, Cynthia Andres","thumbnailUrl":"https://covers.openlibrary.org/b/id/5394766-L.jpg","durationSec":null,"lang":"en","topics":[],"techs":["general"],"tags":["book"],"price":"paid","featured":false,"featuredOrder":0,"packageName":null,"source":"openlibrary","publishedAt":"1999-01-01T00:00:00.000Z","popularity":59,"verifiedAt":"2026-11-26T11:00:08.000Z","curated":false,"addedBy":"6b0857a15a2625e44ecaabe6","createdAt":"2026-11-26T16:01:04.995Z","updatedAt":"2026-11-26T16:01:04.995Z"},{"id":"6b0857be3c96f2d34d0b420c","type":"article","provider":"web","url":"https://martinfowler.com/articles/feature-toggles.html","embedUrl":null,"title":"Feature Toggles (aka Feature Flags)","description":"Feature Flags can be categorized into several buckets; manage each appropriately. Smart implementation can help constrain complexity.","author":"Martin Fowler","thumbnailUrl":"https://martinfowler.com/articles/feature-toggles/overview-diagram.png","durationSec":null,"lang":"en","topics":[],"techs":["general"],"tags":["martin-fowler"],"price":"free","featured":false,"featuredOrder":0,"packageName":null,"source":"manual","publishedAt":null,"popularity":75,"verifiedAt":"2026-11-26T11:07:20.000Z","curated":false,"addedBy":"6b0857a15a2625e44ecaabe6","createdAt":"2026-11-26T16:00:59.965Z","updatedAt":"2026-11-26T16:00:59.965Z"},{"id":"6b0857c63c96f2d34d0b48da","type":"video","provider":"youtube","url":"https://www.youtube.com/watch?v=flxmpq7_tdM","embedUrl":"https://www.youtube-nocookie.com/embed/flxmpq7_tdM","title":"How To Avoid Designing A Big Ball Of Mud (YAGNI)","description":"What is good design and how can we tell the difference between that and bad software design? Extreme Programming introduced some key ideas about software design, that often get overlooked. YAGNI, “You Ain’t Gonna Need It”, and an evolutionary design approach that Kent Beck referred to as \"design…","author":"Modern Software Engineering","thumbnailUrl":"https://i.ytimg.com/vi/flxmpq7_tdM/hqdefault.jpg","durationSec":null,"lang":"en","topics":["yagni"],"techs":["general"],"tags":["refactoring","legacy-code"],"price":"free","featured":false,"featuredOrder":0,"packageName":null,"source":"youtube-rss","publishedAt":"2022-09-21T18:00:15.000Z","popularity":68,"verifiedAt":"2026-11-26T11:13:34.000Z","curated":false,"addedBy":"6b0857a15a2625e44ecaabe6","createdAt":"2026-11-26T16:01:10.095Z","updatedAt":"2026-11-26T16:01:10.095Z"}],"challenges":[],"attachments":[],"hasCode":true,"repoUrl":null,"series":[{"slug":"design-principles","title":"Design principles","items":[{"slug":"kiss","title":"KISS: the simplest working solution usually wins","current":false},{"slug":"dry","title":"DRY: one piece of knowledge, one place – and when duplication is cheaper","current":false},{"slug":"yagni","title":"YAGNI: don't build ahead – what a feature nobody ordered really costs","current":true},{"slug":"law-of-demeter","title":"The Law of Demeter: talk only to your friends","current":false},{"slug":"solid","title":"SOLID: five object-oriented design principles without the myths","current":false}]}],"prev":{"slug":"dry","title":"DRY: one piece of knowledge, one place – and when duplication is cheaper"},"next":{"slug":"law-of-demeter","title":"The Law of Demeter: talk only to your friends"},"featured":false,"authorCard":{"id":"6b08bd2e80884893657cc8cf","name":"Redakcja Art of Devs","avatar":null,"handle":"redakcja","headline":"Wzorce projektowe · Architektura · AI i LLM","community":false,"reputation":{"points":3900,"level":{"key":"mentor","label":"Mentor"}},"articles":104,"skills":[{"key":"typescript","title":"TypeScript","level":5,"levelLabel":"Authority","articles":90},{"key":"dotnet","title":".NET","level":5,"levelLabel":"Authority","articles":61},{"key":"python","title":"Python","level":5,"levelLabel":"Authority","articles":59},{"key":"java","title":"Java","level":5,"levelLabel":"Authority","articles":46}]}}