liveBook · Manning

YAGNI principle

As explained in Unit Testing Principles, Practices, and Patterns

YAGNI is the principle of avoiding implementation of functionality until it is actually needed.

YAGNI—“You aren’t gonna need it”—means postponing functionality and structural changes until a real need exists. Avoid speculative interfaces, features, and flexibility. section 8.4

Speculative work carries opportunity cost: requirements may change before the feature is needed, forcing rework while increasing code and ownership costs. Simpler, smaller solutions usually offer better returns. section 8.4

Apply the principle with these rules:

  • Do not add an interface merely to enable an imagined future implementation; a single implementation is not a genuine abstraction. section 8.4
  • Implement the required behavior directly, then discover and extract abstractions when multiple implementations create a real need. section 8.4
public interface IMessageBus
public class MessageBus : IMessageBus

public interface IUserRepository
public class UserRepository : IUserRepository

Compare interfaces with their sole implementations to spot structure introduced only for hypothetical flexibility. section 8.4

How Unit Testing Principles, Practices, and Patterns covers it

How deep it goes
Explains the principle through opportunity cost, changing requirements, ownership cost, and the limits of single-implementation interfaces.
Examples
Uses single-implementation interfaces and hypothetical future functionality as examples of premature design.
What you can do after
Evaluate proposed abstractions and defer code whose need has not yet emerged.
How it connects
Connects YAGNI with interfaces, abstractions, loose coupling, and the Open-Closed principle.

Also explained in

Compare how 3 books explain YAGNI principle →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Unit Testing Principles, Practices, and Patterns. The section links point to the parts of the book it draws on.