improve-architecture

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

improve-architecture

improve-architecture

Invest in the shape of the system a little at a time rather than paying for a rewrite later. This skill diagnoses and proposes; it does not rewrite on its own.
少量、持续地投资于系统结构,避免日后付出重写的代价。此 skill 用于诊断并提出建议;它本身不会重写代码。

Method

方法

  1. Map first. Get the current shape before judging it - entry points, modules, data flow, who calls whom. Reuse
    module-map
    for the one-screen view; do not skip to opinions.
  2. Find the strain. Look for the load-bearing problems, not style nits:
    • God modules that everything imports and nothing can change safely.
    • Leaky boundaries - a module reaching into another's internals instead of its surface.
    • Duplicated logic that drifts (the same rule implemented three ways).
    • Wrong seams - the code is split where it does not bend and fused where it does.
    • Cyclic dependencies.
    • A shape that fights the domain (see
      domain-modeling
      - boundaries in code should track boundaries in the language).
  3. Propose the smallest move. For each problem, the least change that relieves it: extract a boundary, collapse a duplicate, invert a dependency, move a seam. Prefer a sequence of safe steps over one big cut.
  4. Rank by leverage. Order the moves by pain relieved over effort. Say which are safe now and which need a test net first (pair with
    tdd
    ).
  5. Record the big calls. For a structural change a future reader would question, write a decision record in
    docs/decisions/NNNN-slug.md
    : context, choice, alternatives rejected, why.
  1. 先绘制结构图。 在评判之前了解当前形态——入口点、模块、数据流以及调用关系。可以复用
    module-map
    获取一屏视图;不要直接发表主观意见。
  2. 找出压力所在。 寻找影响系统结构的关键问题,而不是纠结代码风格:
    • 上帝模块:所有内容都导入它,却没有任何内容能够安全修改。
    • 边界泄漏——某个模块直接访问另一个模块的内部实现,而不是使用其对外接口。
    • 不断产生差异的重复逻辑(同一规则以三种方式实现)。
    • 不合理的接缝——代码拆分的位置无法灵活变化,而应当拆分的地方却耦合在一起。
    • 循环依赖。
    • 与领域模型冲突的结构形态(参见
      domain-modeling
      ——代码中的边界应当与语言中的边界保持一致)。
  3. 提出最小调整。 针对每个问题,采用能够缓解该问题的最小改动:提取边界、合并重复逻辑、反转依赖或移动接缝。优先采用一系列安全步骤,而不是一次性大规模重构。
  4. 按收益排序。 根据“缓解的痛苦与所需投入之比”对调整进行排序。说明哪些调整现在可以安全执行,哪些需要先建立测试保护网(可与
    tdd
    配合使用)。
  5. 记录重大决策。 对于未来读者可能会质疑的结构性变更,在
    docs/decisions/NNNN-slug.md
    中编写决策记录:背景、选择、被否决的备选方案以及原因。

Guardrails

约束条件

  • Diagnose, do not rewrite. The deliverable is a plan the user approves before any code moves.
  • No churn for taste. A move must relieve a named strain; "cleaner" is not a reason.
  • Three similar lines beat a premature abstraction - do not trade duplication for indirection unless the duplication actually drifts.
  • Keep behavior fixed. Structural moves are refactors; land them behind green tests.
  • 只做诊断,不进行重写。交付物是一份计划,用户确认后才可调整代码。
  • 不要为了个人审美而制造变动。每次调整都必须缓解一个明确的问题;“看起来更干净”不是理由。
  • 三行相似代码优于过早抽象——除非重复逻辑确实已经产生差异,否则不要用间接层换取抽象。
  • 保持行为不变。结构调整属于重构,必须在测试全部通过的情况下落地。

Output

输出

A prioritized list: each item is the problem, the smallest move, the risk, and whether it needs a test net first. Plus decision records for the big moves. No code changes until the user picks what to do.
一份优先级列表:每项包含问题、最小调整、风险,以及是否需要先建立测试保护网。对于重大调整,还需提供决策记录。在用户选择执行方案之前,不进行任何代码变更。