跳到正文
Skillpedia
通用编程8.2k · #12539

simplify-code

它能做什么

这项技能帮助你的 AI 找出并去除代码中不必要的复杂部分。适合代码已经变得难读或难维护的开发者。

适用场景

  • ✓当代码库中有一些几乎不起作用的包装类或抽象的时候。
  • ✓当你发现无用代码、未使用的导入或难以理清的深层嵌套逻辑的时候。
  • ✓当你希望代码更易读,同时保持原有行为的时候。

技能链接

https://github.com/vudovn/ag-kit/blob/main/.agents/skills/simplify-code/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/vudovn/ag-kit/blob/main/.agents/skills/simplify-code/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请简化这个模块。删除所有没用到的部分,并拉平过深的嵌套逻辑,但行为要保持不变。”

每周 5 个新技能,1 分钟读完。

工作原理

技能会对照常见复杂性问题的清单检查代码,然后删除或拉平不需要的部分,并保持行为不变。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Simplify Code — Reduce Unnecessary Complexity

The best code is the code you don't have to write. The second best is the code anyone can read.

Core Principle

Complexity is a cost. Every abstraction, every indirection, every clever pattern
adds cognitive load. Simplify ruthlessly unless complexity serves a clear purpose.

Simplification Checklist

1. Unnecessary Abstractions

Smell Simplification
Wrapper class that just delegates Remove wrapper, use the inner class directly
Factory that creates only one type Replace with direct constructor
Strategy pattern with one strategy Replace with simple function
Interface with one implementation Remove interface, use the class
Abstract class with one child Merge into the child class
Config object for 2 values Use function parameters

2. Dead Code

Smell Action
Unused imports Remove
Unreachable branches Remove (check tests first)
Commented-out code Remove (it's in git history)
Unused variables/functions Remove
TODO comments older than 6 months Remove or create issue
Feature flags for launched features Remove flag, keep the code

3. Deep Nesting

// ❌ Before: 4 levels deep
function process(data) {
  if (data) {
    if (data.items) {
      for (const item of data.items) {
        if (item.active) {
          doSomething(item)
        }
      }
    }
  }
}

// ✅ After: Early returns + filter
function process(data) {
  if (!data?.items) return

  data.items
    .filter(item => item.active)
    .forEach(doSomething)
}

4. Over-Parameterized Functions

// ❌ Before: 8 parameters
function createUser(name, email, age, role, dept, active, verified, avatar) { }

// ✅ After: Object parameter
function createUser(opts: CreateUserOpts) { }

5. Premature Optimization

Smell Simplification
Custom cache for <100 items Remove cache, measure first
Memoization on cheap functions Remove memo
Lazy loading for small modules Use direct import
Complex state machine for 3 states Use simple if/else or switch

Simplification Protocol

Step 1: Identify Complexity

- Count nesting levels (target: ≤3)
- Count function parameters (target: ≤4)
- Count lines per function (target: ≤30)
- Count abstractions per feature (target: ≤2)
- Check for dead code (target: 0)

Step 2: Verify Understanding

Before simplifying, ensure you understand:

  • What the code does (not what it looks like it does)
  • Why it was written this way (maybe there's a reason)
  • What tests cover it (simplification must not break tests)

Step 3: Simplify Incrementally

1. Remove dead code first (safest)
2. Flatten nesting with early returns
3. Inline trivial abstractions
4. Merge related functions
5. Simplify data structures

Step 4: Verify Behavior Preserved

npm run test    # All existing tests still pass
npm run build   # Still compiles

When NOT to Simplify

Situation Why Keep Complexity
Performance-critical hot path Optimization may look complex but is necessary
Required by framework/library External constraints
Explicitly requested pattern User chose this architecture
Will need extension soon Abstraction prepares for known growth

Ask first: "This pattern seems over-engineered. Should I simplify it, or is there a reason for the abstraction?"

常见问题

简化会改变我代码的工作方式吗?

这项技能设计为保持行为不变,并在删除分支前检查测试。

什么算作无用代码?

未使用的导入、未使用的变量和函数、无法到达的分支,以及旧的被注释掉的代码。

它会移除功能开关吗?

对于已经全面上线的功能,它建议移除开关,但保留功能代码本身。

详情

作者的原始描述

Reduce complexity of over-engineered code. Identify unnecessary abstractions, remove dead code, flatten deep nesting, and simplify logic while preserving behavior.

repo: vudovn/ag-kit

热度

GitHub 星标
8.2k
排名
#12539
许可证
MIT
更新时间
2026/10/18

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能