← 返回规律列表
设计

KISS:保持简单

设计和系统应当尽可能简单,但不能简单到失去必要能力。

显示原始英文内容

KISS (Keep It Simple, Stupid)

Designs and systems should be as simple as possible.

Takeaways

  • KISS primarily refers to avoiding complexity in solution designs. A solution that satisfies the requirement and is simple in design is better than a complex one.
  • Simple code is easier and faster to understand and debug. It's even easier to understand and fix if you're your own future self. Smarter code may look impressive at first, but it often masks problems.
  • The code solution should be as simple as possible. Anything that adds complexity unnecessarily is counter to the KISS Principle.

Overview

The KISS principle is a reminder that simplicity should be a key goal. If you can solve a problem with a 50-line script versus a complex 500-line solution, KISS favors the 50-line solution because each line of code has the potential to cause an error.

Why is simplicity so important? Software has to be understood by humans. A simple design is much easier to maintain: new team members can get up to speed faster, bugs are easier to localize, and modifications cause fewer ripple effects.

The KISS principle encourages developers to resist "clever" code that does too much at once, and to avoid architecting solutions that address future problems at the cost of current complexity.

Examples

A web application needs to generate reports. A KISS approach would use a simple script or existing library to query the database and output a CSV. A non-KISS approach would design a complete plug-in architecture "just in case" you need it more generically later. If requirements are small, the latter is overkill.

When writing a function to parse a file, a KISS implementation uses simple logic: open the file, read line by line, split fields, handle errors. A complex approach might implement a fully generic parser-combinator framework. Unless you truly need that generality, the simpler approach will be easier to get right and maintain.

Many experienced engineers advise: "First make it work, then make it right, then make it fast (if needed)."

Origins

The phrase "Keep It Simple, Stupid" came from the U.S. military. It's attributed to Kelly Johnson, a lead engineer at Lockheed's Skunk Works in the 1960s. Johnson challenged his team: design an aircraft that could be repaired with basic tools by an average mechanic in the field.

Influential programmers such as Edsger Dijkstra and C.A.R. Hoare often discussed complexity. Hoare said, "There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult."

The KISS principle has since become a cornerstone of software engineering best practices.

核心含义

简单意味着更少的概念、更清晰的边界、更少的特殊情况和更容易验证的行为。简单设计降低学习、测试、调试和变更成本。

KISS 不等于追求最短代码,也不等于忽略领域复杂度。真正的目标是不要在问题之外额外引入复杂度。

实践例子

一个内部工具只需定时执行几项任务,却被设计成分布式插件平台。使用标准定时器和清晰配置,可能比引入多层抽象更合适。

来源与边界

KISS 是工程和设计领域常见的简洁原则。复杂度如果来自真实约束,就应该被表达和管理,而不是用口号强行删除。