← 返回规律列表
设计

YAGNI:你不会需要它

不要为尚未出现的需求提前增加功能和抽象。

显示原始英文内容

YAGNI (You Aren't Gonna Need It)

Don't add functionality until it is necessary.

Takeaways

  • YAGNI is here to remind developers to focus on current tasks rather than implementing features that aren't needed now.
  • Anticipating future needs often leads to over-engineering, and this adds complexity and maintenance issues.
  • YAGNI encourages iterative development. You implement minimal solutions and refine or extend them when needed.

Overview

YAGNI captures a core philosophy of agile development: don't write code for features that haven't been requested or aren't immediately needed.

If you're implementing Module A and think "in the future we might need Module B to do X, so I'll code some hooks for that now," YAGNI advises against it because that future feature may never come or may change drastically.

This principle directly fights over-engineering. To apply YAGNI successfully, teams rely on confidence in refactoring. You defer a feature only if you trust you can add it later at low cost.

Agile methods provide that safety net via good test coverage, refactoring tools, and continuous integration. YAGNI pushes the problem of complexity to when it's actually needed.

Examples

You're building a feature with specific behavior. You think "maybe in the future someone will want to toggle this on/off, so I'll add a configuration flag now." YAGNI challenges that. Unless there's a current requirement for configurability, implement only expected behavior, nothing more.

You're working on a library function that currently does one thing. You realize it might be useful more generally, so you might add parameters or abstraction tiers. YAGNI says implement it for the original task.

Your app needs JSON export? Implement simple JSON, not a full serialization library supporting XML, YAML, etc.

Teams that adopt YAGNI are often comfortable refactoring because they trust that when the feature set grows, they'll have more information about real use cases, leading to better abstractions.

Origins

YAGNI was part of the Extreme Programming (XP) paradigm in the late 1990s. It was promoted by Ron Jeffries, one of XP's founders, who wrote: "Always implement things when you actually need them, not when you just foresee that you need them."

The slogan was adopted in XP literature, such as Extreme Programming Installed (2001).

核心含义

只实现当前已经明确、可验证的需求。提前加入配置开关、扩展点、通用参数或多种格式支持,未来不一定会用到,反而会增加测试、文档和维护成本。

YAGNI 并不是拒绝设计,而是把设计决策推迟到拥有更多真实信息的时候。良好的测试和持续集成让团队可以在需求出现时安全地扩展系统。

实践例子

应用当前只需要导出 JSON,就先实现简单的 JSON 导出,而不是同时搭建支持 XML、YAML 和插件体系的序列化框架。

来源与边界

YAGNI 是极限编程中的核心口号。对于安全、合规、数据迁移等无法临时补救的约束,应该提前设计;它主要针对没有证据支撑的功能性过度设计。