| name | code-simplifier |
| description | 代码简化器,KISS原则执行者,Linus品味守护者 |
| trigger | 开发阶段自动加载 |
Code Simplifier Skill
"Simplicity is the ultimate sophistication." — Leonardo da Vinci
"代码越少,Bug越少。" — 工程真理
核心使命
在开发过程中主动识别和简化复杂代码,确保代码符合 Linus 的"品味"标准。
🎯 简化原则
1. 函数简化
function processUser(data) {
}
function processUser(data) {
const validated = validateUser(data);
const normalized = normalizeUser(validated);
return saveUser(normalized);
}
2. 条件简化
if (a) {
if (b) {
if (c) {
doSomething();
}
}
}
if (!a) return;
if (!b) return;
if (!c) return;
doSomething();
3. 循环简化
const result = [];
for (let i = 0; i < items.length; i++) {
if (items[i].active) {
result.push(items[i].name);
}
}
const result = items
.filter(item => item.active)
.map(item => item.name);
4. 类型简化
type DeepPartial<T> = T extends object
? { [P in keyof T]?: DeepPartial<T[P]> }
: T;
interface UserUpdate {
name?: string;
email?: string;
}
5. 抽象简化
abstract class AbstractRepositoryFactory<T extends BaseEntity> {
abstract createRepository(): IRepository<T>;
}
function getUsers(): User[] {
return db.query('SELECT * FROM users');
}
🔍 自动检测规则
开发时自动检测以下问题:
| 检测项 | 阈值 | 动作 |
|---|
| 函数行数 | >50行 | 建议拆分 |
| 嵌套深度 | >3层 | 建议重构 |
| 参数数量 | >4个 | 建议封装对象 |
| 圈复杂度 | >10 | 建议简化 |
| 重复代码 | >3处 | 建议提取 |
| 魔法数字 | 任意 | 建议常量 |
📋 简化检查清单
每次代码提交前检查:
Linus 品味清单
代码指标
🔄 简化流程
写代码 → 自检 → 发现复杂 → 简化 → 验证 → 提交
↓
记录到 Memory
(下次避免)
💾 记忆沉淀
发现的简化模式和反模式,记录到 Memory MCP:
memory.add({
category: "code_pattern",
content: "避免超过3层嵌套,使用早返回模式",
tags: ["simplification", "nesting"]
})
🛠️ 重构技巧
提取函数
if (user.age >= 18 && user.country === 'US' && user.verified) {
}
function canAccess(user: User): boolean {
return user.age >= 18 && user.country === 'US' && user.verified;
}
if (canAccess(user)) {
}
替换条件为多态
function getArea(shape) {
if (shape.type === 'circle') return Math.PI * shape.radius ** 2;
if (shape.type === 'square') return shape.side ** 2;
}
interface Shape { getArea(): number; }
class Circle implements Shape { getArea() { return Math.PI * this.radius ** 2; } }
class Square implements Shape { getArea() { return this.side ** 2; } }
使用Map替代Switch
switch (status) {
case 'pending': return '待处理';
case 'approved': return '已批准';
case 'rejected': return '已拒绝';
}
const STATUS_MAP = {
pending: '待处理',
approved: '已批准',
rejected: '已拒绝',
};
return STATUS_MAP[status];
⚠️ 不要过度简化
简化有度,以下情况保持原样:
- 性能关键路径 — 可读性让位于性能
- 已稳定的遗留代码 — 不破坏正常工作的代码
- 团队约定的模式 — 遵循项目规范
- 第三方库的用法 — 按文档来
📊 简化报告
每次开发完成后,输出简化报告:
## 代码简化报告
### 已简化
- `src/auth/login.ts`: 拆分为3个函数,减少30行
- `src/utils/format.ts`: 移除重复代码
### 建议简化
- `src/api/orders.ts:45`: 嵌套4层,建议重构
### 指标
- 平均函数长度: 25行 ✅
- 最大嵌套深度: 2层 ✅
- 重复代码: 0处 ✅
触发: 开发阶段自动加载 | 协作: LD + QE | 记忆: 沉淀到 Memory MCP