
YAGNI Explained: The Real Cost of Designing for Imagined Requirements
- كاتب موثّق
محتويات المقال
INotificationChannel interface, a ChannelRegistry, a channel name in configuration and one EmailChannel, because two years ago someone said "we'll add SMS and push later". There is still only email. Then product asks for something nobody predicted: a daily digest instead of one email per event. The design assumed the thing that would vary was the channel. What actually varies is timing, and a SendAsync call per event is in the way. Now the team has to work around the extension point before it can build the feature.
The common misconception
What actually happens
YAGNI targets presumptive features
Four costs of building ahead
Cost of build: the analysis, code and tests for something that may never be used. Cost of delay: the feature you didn't ship while you built the presumptive one. This one applies even when the guess is right. Cost of carry: the extra complexity makes every other change harder to read, modify and debug, until the feature is used or removed. Cost of repair: the right feature built wrong, because by the time it's needed you know more than you did when you wrote it.
Abstractions are presumptive too
What YAGNI doesn't say
The fix: build for today, keep it easy to change
// Built for "SMS and push later". Two years on, email is the only channel.public interface INotificationChannel{ string Name { get; } Task SendAsync(Notification n, CancellationToken ct);}public sealed class ChannelRegistry(IEnumerable<INotificationChannel> all){ public INotificationChannel Get(string name) => all.Single(c => c.Name == name);}public sealed class Notifier(ChannelRegistry registry, string channelName){ public Task NotifyAsync(Notification n, CancellationToken ct) => registry.Get(channelName).SendAsync(n, ct);}public sealed class EmailChannel(IEmailSender email) : INotificationChannel{ public string Name => "email"; public Task SendAsync(Notification n, CancellationToken ct) => email.SendAsync(n.To, n.Subject, n.Body, ct);}// Today's requirement: email a notification. Nothing more.public sealed class Notifier(IEmailSender email){ public Task NotifyAsync(Notification n, CancellationToken ct) => email.SendAsync(n.To, n.Subject, n.Body, ct);}// When a second channel is real, extract an interface here, with tests// in place, knowing what that channel and its users actually need.IEmailSender interface: the email provider is an external system and the tests need to replace it today, so that abstraction serves a current need. What's gone is the machinery for channels that don't exist, and with it the three hops a reader had to follow to learn that Notifier sends an email. When the digest request arrives, the change happens in one small class that says exactly what it does. If SMS ever arrives, the refactoring is an extracted interface and a new class, and it's designed around the real requirement: per-user preferences, fallbacks, or whatever product actually asks for.
How to apply it in your project
List extension points with one implementation Interfaces with a single implementation that tests don't replace, registries, factories and strategy maps with one entry, configuration switches nobody changes. Ask what each one costs today How much harder does it make the current code to read and change? Which features did it slow down? Has the predicted variation arrived? Inline the ones that don't pay Collapse the indirection into the concrete code, with tests protecting the behavior. Keep abstractions that serve a current need, like replacing an external system in tests. Spend the savings on changeability Put the time into tests, refactoring and a fast deployment pipeline, so that when a real requirement arrives, adding the extension point is an easy change.
Key takeaways
YAGNI says to build capabilities, and the abstractions behind them, when a real requirement needs them. Building ahead has four costs: build, delay, carry and repair; the delay and the carry apply even when the guess is right. Speculative abstractions often bet on the wrong axis of change and get in the way of the change that really comes. YAGNI doesn't apply to refactoring, tests or delivery practices; it depends on them. Cheap foresight that adds no real complexity is fine; complexity for an imagined future is presumed guilty.
التعليقات (0)