principletypescriptMajor
YAGNI: You Aren't Gonna Need It — Build What Is Required Now
Viewed 0 times
YAGNIyou aren't gonna need itover-engineeringspeculative abstractionsimplicitypremature optimization
Problem
Developers add generic abstractions, plugin systems, and configuration options for requirements that don't exist yet. The extra complexity makes the code harder to understand, maintain, and change, and the anticipated requirements often never materialize.
Solution
Implement only what is needed for the current requirements. When a requirement actually arrives, extend the code. The simplest code that works is always the right starting point.
// Premature generalization — YAGNI violation
class NotificationService {
constructor(
private channels: NotificationChannel[],
private templateEngine: TemplateEngine,
private priorityQueue: PriorityQueue<Notification>,
private rateLimiter: RateLimiter
) {}
// ... all built before any notification was sent
}
// YAGNI-compliant: send an email, that's it
async function sendWelcomeEmail(to: string, name: string): Promise<void> {
await mailer.send({
to,
subject: 'Welcome!',
body: `Hello ${name}, welcome to our service.`,
});
}
// Add abstractions when you have a second email type to sendWhy
Every abstraction has a cost: it must be understood, maintained, and worked around when the real requirement differs from the imagined one. YAGNI defers that cost until the benefit is concrete.
Gotchas
- YAGNI is not an excuse to write bad code. Clean, readable, non-duplicated code is always required.
- YAGNI and DRY can conflict: fixing a known duplication is not speculative. Building a plugin system for a second plugin that doesn't exist is.
- Security and privacy requirements often need to be built in early — YAGNI does not override compliance requirements.
Revisions (0)
No revisions yet.