boy-scout-rule

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Boy Scout Rule

Boy Scout Rule

"Leave the campground cleaner than you found it."
Always leave code better than you found it. Make incremental improvements when you touch a file.
「离开营地时,要比你刚来时更干净。」
始终让代码比你接手时更完善。 当你接触某个文件时,进行增量式优化。

What to Improve

可优化的方向

Code Quality:
  • Remove dead code (commented blocks, unused functions)
  • Fix linting issues in files you touch
  • Improve unclear naming (
    x
    ,
    temp
    ,
    data
    → descriptive)
  • Add type annotations (TypeScript/Elixir @spec)
  • Extract magic numbers to named constants
  • Simplify complex logic
  • Add missing error handling
  • Update outdated comments
  • Fix formatting
  • Remove unused imports/variables
  • Consolidate duplication
代码质量:
  • 移除死代码(注释块、未使用的函数)
  • 修复你所修改文件中的代码检查问题
  • 优化不清晰的命名(如
    x
    temp
    data
    改为描述性名称)
  • 添加类型注解(TypeScript/Elixir @spec)
  • 将魔法值提取为命名常量
  • 简化复杂逻辑
  • 补充缺失的错误处理
  • 更新过时的注释
  • 修复格式问题
  • 移除未使用的导入/变量
  • 合并重复代码

What NOT to Do

禁止操作

  • ❌ Massive unrelated refactors
  • ❌ Change behavior without tests
  • ❌ Fix everything in the file (stay focused)
  • ❌ Breaking changes without tests
  • ❌ Premature optimization
  • ❌ Change unrelated sections
  • ❌ 进行大量无关的重构
  • ❌ 在没有测试的情况下修改代码行为
  • ❌ 修复文件中的所有问题(保持专注)
  • ❌ 在没有测试的情况下进行破坏性修改
  • ❌ 过早优化
  • ❌ 修改无关代码段

Process

实施流程

  1. Before changes: Read file, note obvious issues, run linter
  2. Make primary changes: Implement feature/fix, write tests
  3. Apply improvements: Fix linting, improve naming, add types, extract constants, remove dead code
  4. Run verification:
    mix lint && mix test
    or
    yarn test:lint && yarn ts:check && yarn test
  5. Document: Include boy scout changes in commit message
  1. 修改前:阅读文件,记录明显问题,运行代码检查工具
  2. 进行主要修改:实现功能/修复问题,编写测试
  3. 应用优化:修复代码检查问题、优化命名、添加类型、提取常量、移除死代码
  4. 验证:执行
    mix lint && mix test
    yarn test:lint && yarn ts:check && yarn test
  5. 记录:在提交信息中注明童子军式优化内容

Example Commit Message

提交信息示例

text
Add worker search filter

- Implement location-based filtering
- Add tests for search radius

Boy Scout improvements:
- Extract SEARCH_RADIUS constant
- Add type annotations to helper functions
- Remove unused import statements
- Improve variable naming in search logic
text
Add worker search filter

- Implement location-based filtering
- Add tests for search radius

Boy Scout improvements:
- Extract SEARCH_RADIUS constant
- Add type annotations to helper functions
- Remove unused import statements
- Improve variable naming in search logic

Example Improvements

优化示例

Before:
typescript
function calculateTotal(items) {  // No types
  let t = 0;  // Poor naming
  for (let i = 0; i < items.length; i++) {  // Old-style
    t += items[i].price * 1.08;  // Magic number
  }
  return t;
}
After:
typescript
const TAX_RATE = 1.08;

function calculateTotal(items: Item[]): number {
  return items.reduce((total, item) => {
    return total + (item.price * TAX_RATE);
  }, 0);
}
Improvements: types, constant, naming, modern syntax, simplified.
优化前:
typescript
function calculateTotal(items) {  // No types
  let t = 0;  // Poor naming
  for (let i = 0; i < items.length; i++) {  // Old-style
    t += items[i].price * 1.08;  // Magic number
  }
  return t;
}
优化后:
typescript
const TAX_RATE = 1.08;

function calculateTotal(items: Item[]): number {
  return items.reduce((total, item) => {
    return total + (item.price * TAX_RATE);
  }, 0);
}
优化点:类型注解、常量提取、命名优化、现代语法、逻辑简化。

Critical Rules

核心规则

  • Keep improvements in same commit as primary changes
  • Focus on "blast radius" (code near your changes)
  • Prioritize readability over cleverness
  • Always run full test suite after improvements
  • Ask for help when unsure about business logic
  • Small improvements > perfect refactors
  • 优化内容与主要修改放在同一个提交中
  • 聚焦“影响范围”(你修改代码附近的区域)
  • 可读性优先于技巧性
  • 优化后务必运行完整测试套件
  • 对业务逻辑存疑时及时求助
  • 小步优化优于完美重构

Checklist

检查清单

  • Removed dead code in files I touched
  • Fixed linting issues
  • Improved naming where I made changes
  • Added type annotations where missing
  • Extracted magic numbers
  • Updated outdated comments
  • Removed unused imports
  • Simplified complex logic
  • Added error handling
  • All tests pass
  • No new linting errors
  • Documented in commit message
  • 已移除我修改文件中的死代码
  • 已修复代码检查问题
  • 已优化我修改部分的命名
  • 已补充缺失的类型注解
  • 已提取魔法值为常量
  • 已更新过时的注释
  • 已移除未使用的导入
  • 已简化复杂逻辑
  • 已补充错误处理
  • 所有测试均通过
  • 无新增代码检查错误
  • 已在提交信息中记录优化内容

Integration

实践场景

During implementation: Make changes, apply boy scout, verify, commit together
During code review: Look for boy scout opportunities, recognize good boy scouting
During bug fixes: Fix bug, improve surrounding code, add tests, clean up
开发过程中:修改代码、应用童子军规则、验证、一起提交
代码评审时:寻找童子军式优化的机会,认可优秀的优化行为
修复Bug时:修复Bug、优化周边代码、添加测试、清理代码

Remember

谨记

Incremental improvement, not perfection

增量优化,而非追求完美

  • Small improvements compound over time
  • Every file touched is an opportunity
  • Be a good steward of the codebase
  • When in doubt, ask for review on larger improvements
  • 小的优化会随着时间积累产生大效果
  • 每一个你接触的文件都是优化的机会
  • 做代码库的好管理者
  • 对较大的优化存疑时,请求评审