code-simplification
它能做什么
这个工具可以帮助你把代码写得更易读、更容易理解。它在不改变代码行为的前提下,提升代码的清晰度。
适用场景
- ✓代码能正常运行,但读起来很费劲的时候。
- ✓在代码审查中遇到过于复杂的代码时。
- ✓遇到非常复杂的逻辑或含义不清的命名时。
技能链接
https://github.com/addyosmani/agent-skills/blob/main/skills/code-simplification/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请简化这段代码,让它更容易理解:”
每周 5 个新技能,1 分钟读完。
工作原理
工具会分析代码,找出让它更直观的方法,比如使用更好的命名,或者调整逻辑的组织方式。它会确保代码的运行结果和之前完全一样。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Code Simplification
Inspired by the Claude Code Simplifier plugin. Adapted here as a model-agnostic, process-driven skill for any AI coding agent.
Overview
Simplify code by reducing complexity while preserving exact behavior. The goal is not fewer lines — it's code that is easier to read, understand, modify, and debug. Every simplification must pass a simple test: "Would a new team member understand this faster than the original?"
When to Use
- After a feature is working and tests pass, but the implementation feels heavier than it needs to be
- During code review when readability or complexity issues are flagged
- When you encounter deeply nested logic, long functions, or unclear names
- When refactoring code written under time pressure
- When consolidating related logic scattered across files
- After merging changes that introduced duplication or inconsistency
When NOT to use:
- Code is already clean and readable — don't simplify for the sake of it
- You don't understand what the code does yet — comprehend before you simplify
- The code is performance-critical and the "simpler" version would be measurably slower
- You're about to rewrite the module entirely — simplifying throwaway code wastes effort
The Five Principles
1. Preserve Behavior Exactly
Don't change what the code does — only how it expresses it. All inputs, outputs, side effects, error behavior, and edge cases must remain identical. If you're not sure a simplification preserves behavior, don't make it.
ASK BEFORE EVERY CHANGE:
→ Does this produce the same output for every input?
→ Does this maintain the same error behavior?
→ Does this preserve the same side effects and ordering?
→ Do all existing tests still pass without modification?
2. Follow Project Conventions
Simplification means making code more consistent with the codebase, not imposing external preferences. Before simplifying:
1. Read CLAUDE.md / project conventions
2. Study how neighboring code handles similar patterns
3. Match the project's style for:
- Import ordering and module system
- Function declaration style
- Naming conventions
- Error handling patterns
- Type annotation depth
Simplification that breaks project consistency is not simplification — it's churn.
3. Prefer Clarity Over Cleverness
Explicit code is better than compact code when the compact version requires a mental pause to parse.
// UNCLEAR: Dense ternary chain
const label = isNew ? 'New' : isUpdated ? 'Updated' : isArchived ? 'Archived' : 'Active';
// CLEAR: Readable mapping
function getStatusLabel(item: Item): string {
if (item.isNew) return 'New';
if (item.isUpdated) return 'Updated';
if (item.isArchived) return 'Archived';
return 'Active';
}
// UNCLEAR: Chained reduces with inline logic
const result = items.reduce((acc, item) => ({
...acc,
[item.id]: { ...acc[item.id], count: (acc[item.id]?.count ?? 0) + 1 }
}), {});
// CLEAR: Named intermediate step
const countById = new Map<string, number>();
for (const item of items) {
countById.set(item.id, (countById.get(item.id) ?? 0) + 1);
}
4. Maintain Balance
Simplification has a failure mode: over-simplification. Watch for these traps:
- Inlining too aggressively — removing a helper that gave a concept a name makes the call site harder to read
- Combining unrelated logic — two simple functions merged into one complex function is not simpler
- Removing "unnecessary" abstraction — some abstractions exist for extensibility or testability, not complexity
- Optimizing for line count — fewer lines is not the goal; easier comprehension is
5. Scope to What Changed
Default to simplifying recently modified code. Avoid drive-by refactors of unrelated code unless explicitly asked to broaden scope. Unscoped simplification creates noise in diffs and risks unintended regressions.
The Simplification Process
Step 1: Understand Before Touching (Chesterton's Fence)
Before changing or removing anything, understand why it exists. This is Chesterton's Fence: if you see a fence across a road and don't understand why it's there, don't tear it down. First understand the reason, then decide if the reason still applies.
BEFORE SIMPLIFYING, ANSWER:
- What is this code's responsibility?
…
常见问题
这会改变代码的运行方式吗?
不会。目标只是提升清晰度,代码会和之前做完全相同的事情。
我需要会编程才能使用吗?
了解一些编程基础会有帮助,这样更容易看懂代码在做什么。不过这个工具的设计初衷就是帮你简化代码,让它更容易理解。
我可以用它把代码写得更短吗?
它的重点是清晰和易懂,而不是减少行数。有时候,代码稍微长一点反而更好读。
详情
作者的原始描述
Simplifies code for clarity. Use when refactoring code for clarity without changing behavior. Use when code works but is harder to read, maintain, or extend than it should be. Use when reviewing code that has accumulated unnecessary complexity.
repo: addyosmani/agent-skills
热度
- GitHub 星标
- 107k
- 排名
- #765
- 许可证
- MIT
- 更新时间
- 2026/10/13
评论
- …
同一仓库的更多技能
api-and-interface-design
107k帮助设计定义清晰的 API 和接口。适用于规划软件不同部分之间的通信方式。
browser-testing-with-devtools
107k帮助你的智能体直接在真实浏览器中测试代码。适用于检查功能在实际使用环境中的表现,并发现错误和性能问题。
ci-cd-and-automation
107k教你的 AI 智能体自动化重复性任务、发布项目并管理基础设施。
code-review-and-quality
107k从多个角度对代码进行详细审查。适用于合并新改动之前,确保质量并避免日后出现问题。