Martin Fowler—Bliki: Yagni ("You Aren't Gonna Need It")
Join the DZone community and get the full member experience.Join For Free
yagni originally is an acronym that stands for "you aren't gonna need it". it is a mantra from extremeprogramming that's often used generally in agile software teams. it's a statement that some capability we presume our software needs in the future should not be built now because "you aren't gonna need it".
yagni is a way to refer to the xp practice of simple design (from the first edition of
the white book
the second edition refers to the related notion of "incremental design"). like many elements of xp, it's a sharp contrast to elements of the widely held principles of software engineering in the late 90s. at that time there was a big push for careful up-front planning of software development.
let's imagine i'm working with a startup in minas tirith selling insurance for the shipping business. their software system is broken into two main components: one for pricing, and one for sales. the dependencies are such that they can't usefully build sales software until the relevant pricing software is completed.
at the moment, the team is working on updating the pricing component to add support for risks from storms. they know that in six months time, they will need to also support pricing for piracy risks. since they are currently working on the pricing engine they consider building the presumptive feature for piracy pricing now, since that way the pricing service will be complete before they start working on the sales software.
yagni argues against this, it says that since you won't need piracy pricing for six months you shouldn't build it until it's necessary. so if you think it will take two months to build this software, then you shouldn't start for another four months (neglecting any buffer time for schedule risk and updating the sales component).
the first argument for yagni is that while we may now think we need this presumptive feature, it's likely that we will be wrong. after all the context of agile methods is an acceptance that we welcome changing requirements. a plan-driven requirements guru might counter argue that this is because we didn't do a good-enough job of our requirements analysis, we should have put more time and effort into it. i counter that by pointing out how difficult and costly it is to figure out your needs in advance, but even if you can, you can still be blind-sided when the gondor navy wipes out the pirates, thus undermining the entire business model.
in this case, there's an obvious cost of the presumptive feature - the cost of build : all the effort spent on analyzing, programming, and testing this now useless feature.
but let's consider that we were completely correct with our understanding of our needs, and the gondor navy didn't wipe out the pirates. even in this happy case, building the presumptive feature incurs two serious costs. the first cost is the cost of delayed value. by expending our effort on the piracy pricing software we didn't build some other feature. if we'd instead put our energy into building the sales software for weather risks, we could have put a full storm risks feature into production and be generating revenue two months earlier. this cost of delay due to the presumptive feature is two months revenue from storm insurance.
the common reason why people build presumptive features is because they think it will be cheaper to build it now rather than build it later. but that cost comparison has to be made at least against the cost of delay, preferably factoring in the probability that you're building an unnecessary feature, for which your odds are at least ⅔.
often people don't think through the comparative cost of building now to building later. one approach i use when mentoring developers in this situation is to ask them to imagine any refactoring they would have to do later to introduce the capability when it's needed. often that thought experiment is enough to convince them that it won't be significantly more expensive to add it later. another result from such an imagining is to add something that's easy to do now, adds minimal complexity, yet significantly reduces the later cost. using lookup tables for error messages rather than inline literals are an example that are simple yet make later translations easier to support.
reminder, any extensibility point that’s never used isn’t just wasted effort, it’s likely to also get in your way as well -- jeremy miller
the cost of delay is one cost that a successful presumptive feature imposes, but another is the cost of carry . the code for the presumptive feature adds some complexity to the software, this complexity makes it harder to modify and debug that software, thus increasing the cost of other features. the extra complexity from having the piracy-pricing feature in the software might add a couple of weeks to how long it takes to build the storm insurance sales component. that two weeks hits two ways: the additional cost to build the feature, plus the additional cost of delay since it look longer to put it into production. we'll incur a cost of carry on every feature built between now and the time the piracy insurance software starts being useful. should we never need the piracy-pricing software, we'll incur a cost of carry on every feature built until we remove the piracy-pricing feature (assuming we do), together with the cost of removing it.
so far i've divided presumptive features in two categories: successful and unsuccessful. naturally there's really a spectrum there, and with one point on that spectrum that's worth highlighting: the right feature built wrong. development teams are always learning, both about their users and about their code base. they learn about the tools they're using and these tools go through regular upgrades. they also learn about how their code works together. all this means that you often realize that a feature coded six months ago wasn't done the way you now realize it should be done. in that case you have accumulated technicaldebt and have to consider the cost of repair for that feature or the on-going costs of working around its difficulties.
so we end up with three classes of presumptive features, and four kinds of costs that occur when you neglect yagni for them.
my insurance example talks about relatively user-visible functionality, but the same argument applies for abstractions to support future flexibility. when building the storm risk calculator, you may consider putting in abstractions and parameterizations now to support piracy and other risks later. yagni says not to do this, because you may not need the other pricing functions, or if you do your current ideas of what abstractions you'll need will not match what you learn when you do actually need them. this doesn't mean to forego all abstractions, but it does mean any abstraction that makes it harder to understand the code for current requirements is presumed guilty.
yagni is at its most visible with larger features, but you see it more frequently with small things. recently i wrote some code that allows me to highlight part of a line of code. for this, i allow the highlighted code to be specified using a regular expression. one problem i see with this is that since the whole regular expression is highlighted, i'm unable to deal with the case where i need the regex to match a larger section than what i'd like to highlight. i expect i can solve that by using a group within the regex and letting my code only highlight the group if a group is present. but i haven't needed to use a regex that matches more than what i'm highlighting yet, so i haven't extended my highlighting code to handle this case - and won't until i actually need it. for similar reasons i don't add fields or methods until i'm actually ready to use them.
small yagni decisions like this fly under the radar of project planning. as a developer it's easy to spend an hour adding an abstraction that we're sure will soon be needed. yet all the arguments above still apply, and a lot of small yagni decisions add up to significant reductions in complexity to a code base, while speeding up delivery of features that are needed more urgently.
now we understand why yagni is important we can dig into a common confusion about yagni. yagni only applies to capabilities built into the software to support a presumptive feature, it does not apply to effort to make the software easier to modify. yagni is only a viable strategy if the code is easy to change, so expending effort on refactoring isn't a violation of yagni because refactoring makes the code more malleable. similar reasoning applies for practices like selftestingcode and continuousdelivery . these are enabling practices for evolutionary design , without them yagni turns from a beneficial practice into a curse. but if you do have a malleable code base, then yagni reinforces that flexibility. yagni has the curious property that it is both enabled by and enables evolutionary design.
yagni is not a justification for neglecting the health of your code base. yagni requires (and enables) malleable code.
i also argue that yagni only applies when you introduce extra complexity now that you won't take advantage of until later. if you do something for a future need that doesn't actually increase the complexity of the software, then there's no reason to invoke yagni.
having said all this, there are times when applying yagni does cause a problem, and you are faced with an expensive change when an earlier change would have been much cheaper. the tricky thing here is that these cases are hard to spot in advance, and much easier to remember than the cases where yagni saved effort . my sense is that yagni-failures are relatively rare and their costs are easily outweighed by when yagni succeeds.
Published at DZone with permission of Martin Fowler, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.