clean-as-you-go

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

The Boy Scout Rule

童子军法则

Leave the code cleaner than you found it — but only the code you already had to touch.
让代码比你接手时更整洁——但仅限你已经接触到的代码。

The Rule

法则内容

  1. Complete the requested task first.
  2. Scan the immediate vicinity of your change (same function, same block, nearby declarations) for one proportional improvement.
  3. Apply
    clean-typescript
    rules to the touched code.
  4. Do not touch code outside your change scope.
Proportional means: the cleanup matches the change size. A one-line fix warrants a renamed variable, not a refactored module.
  1. 先完成要求的任务。
  2. 扫描你变更的紧邻区域(同一函数、同一代码块、附近的声明),寻找一处与变更规模相称的改进点。
  3. 对涉及的代码应用
    clean-typescript
    规则。
  4. 不要触碰变更范围外的代码。
相称意味着: 清理工作的规模与变更大小匹配。一行代码的修复只需要重命名变量,而不是重构整个模块。

What to Look For

检查要点

  • Poorly named variable or parameter
  • Redundant comment or dead code
  • Magic value that should be a named constant
  • Deeply nested block extractable into a named function
  • Output argument mutation replaceable with a return value
  • Boolean flag hiding two separate responsibilities
  • 命名不佳的变量或参数
  • 冗余注释或死代码
  • 应替换为命名常量的魔法值
  • 可提取为命名函数的深层嵌套代码块
  • 可替换为返回值的输出参数突变
  • 隐藏两种独立职责的布尔标志

Example

示例

ts
// Asked to fix a bug in:
function proc(d: number[], x: number[], flag = false): number[] {
  for (const i of d) {
    if (i > 0) {
      if (flag) { x.push(i * 1.0825); }
      else { x.push(i); }
    }
  }
  return x;
}

// Fix the bug AND leave it cleaner:
const TAX_RATE = 0.0825;

function processPositiveValues(values: readonly number[]): number[] {
  return values.filter((value) => value > 0);
}

function processTaxablePositiveValues(values: readonly number[]): number[] {
  return values.filter((value) => value > 0).map((value) => value * (1 + TAX_RATE));
}
The cleanup — descriptive names, named constant, no mutation, boolean flag removed — is proportional because it stays within the same function.
ts
// Asked to fix a bug in:
function proc(d: number[], x: number[], flag = false): number[] {
  for (const i of d) {
    if (i > 0) {
      if (flag) { x.push(i * 1.0825); }
      else { x.push(i); }
    }
  }
  return x;
}

// Fix the bug AND leave it cleaner:
const TAX_RATE = 0.0825;

function processPositiveValues(values: readonly number[]): number[] {
  return values.filter((value) => value > 0);
}

function processTaxablePositiveValues(values: readonly number[]): number[] {
  return values.filter((value) => value > 0).map((value) => value * (1 + TAX_RATE));
}
此次清理——使用描述性命名、命名常量、无突变、移除布尔标志——是相称的,因为所有修改都局限在原函数的范围内。

AI Behavior

AI行为规范

  1. Complete the task first.
  2. Scan the touched function or block for one improvement worth making.
  3. Apply it only if it stays within the change scope.
  4. Briefly note what was improved.
If the surrounding code is already clean, say so and move on. Do not manufacture a change to satisfy the rule.
  1. 先完成任务。
  2. 扫描所涉及的函数或代码块,寻找一处值得改进的地方。
  3. 仅在变更范围内应用改进。
  4. 简要说明改进的内容。
如果周边代码已经很整洁,直接说明并继续后续工作。不要为了满足法则而刻意制造变更。