keep-it-simple
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseKISS (Keep It Simple, Stupid)
KISS(保持简单,别搞复杂)
Overview
概述
The simplest solution that works is the best solution.
Clever code impresses no one. Simple code ships faster, breaks less, and others can maintain it.
最简单且有效的解决方案就是最佳方案。
炫技代码不会打动任何人。简洁的代码交付更快、故障更少,且便于他人维护。
When to Use
适用场景
- Solution feels complex
- Writing "elegant" or "clever" code
- Multiple approaches exist
- Tempted to show off skills
- Using advanced patterns for simple problems
- 解决方案看起来复杂
- 想要编写“优雅”或“炫技”的代码
- 存在多种实现方式
- 忍不住想炫耀技能
- 为简单问题使用高级模式
The Iron Rule
铁则
NEVER choose clever over clear. Simple wins.No exceptions:
- Not for "it's more elegant"
- Not for "it shows advanced skills"
- Not for "it's technically better"
- Not for "it's a nice one-liner"
NEVER choose clever over clear. Simple wins.无例外:
- 不要因为“更优雅”而选择炫技
- 不要因为“能展示高级技能”而选择炫技
- 不要因为“技术上更优”而选择炫技
- 不要因为“是漂亮的单行代码”而选择炫技
Detection: The "Clever" Smell
识别:“炫技”信号
If you're proud of how clever your code is, simplify it:
typescript
// ❌ VIOLATION: Clever one-liner
const isPalindrome = (s: string): boolean =>
(s = s.toLowerCase().replace(/[^a-z0-9]/g, '')) === [...s].reverse().join('');
// ✅ CORRECT: Simple and clear
function isPalindrome(str: string): boolean {
const cleaned = str.toLowerCase().replace(/[^a-z0-9]/g, '');
for (let i = 0; i < cleaned.length / 2; i++) {
if (cleaned[i] !== cleaned[cleaned.length - 1 - i]) {
return false;
}
}
return true;
}如果你为自己代码的“炫技感”感到自豪,那就简化它:
typescript
// ❌ 违规:炫技单行代码
const isPalindrome = (s: string): boolean =>
(s = s.toLowerCase().replace(/[^a-z0-9]/g, '')) === [...s].reverse().join('');
// ✅ 正确:简洁清晰
function isPalindrome(str: string): boolean {
const cleaned = str.toLowerCase().replace(/[^a-z0-9]/g, '');
for (let i = 0; i < cleaned.length / 2; i++) {
if (cleaned[i] !== cleaned[cleaned.length - 1 - i]) {
return false;
}
}
return true;
}The Cost of Complexity
复杂度的代价
| Complex Code | Impact |
|---|---|
| Clever one-liners | Unreadable, hard to debug |
| Nested ternaries | Logic becomes opaque |
| Over-abstraction | Indirection hides intent |
| Premature optimization | Complexity without measured benefit |
| Design pattern overuse | Patterns for patterns' sake |
| 复杂代码 | 影响 |
|---|---|
| 炫技单行代码 | 难以阅读、难以调试 |
| 嵌套三元表达式 | 逻辑变得模糊 |
| 过度抽象 | 间接性掩盖了真实意图 |
| 过早优化 | 无实测收益的复杂度 |
| 过度使用设计模式 | 为模式而模式 |
Correct Pattern: Obvious Code
正确模式:直白代码
Write code that a junior developer can understand in 30 seconds:
typescript
// ❌ COMPLEX: "Elegant" functional chain
const result = data
.filter(Boolean)
.map(x => transform(x))
.reduce((acc, x) => ({ ...acc, [x.id]: x }), {})
.values()
.filter(x => x.active)
.sort((a, b) => b.date - a.date)
.slice(0, 10);
// ✅ SIMPLE: Clear steps with names
const validItems = data.filter(Boolean);
const transformed = validItems.map(transform);
const activeItems = transformed.filter(item => item.active);
const sorted = activeItems.sort((a, b) => b.date - a.date);
const topTen = sorted.slice(0, 10);编写能让初级开发者在30秒内理解的代码:
typescript
// ❌ 复杂:“优雅”的链式调用
const result = data
.filter(Boolean)
.map(x => transform(x))
.reduce((acc, x) => ({ ...acc, [x.id]: x }), {})
.values()
.filter(x => x.active)
.sort((a, b) => b.date - a.date)
.slice(0, 10);
// ✅ 简洁:带命名的清晰步骤
const validItems = data.filter(Boolean);
const transformed = validItems.map(transform);
const activeItems = transformed.filter(item => item.active);
const sorted = activeItems.sort((a, b) => b.date - a.date);
const topTen = sorted.slice(0, 10);Pressure Resistance Protocol
应对外界压力的准则
1. "It's More Elegant"
1. “它更优雅”
Pressure: "This one-liner is more elegant than the verbose version"
Response: Elegance is clarity, not brevity. Simple code is more elegant than clever code.
Action: Use the clear version. Name intermediate variables.
压力:“这个单行代码比冗长的版本更优雅”
**回应:**优雅是清晰,不是简洁。简洁的代码比炫技代码更优雅。
**行动:**使用清晰的版本,为中间变量命名。
2. "It Shows Advanced Skills"
2. “它能展示高级技能”
Pressure: "I want to demonstrate I know advanced patterns"
Response: Senior engineers are recognized for simple solutions, not complex ones.
Action: Solve the problem simply. Save cleverness for where it's needed.
压力:“我想展示我懂高级模式”
**回应:**资深工程师因简洁的解决方案而非复杂方案得到认可。
**行动:**用简单方式解决问题。只在必要时使用炫技手法。
3. "It's Technically Better"
3. “它在技术上更优”
Pressure: "The complex version is O(n) vs O(n log n)"
Response: Premature optimization. Is this actually a bottleneck?
Action: Write simple code. Optimize only when profiling shows need.
压力:“复杂版本的时间复杂度是O(n),而另一个是O(n log n)”
**回应:**过早优化。这真的是性能瓶颈吗?
**行动:**编写简洁代码。仅当性能分析显示需要时再优化。
4. "Let Me Show Multiple Approaches"
4. “我来展示多种实现方式”
Pressure: "I'll provide two implementations to show versatility"
Response: One clear solution is better than multiple options.
Action: Pick the simplest approach. Provide only that.
压力:“我会提供两种实现来展示多面性”
**回应:**一个清晰的解决方案比多个选项更好。
**行动:**选择最简单的实现方式,只提供这一种。
Red Flags - STOP and Reconsider
危险信号 - 立即停止并重新考虑
If you notice ANY of these, simplify:
- Code requires explanation comments
- Nested ternaries
? : ? : - Multiple chained operations (5+)
- Regex that needs decoding
- Proud of how clever it is
- "One-liner" implementations
- Junior couldn't understand in 30 seconds
- Multiple solutions "for versatility"
All of these mean: Rewrite simply.
如果你注意到以下任何一种情况,请简化代码:
- 代码需要注释来解释
- 嵌套三元表达式
? : ? : - 多个链式操作(5个及以上)
- 需要“解码”的正则表达式
- 为代码的炫技感感到自豪
- “单行代码”实现
- 初级开发者无法在30秒内理解
- 提供多种解决方案“以展示多面性”
以上所有情况都意味着:重写为简洁版本。
Simplification Techniques
简化技巧
| Complex | Simple |
|---|---|
| Nested ternaries | if/else statements |
| Long chains | Named intermediate variables |
| Clever regex | Multiple simple checks |
| One-liner | Multi-line with comments |
| Implicit | Explicit |
| Magic numbers | Named constants |
| 复杂写法 | 简洁写法 |
|---|---|
| 嵌套三元表达式 | if/else语句 |
| 长链式调用 | 命名中间变量 |
| 炫技正则 | 多个简单检查 |
| 单行代码 | 多行加注释 |
| 隐式逻辑 | 显式逻辑 |
| 魔法数字 | 命名常量 |
Quick Reference
快速参考
| Symptom | Action |
|---|---|
| "Elegant" one-liner | Expand to clear multi-line |
| Nested ternary | Convert to if/else |
| Complex chain | Break into named steps |
| Multiple approaches | Pick simplest one |
| Pride in cleverness | Rewrite simply |
| 症状 | 行动 |
|---|---|
| “优雅”的单行代码 | 扩展为清晰的多行代码 |
| 嵌套三元表达式 | 转换为if/else |
| 复杂链式调用 | 拆分为带命名的步骤 |
| 多种实现方式 | 选择最简单的一种 |
| 为炫技感自豪 | 重写为简洁版本 |
Common Rationalizations (All Invalid)
常见合理化借口(全无效)
| Excuse | Reality |
|---|---|
| "It's more elegant" | Clear code is more elegant than clever code. |
| "Shows advanced skills" | Simple solutions show more skill. |
| "It's a nice one-liner" | One-liners are often unreadable. |
| "Technically better" | Premature optimization is bad. |
| "Multiple options show versatility" | One clear solution is better. |
| "It's how experts do it" | Experts write simple code. |
| 借口 | 真相 |
|---|---|
| “它更优雅” | 清晰的代码比炫技代码更优雅。 |
| “展示高级技能” | 简洁的解决方案更能体现技能。 |
| “它是漂亮的单行代码” | 单行代码通常难以阅读。 |
| “技术上更优” | 过早优化是不良实践。 |
| “多种选项展示多面性” | 一个清晰的解决方案更好。 |
| “专家就是这么做的” | 专家编写简洁的代码。 |
The Bottom Line
核心结论
Simple beats clever. Clear beats concise. Obvious beats elegant.
When solving a problem: find the simplest solution that works. If a junior dev can't understand it in 30 seconds, simplify it.
简洁胜于炫技。清晰胜于简洁。直白胜于优雅。
解决问题时:找到最简单且有效的方案。如果初级开发者无法在30秒内理解,那就简化它。