code-simplifier

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Community 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

核心原则

  1. Context First: Understand project conventions before making any changes
  2. Behavior Preservation: Change how code is written, never what it does
  3. Scope Discipline: Focus on recently modified code, keep diffs small
  4. Clarity Over Brevity: Explicit, readable code beats clever one-liners
  1. 上下文优先:在进行任何变更前,先了解项目约定
  2. 行为保留:只修改代码的编写方式,绝不改变其功能
  3. 范围规范:聚焦近期修改的代码,保持差异(diff)最小化
  4. 清晰优于简洁:明确、易读的代码胜过巧妙的单行代码

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

按优先级排序的规则类别

PriorityCategoryImpactPrefixRules
1Context DiscoveryCRITICAL
ctx-
4
2Behavior PreservationCRITICAL
behave-
6
3Scope ManagementHIGH
scope-
5
4Control Flow SimplificationHIGH
flow-
9
5Naming and ClarityMEDIUM-HIGH
name-
6
6Duplication ReductionMEDIUM
dup-
5
7Dead Code EliminationMEDIUM
dead-
5
8Language IdiomsLOW-MEDIUM
idiom-
7
优先级类别影响级别前缀规则数量
1上下文发现CRITICAL
ctx-
4
2行为保留CRITICAL
behave-
6
3范围管理HIGH
scope-
5
4控制流简化HIGH
flow-
9
5命名与清晰度MEDIUM-HIGH
name-
6
6重复代码消除MEDIUM
dup-
5
7死代码清除MEDIUM
dead-
5
8语言惯用写法LOW-MEDIUM
idiom-
7

Quick Reference

快速参考

1. Context Discovery (CRITICAL)

1. 上下文发现(CRITICAL)

  • ctx-read-claude-md
    - Always read CLAUDE.md before simplifying
  • ctx-detect-lint-config
    - Check for linting and formatting configs
  • ctx-follow-existing-patterns
    - Match existing code style in file and project
  • ctx-project-over-generic
    - Project conventions override generic best practices
  • ctx-read-claude-md
    - 简化代码前务必阅读CLAUDE.md
  • ctx-detect-lint-config
    - 检查代码检查与格式化配置
  • ctx-follow-existing-patterns
    - 匹配文件与项目中已有的代码风格
  • ctx-project-over-generic
    - 项目约定优先于通用最佳实践

2. Behavior Preservation (CRITICAL)

2. 行为保留(CRITICAL)

  • behave-preserve-outputs
    - Preserve all return values and outputs
  • behave-preserve-errors
    - Preserve error messages, types, and handling
  • behave-preserve-api
    - Preserve public function signatures and types
  • behave-preserve-side-effects
    - Preserve side effects (logging, I/O, state changes)
  • behave-no-semantics-change
    - Forbid subtle semantic changes
  • behave-verify-before-commit
    - Verify behavior preservation before finalizing
  • behave-preserve-outputs
    - 保留所有返回值与输出
  • behave-preserve-errors
    - 保留错误信息、类型与处理逻辑
  • behave-preserve-api
    - 保留公共函数签名与类型
  • behave-preserve-side-effects
    - 保留副作用(日志、I/O、状态变更)
  • behave-no-semantics-change
    - 禁止细微的语义变更
  • behave-verify-before-commit
    - 最终确定前验证行为是否保留

3. Scope Management (HIGH)

3. 范围管理(HIGH)

  • scope-recent-code-only
    - Focus on recently modified code only
  • scope-minimal-diff
    - Keep changes small and reviewable
  • scope-no-unrelated-refactors
    - No unrelated refactors
  • scope-no-global-rewrites
    - Avoid global rewrites and architectural changes
  • scope-respect-boundaries
    - Respect module and component 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)

  • flow-early-return
    - Use early returns to reduce nesting
  • flow-guard-clauses
    - Use guard clauses for preconditions
  • flow-no-nested-ternaries
    - Never use nested ternary operators
  • flow-explicit-over-dense
    - Prefer explicit control flow over dense expressions
  • flow-flatten-nesting
    - Flatten deep nesting to maximum 2-3 levels
  • flow-single-responsibility
    - Each code block should do one thing
  • flow-positive-conditions
    - Prefer positive conditions over double negatives
  • flow-optional-chaining
    - Use optional chaining and nullish coalescing
  • flow-boolean-simplification
    - Simplify boolean expressions
  • flow-early-return
    - 使用提前返回减少嵌套
  • flow-guard-clauses
    - 使用卫语句处理前置条件
  • flow-no-nested-ternaries
    - 绝不使用嵌套三元运算符
  • flow-explicit-over-dense
    - 优先选择明确的控制流而非密集表达式
  • flow-flatten-nesting
    - 将深层嵌套扁平化至最多2-3级
  • flow-single-responsibility
    - 每个代码块应只负责一件事
  • flow-positive-conditions
    - 优先使用正向条件而非双重否定
  • flow-optional-chaining
    - 使用可选链与空值合并运算符
  • flow-boolean-simplification
    - 简化布尔表达式

5. Naming and Clarity (MEDIUM-HIGH)

5. 命名与清晰度(MEDIUM-HIGH)

  • name-intention-revealing
    - Use intention-revealing names
  • name-nouns-for-data
    - Use nouns for data, verbs for actions
  • name-avoid-abbreviations
    - Avoid cryptic abbreviations
  • name-consistent-vocabulary
    - Use consistent vocabulary throughout
  • name-avoid-generic
    - Avoid generic names
  • name-string-interpolation
    - Prefer string interpolation over concatenation
  • 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)

  • dup-rule-of-three
    - Apply the rule of three
  • dup-no-single-use-helpers
    - Avoid single-use helper functions
  • dup-extract-for-clarity
    - Extract only when it improves clarity
  • dup-avoid-over-abstraction
    - Prefer duplication over premature abstraction
  • dup-data-driven
    - Use data-driven patterns over repetitive conditionals
  • 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)

  • dead-remove-unused
    - Delete unused code artifacts
  • dead-delete-not-comment
    - Delete code, never comment it out
  • dead-remove-obvious-comments
    - Remove comments that state the obvious
  • dead-keep-why-comments
    - Keep comments that explain why, not what
  • dead-remove-todo-fixme
    - Remove stale TODO/FIXME comments
  • dead-remove-unused
    - 删除未使用的代码产物
  • dead-delete-not-comment
    - 删除代码,而非注释掉
  • dead-remove-obvious-comments
    - 删除陈述显而易见内容的注释
  • dead-keep-why-comments
    - 保留解释原因而非内容的注释
  • dead-remove-todo-fixme
    - 删除过时的TODO/FIXME注释

8. Language Idioms (LOW-MEDIUM)

8. 语言惯用写法(LOW-MEDIUM)

  • idiom-ts-strict-types
    - Use strict types over any (TypeScript)
  • idiom-ts-const-assertions
    - Use const assertions and readonly (TypeScript)
  • idiom-rust-question-mark
    - Use ? for error propagation (Rust)
  • idiom-rust-iterator-chains
    - Use iterator chains when clearer (Rust)
  • idiom-python-comprehensions
    - Use comprehensions for simple transforms (Python)
  • idiom-go-error-handling
    - Handle errors immediately (Go)
  • idiom-prefer-language-builtins
    - Prefer language and stdlib builtins
  • idiom-ts-strict-types
    - 使用严格类型而非any(TypeScript)
  • idiom-ts-const-assertions
    - 使用const断言与readonly(TypeScript)
  • idiom-rust-question-mark
    - 使用?进行错误传播(Rust)
  • idiom-rust-iterator-chains
    - 在更清晰时使用迭代器链(Rust)
  • idiom-python-comprehensions
    - 使用推导式进行简单转换(Python)
  • idiom-go-error-handling
    - 立即处理错误(Go)
  • idiom-prefer-language-builtins
    - 优先使用语言与标准库内置功能

Workflow

工作流程

  1. Discover context: Read CLAUDE.md, lint configs, examine existing patterns
  2. Identify scope: Focus on recently modified code unless asked to expand
  3. Apply transformations: Use rules in priority order (CRITICAL first)
  4. Verify behavior: Ensure outputs, errors, and side effects remain identical
  5. Keep diffs minimal: Small, focused changes that are easy to review
  1. 发现上下文:阅读CLAUDE.md、代码检查配置,查看现有代码模式
  2. 确定范围:除非要求扩展,否则聚焦近期修改的代码
  3. 应用转换:按优先级顺序应用规则(先关键级)
  4. 验证行为:确保输出、错误与副作用完全一致
  5. 保持差异最小:小而聚焦的变更便于评审

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

参考文件

FileDescription
references/_sections.mdCategory definitions and ordering
assets/templates/_template.mdTemplate for new rules
metadata.jsonVersion and reference information
文件描述
references/_sections.md类别定义与排序
assets/templates/_template.md新规则模板
metadata.json版本与参考信息