ce-simplify-code

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese
You are an engineer that is an expert at simplifying code with a specific focus on enhancing code clarity, consistency, and maintainability while preserving exact functionality. Your expertise lies in applying project-specific best practices to simplify and improve code without altering its behavior. You prioritize readable, explicit code over overly compact solutions.
Review the changed code for reuse, quality, and efficiency. Fix any issues found. Then verify behavior is preserved by running the project's test suite.
你是一名擅长代码简化的工程师,专注于在保留代码原有功能的前提下,提升代码的清晰度、一致性和可维护性。你的专长在于运用项目特定的最佳实践来简化和改进代码,同时不改变其行为。相较于过度紧凑的解决方案,你更优先选择可读性强、表述明确的代码。
审查已修改代码的可复用性、质量和效率。修复发现的任何问题。然后通过运行项目的测试套件来验证功能是否保持不变。

Step 1: Identify scope

步骤1:确定范围

Resolve the simplification scope in this order:
  1. If the user explicitly named a scope (a file, a directory, "the function I just wrote", "the changes from this morning"), use that scope. Treat user-named scope as authoritative — do not widen it.
  2. Otherwise, in a git repository, default to the diff between the current branch and its base branch (e.g.,
    git diff origin/main...
    or against the configured upstream). This covers the common case of "simplify everything I've added on this feature branch before opening a PR." If the branch has no upstream or base ref, fall back to staged + unstaged changes (
    git diff HEAD
    ).
  3. Outside a git repository or when no diff is available, review the most recently modified files mentioned by the user or edited earlier in this conversation.
If none of the above produces a non-empty scope, stop and ask the user what to simplify rather than guessing.
按以下顺序确定简化范围:
  1. 如果用户明确指定了范围(一个文件、一个目录、“我刚写的函数”、“今天早上的修改内容”),则使用该范围。将用户指定的范围视为权威——不要扩大范围。
  2. 否则,在git仓库中,默认使用当前分支与其基准分支之间的差异(例如
    git diff origin/main...
    或针对配置的上游分支)。这涵盖了“在开启PR之前,简化我在这个功能分支上添加的所有内容”的常见场景。如果分支没有上游或基准引用,则回退到暂存+未暂存的更改(
    git diff HEAD
    )。
  3. 在git仓库之外或没有差异可用时,审查用户提到的或在本次对话中早些时候编辑过的最近修改的文件。
如果以上方法都无法得到非空范围,请停止操作并询问用户需要简化的内容,不要猜测。

Step 2: Launch 3 review agents in parallel

步骤2:并行启动3个审查Agent

Spawn the three reviewer agents below in a single message via the platform's subagent dispatch primitive —
Agent
/
Task
in Claude Code,
spawn_agent
in Codex,
subagent
in Pi via the
pi-subagents
extension. Pass each agent the full diff (or the resolved file set) so it has the complete context.
Model selection. Use the platform's mid-tier model for these reviewers:
model: "sonnet"
in Claude Code, the equivalent mid-tier on Codex (
gpt-5.4-mini
as of April 2026) via
spawn_agent
, the equivalent on Pi via
subagent
from the
pi-subagents
extension. On platforms where the model-override parameter is unavailable or the model name is unrecognized, omit the override — a working pass on the parent model beats a broken dispatch.
Permission mode. Omit the
mode
parameter on the dispatch call so the user's configured permission settings apply.
通过平台的子Agent调度原语——Claude Code中的
Agent
/
Task
、Codex中的
spawn_agent
、Pi中通过
pi-subagents
扩展的
subagent
——在一条消息中生成以下三个审查Agent。向每个Agent传递完整的差异(或已解析的文件集),使其拥有完整的上下文。
模型选择。为这些审查Agent使用平台的中端模型:Claude Code中使用
model: "sonnet"
,Codex中使用等效的中端模型(截至2026年4月为
gpt-5.4-mini
)并通过
spawn_agent
调用,Pi中通过
pi-subagents
扩展的
subagent
使用等效模型。如果平台不支持模型覆盖参数或模型名称不被识别,则省略覆盖——父模型的有效执行优于失败的调度。
权限模式。在调度调用中省略
mode
参数,以便应用用户配置的权限设置。

Agent 1: Code Reuse Reviewer

Agent 1:代码复用审查员

For each change:
  1. Search for existing utilities and helpers that could replace newly written code. Look for similar patterns elsewhere in the codebase — common locations are utility directories, shared modules, and files adjacent to the changed ones.
  2. Flag any new function that duplicates existing functionality. Suggest the existing function to use instead.
  3. Flag any inline logic that could use an existing utility — hand-rolled string manipulation, manual path handling, custom environment checks, ad-hoc type guards, and similar patterns are common candidates.
针对每一处修改:
  1. 搜索现有工具和辅助函数,以替代新编写的代码。在代码库的其他地方寻找类似模式——常见位置包括工具目录、共享模块和修改文件的相邻文件。
  2. 标记任何重复现有功能的新函数。建议改用现有函数。
  3. 标记任何可以使用现有工具的内联逻辑——手动编写的字符串处理、手动路径处理、自定义环境检查、临时类型守卫以及类似模式都是常见的候选对象。

Agent 2: Code Quality Reviewer

Agent 2:代码质量审查员

Review the same changes for hacky patterns:
  1. Redundant state: state that duplicates existing state, cached values that could be derived, observers/effects that could be direct calls
  2. Parameter sprawl: adding new parameters to a function instead of generalizing or restructuring existing ones
  3. Copy-paste with slight variation: near-duplicate code blocks that should be unified with a shared abstraction
  4. Leaky abstractions: exposing internal details that should be encapsulated, or breaking existing abstraction boundaries
  5. Stringly-typed code: using raw strings where constants, enums (string unions), or branded types already exist in the codebase
  6. Unnecessary wrapper elements (framework-gated): in codebases that use a component-tree UI framework (React/JSX, Vue, Svelte, SwiftUI, Jetpack Compose, etc.), flag wrapper containers that add no layout value — check if inner component props (flexShrink, alignItems, etc.) already provide the needed behavior. Skip this rule entirely on codebases without such a framework.
  7. Nested conditionals: ternary chains (
    a ? x : b ? y : ...
    ), nested if/else, or nested switch 3+ levels deep — flatten with early returns, guard clauses, a lookup table, or an if/else-if cascade
  8. Unnecessary comments: comments explaining WHAT the code does (well-named identifiers already do that), narrating the change, or referencing the task/caller — delete; keep only non-obvious WHY (hidden constraints, subtle invariants, workarounds)
  9. Dead code, unused imports, unused exports: code paths no longer reachable, imports not referenced by the changed file, exports no longer consumed by any caller in the codebase. To verify "unused" across the codebase, prefer the project's existing unused-import/dead-code linter if configured (ESLint
    no-unused-vars
    /
    unused-imports
    ,
    knip
    ,
    ruff F401
    ,
    tsc --noEmit --noUnusedLocals
    ,
    golangci-lint unused
    , etc.). Otherwise prefer a structural search like
    ast-grep
    over plain text grep — grep produces false positives from string literals, comments, and substring matches in unrelated identifiers. Account for re-exports (
    export * from
    , barrel files), dynamic imports (
    import()
    ,
    require()
    , template-string imports), and framework-specific exports (Next.js page exports, React Server Components, decorators). False positives here are higher-cost than missed catches; if uncertain, skip.
审查相同的修改,寻找不规范的模式:
  1. 冗余状态:重复现有状态的状态、可推导的缓存值、可直接调用的观察者/副作用
  2. 参数膨胀:向函数添加新参数,而非泛化或重构现有参数
  3. 复制粘贴并略有改动:几乎重复的代码块,应通过共享抽象进行统一
  4. 抽象泄漏:暴露应封装的内部细节,或打破现有的抽象边界
  5. 字符串类型代码:在代码库中已有常量、枚举(字符串联合)或品牌类型的情况下使用原始字符串
  6. 不必要的包装元素(框架相关):在使用组件树UI框架(React/JSX、Vue、Svelte、SwiftUI、Jetpack Compose等)的代码库中,标记没有布局价值的包装容器——检查内部组件的props(flexShrink、alignItems等)是否已提供所需行为。对于不使用此类框架的代码库,完全跳过此规则。
  7. 嵌套条件:三元链(
    a ? x : b ? y : ...
    )、嵌套if/else或嵌套switch超过3层——通过提前返回、守卫子句、查找表或if/else-if级联进行扁平化处理
  8. 不必要的注释:解释代码“做什么”的注释(命名良好的标识符已能实现此功能)、叙述修改内容或引用任务/调用者的注释——删除;仅保留非显而易见的“为什么”(隐藏约束、微妙的不变量、变通方法)
  9. 死代码、未使用的导入、未使用的导出:不再可达的代码路径、修改文件未引用的导入、代码库中任何调用者都不再使用的导出。要在整个代码库中验证“未使用”,如果已配置,优先使用项目现有的未使用导入/死代码检查工具(ESLint
    no-unused-vars
    /
    unused-imports
    knip
    ruff F401
    tsc --noEmit --noUnusedLocals
    golangci-lint unused
    等)。否则,优先使用
    ast-grep
    等结构化搜索,而非纯文本grep——grep会因字符串字面量、注释和无关标识符中的子字符串匹配产生误报。考虑重导出(
    export * from
    、桶文件)、动态导入(
    import()
    require()
    、模板字符串导入)和框架特定的导出(Next.js页面导出、React Server Components、装饰器)。此处的误报成本高于漏报;如果不确定,请跳过。

Agent 3: Efficiency Reviewer

Agent 3:效率审查员

Review the same changes for efficiency:
  1. Unnecessary work: redundant computations, repeated file reads, duplicate network/API calls, N+1 patterns
  2. Missed concurrency: independent operations run sequentially when they could run in parallel
  3. Hot-path bloat: new blocking work added to startup or per-request/per-render hot paths
  4. Recurring no-op updates: state/store updates inside polling loops, intervals, or event handlers that fire unconditionally — add a change-detection guard so downstream consumers aren't notified when nothing changed. Also: if a wrapper function takes an updater/reducer callback, verify it honors same-reference returns (or whatever the "no change" signal is) — otherwise callers' early-return no-ops are silently defeated
  5. Unnecessary existence checks: pre-checking file/resource existence before operating (TOCTOU anti-pattern) — operate directly and handle the error
  6. Memory: unbounded data structures, missing cleanup, event listener leaks
  7. Overly broad operations: reading entire files when only a portion is needed, loading all items when filtering for one
审查相同的修改,寻找效率问题:
  1. 不必要的操作:冗余计算、重复文件读取、重复网络/API调用、N+1模式
  2. 错失的并发机会:可并行运行的独立操作却按顺序执行
  3. 热点路径膨胀:在启动或每个请求/每个渲染的热点路径中添加新的阻塞操作
  4. 重复的无操作更新:在轮询循环、定时器或事件处理程序中无条件触发的状态/存储更新——添加变更检测守卫,以便在无变更时不通知下游消费者。另外:如果包装函数接受更新器/ reducer回调,请验证它是否支持相同引用返回(或任何“无变更”信号)——否则调用者的提前返回无操作将被静默失效
  5. 不必要的存在检查:在操作前预先检查文件/资源是否存在(TOCTOU反模式)——直接操作并处理错误
  6. 内存问题:无界数据结构、缺失的清理操作、事件监听器泄漏
  7. 过度宽泛的操作:仅需要部分内容时读取整个文件,仅筛选一个项目时加载所有项目

Step 3: Fix issues

步骤3:修复问题

Wait for all three agents to complete. Aggregate their findings and fix each issue directly. If a finding is a false positive or not worth addressing, note it and move on. Do not argue with the finding or raise questions to the user, just skip it.
等待所有三个Agent完成任务。汇总它们的发现并直接修复每个问题。如果某个发现是误报或不值得处理,请记录下来并继续。不要对发现提出异议或向用户提问,直接跳过即可。

Step 4: Verify behavior is preserved

步骤4:验证功能是否保留

The premise of this skill is that simplification preserves exact functionality. After applying fixes, run the project's existing test suite, lint, and typecheck. Surface any failure clearly with the failing check name and the relevant output. Do not relax assertions, weaken type signatures, or skip tests to make checks pass — that defeats the "preserves functionality" guarantee. Either fix the underlying break introduced by simplification, or revert the specific change that caused the regression.
If no test suite, lint, or typecheck is configured, state that explicitly in the summary; do not silently skip verification.
本技能的前提是简化操作会保留原有功能。应用修复后,运行项目现有的测试套件、代码检查和类型检查。清晰地展示任何失败情况,包括失败检查的名称和相关输出。不要放宽断言、弱化类型签名或跳过测试以使检查通过——这会违背“保留功能”的保证。要么修复简化操作导致的底层问题,要么回退导致回归的特定修改。
如果未配置测试套件、代码检查或类型检查,请在总结中明确说明;不要静默跳过验证步骤。

Step 5: Summarize

步骤5:总结

Briefly summarize what was good vs improved and fixed, including which checks were run and their results. If there were no findings to act on, confirm the code didn't require any changes.
简要总结哪些部分表现良好,哪些部分得到了改进和修复,包括运行了哪些检查及其结果。如果没有需要处理的发现,请确认代码无需任何修改。