code-simplifier
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCommunity Code Simplification Best Practices
社区代码简化最佳实践
Comprehensive code simplification guide for AI agents and LLMs. Contains 47 rules across 8 categories, prioritized by impact from critical (context discovery, behavior preservation) to incremental (language idioms). Each rule includes detailed explanations, real-world examples comparing incorrect vs. correct implementations, and specific impact metrics.
这是一份面向AI Agent与大语言模型(LLMs)的综合性代码简化指南。指南包含8个类别下的47条规则,按影响优先级排序,从关键级(上下文发现、行为保留)到增量级(语言惯用写法)。每条规则都配有详细说明、对比错误与正确实现的真实示例,以及具体的影响指标。
Core Principles
核心原则
- Context First: Understand project conventions before making any changes
- Behavior Preservation: Change how code is written, never what it does
- Scope Discipline: Focus on recently modified code, keep diffs small
- Clarity Over Brevity: Explicit, readable code beats clever one-liners
- 上下文优先:在进行任何变更前,先了解项目约定
- 行为保留:只修改代码的编写方式,绝不改变其功能
- 范围规范:聚焦近期修改的代码,保持差异(diff)最小化
- 清晰优于简洁:明确、易读的代码胜过巧妙的单行代码
When to Apply
适用场景
Reference these guidelines when:
- Simplifying or cleaning up recently modified code
- Reducing nesting, complexity, or duplication
- Improving naming and readability
- Applying language-specific idiomatic patterns
- Reviewing code for maintainability issues
在以下场景中可参考本指南:
- 简化或清理近期修改的代码
- 减少嵌套、复杂度或重复代码
- 提升命名的合理性与代码可读性
- 应用特定语言的惯用写法
- 评审代码的可维护性问题
Rule Categories by Priority
按优先级排序的规则类别
| Priority | Category | Impact | Prefix | Rules |
|---|---|---|---|---|
| 1 | Context Discovery | CRITICAL | | 4 |
| 2 | Behavior Preservation | CRITICAL | | 6 |
| 3 | Scope Management | HIGH | | 5 |
| 4 | Control Flow Simplification | HIGH | | 9 |
| 5 | Naming and Clarity | MEDIUM-HIGH | | 6 |
| 6 | Duplication Reduction | MEDIUM | | 5 |
| 7 | Dead Code Elimination | MEDIUM | | 5 |
| 8 | Language Idioms | LOW-MEDIUM | | 7 |
| 优先级 | 类别 | 影响级别 | 前缀 | 规则数量 |
|---|---|---|---|---|
| 1 | 上下文发现 | CRITICAL | | 4 |
| 2 | 行为保留 | CRITICAL | | 6 |
| 3 | 范围管理 | HIGH | | 5 |
| 4 | 控制流简化 | HIGH | | 9 |
| 5 | 命名与清晰度 | MEDIUM-HIGH | | 6 |
| 6 | 重复代码消除 | MEDIUM | | 5 |
| 7 | 死代码清除 | MEDIUM | | 5 |
| 8 | 语言惯用写法 | LOW-MEDIUM | | 7 |
Quick Reference
快速参考
1. Context Discovery (CRITICAL)
1. 上下文发现(CRITICAL)
- - Always read CLAUDE.md before simplifying
ctx-read-claude-md - - Check for linting and formatting configs
ctx-detect-lint-config - - Match existing code style in file and project
ctx-follow-existing-patterns - - Project conventions override generic best practices
ctx-project-over-generic
- - 简化代码前务必阅读CLAUDE.md
ctx-read-claude-md - - 检查代码检查与格式化配置
ctx-detect-lint-config - - 匹配文件与项目中已有的代码风格
ctx-follow-existing-patterns - - 项目约定优先于通用最佳实践
ctx-project-over-generic
2. Behavior Preservation (CRITICAL)
2. 行为保留(CRITICAL)
- - Preserve all return values and outputs
behave-preserve-outputs - - Preserve error messages, types, and handling
behave-preserve-errors - - Preserve public function signatures and types
behave-preserve-api - - Preserve side effects (logging, I/O, state changes)
behave-preserve-side-effects - - Forbid subtle semantic changes
behave-no-semantics-change - - Verify behavior preservation before finalizing
behave-verify-before-commit
- - 保留所有返回值与输出
behave-preserve-outputs - - 保留错误信息、类型与处理逻辑
behave-preserve-errors - - 保留公共函数签名与类型
behave-preserve-api - - 保留副作用(日志、I/O、状态变更)
behave-preserve-side-effects - - 禁止细微的语义变更
behave-no-semantics-change - - 最终确定前验证行为是否保留
behave-verify-before-commit
3. Scope Management (HIGH)
3. 范围管理(HIGH)
- - Focus on recently modified code only
scope-recent-code-only - - Keep changes small and reviewable
scope-minimal-diff - - No unrelated refactors
scope-no-unrelated-refactors - - Avoid global rewrites and architectural changes
scope-no-global-rewrites - - Respect module and component boundaries
scope-respect-boundaries
- - 仅聚焦近期修改的代码
scope-recent-code-only - - 保持变更最小化,便于评审
scope-minimal-diff - - 不进行无关的重构
scope-no-unrelated-refactors - - 避免全局重写与架构变更
scope-no-global-rewrites - - 遵守模块与组件边界
scope-respect-boundaries
4. Control Flow Simplification (HIGH)
4. 控制流简化(HIGH)
- - Use early returns to reduce nesting
flow-early-return - - Use guard clauses for preconditions
flow-guard-clauses - - Never use nested ternary operators
flow-no-nested-ternaries - - Prefer explicit control flow over dense expressions
flow-explicit-over-dense - - Flatten deep nesting to maximum 2-3 levels
flow-flatten-nesting - - Each code block should do one thing
flow-single-responsibility - - Prefer positive conditions over double negatives
flow-positive-conditions - - Use optional chaining and nullish coalescing
flow-optional-chaining - - Simplify boolean expressions
flow-boolean-simplification
- - 使用提前返回减少嵌套
flow-early-return - - 使用卫语句处理前置条件
flow-guard-clauses - - 绝不使用嵌套三元运算符
flow-no-nested-ternaries - - 优先选择明确的控制流而非密集表达式
flow-explicit-over-dense - - 将深层嵌套扁平化至最多2-3级
flow-flatten-nesting - - 每个代码块应只负责一件事
flow-single-responsibility - - 优先使用正向条件而非双重否定
flow-positive-conditions - - 使用可选链与空值合并运算符
flow-optional-chaining - - 简化布尔表达式
flow-boolean-simplification
5. Naming and Clarity (MEDIUM-HIGH)
5. 命名与清晰度(MEDIUM-HIGH)
- - Use intention-revealing names
name-intention-revealing - - Use nouns for data, verbs for actions
name-nouns-for-data - - Avoid cryptic abbreviations
name-avoid-abbreviations - - Use consistent vocabulary throughout
name-consistent-vocabulary - - Avoid generic names
name-avoid-generic - - Prefer string interpolation over concatenation
name-string-interpolation
- - 使用能体现意图的命名
name-intention-revealing - - 数据用名词命名,动作使用动词
name-nouns-for-data - - 避免晦涩的缩写
name-avoid-abbreviations - - 在整个项目中使用一致的术语
name-consistent-vocabulary - - 避免使用通用名称
name-avoid-generic - - 优先使用字符串插值而非拼接
name-string-interpolation
6. Duplication Reduction (MEDIUM)
6. 重复代码消除(MEDIUM)
- - Apply the rule of three
dup-rule-of-three - - Avoid single-use helper functions
dup-no-single-use-helpers - - Extract only when it improves clarity
dup-extract-for-clarity - - Prefer duplication over premature abstraction
dup-avoid-over-abstraction - - Use data-driven patterns over repetitive conditionals
dup-data-driven
- - 应用三次原则
dup-rule-of-three - - 避免仅使用一次的辅助函数
dup-no-single-use-helpers - - 仅在提升清晰度时提取代码
dup-extract-for-clarity - - 优先选择重复而非过早抽象
dup-avoid-over-abstraction - - 使用数据驱动模式替代重复的条件判断
dup-data-driven
7. Dead Code Elimination (MEDIUM)
7. 死代码清除(MEDIUM)
- - Delete unused code artifacts
dead-remove-unused - - Delete code, never comment it out
dead-delete-not-comment - - Remove comments that state the obvious
dead-remove-obvious-comments - - Keep comments that explain why, not what
dead-keep-why-comments - - Remove stale TODO/FIXME comments
dead-remove-todo-fixme
- - 删除未使用的代码产物
dead-remove-unused - - 删除代码,而非注释掉
dead-delete-not-comment - - 删除陈述显而易见内容的注释
dead-remove-obvious-comments - - 保留解释原因而非内容的注释
dead-keep-why-comments - - 删除过时的TODO/FIXME注释
dead-remove-todo-fixme
8. Language Idioms (LOW-MEDIUM)
8. 语言惯用写法(LOW-MEDIUM)
- - Use strict types over any (TypeScript)
idiom-ts-strict-types - - Use const assertions and readonly (TypeScript)
idiom-ts-const-assertions - - Use ? for error propagation (Rust)
idiom-rust-question-mark - - Use iterator chains when clearer (Rust)
idiom-rust-iterator-chains - - Use comprehensions for simple transforms (Python)
idiom-python-comprehensions - - Handle errors immediately (Go)
idiom-go-error-handling - - Prefer language and stdlib builtins
idiom-prefer-language-builtins
- - 使用严格类型而非any(TypeScript)
idiom-ts-strict-types - - 使用const断言与readonly(TypeScript)
idiom-ts-const-assertions - - 使用?进行错误传播(Rust)
idiom-rust-question-mark - - 在更清晰时使用迭代器链(Rust)
idiom-rust-iterator-chains - - 使用推导式进行简单转换(Python)
idiom-python-comprehensions - - 立即处理错误(Go)
idiom-go-error-handling - - 优先使用语言与标准库内置功能
idiom-prefer-language-builtins
Workflow
工作流程
- Discover context: Read CLAUDE.md, lint configs, examine existing patterns
- Identify scope: Focus on recently modified code unless asked to expand
- Apply transformations: Use rules in priority order (CRITICAL first)
- Verify behavior: Ensure outputs, errors, and side effects remain identical
- Keep diffs minimal: Small, focused changes that are easy to review
- 发现上下文:阅读CLAUDE.md、代码检查配置,查看现有代码模式
- 确定范围:除非要求扩展,否则聚焦近期修改的代码
- 应用转换:按优先级顺序应用规则(先关键级)
- 验证行为:确保输出、错误与副作用完全一致
- 保持差异最小:小而聚焦的变更便于评审
How to Use
使用方法
Read individual reference files for detailed explanations and code examples:
- Section definitions - Category structure and impact levels
- Rule template - Template for adding new rules
阅读单个参考文件获取详细说明与代码示例:
- 章节定义 - 类别结构与影响级别
- 规则模板 - 添加新规则的模板
Reference Files
参考文件
| File | Description |
|---|---|
| references/_sections.md | Category definitions and ordering |
| assets/templates/_template.md | Template for new rules |
| metadata.json | Version and reference information |
| 文件 | 描述 |
|---|---|
| references/_sections.md | 类别定义与排序 |
| assets/templates/_template.md | 新规则模板 |
| metadata.json | 版本与参考信息 |