code-simplifier

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Code Simplifier

代码简化器

A guide for simplifying and refining code while preserving functionality.
一份在保留功能的前提下简化和优化代码的指南。

When to Use

使用场景

  • Refactor, simplify code, clean up, code review
  • After completing a coding task or writing a logical chunk of code
  • Cleaning up complex PRs before review
  • Refactoring code for better readability and maintainability
  • 重构简化代码代码清理代码评审
  • 完成编码任务或编写完一段逻辑代码后
  • 在评审前清理复杂的PR
  • 重构代码以提升可读性和可维护性

Core Principles

核心原则

1. Preserve Functionality

1. 保留功能

Never change what the code does - only how it does it.
  • All original features, outputs, and behaviors must remain intact
  • Test before and after to verify no regressions
  • If unsure, ask before refactoring
绝不改变代码的功能——只优化实现方式。
  • 所有原有特性、输出和行为必须保持完整
  • 重构前后进行测试,确保没有回归问题
  • 若有疑问,先询问再进行重构

2. Apply Project Standards

2. 遵循项目标准

Follow established coding standards from project configuration (CLAUDE.md, .editorconfig, ESLint, etc.):
  • Consistent import sorting and organization
  • Preferred function styles (arrow vs function keyword)
  • Explicit type annotations where required
  • Component patterns and naming conventions
遵循项目配置中既定的编码标准(如CLAUDE.md、.editorconfig、ESLint等):
  • 统一的导入排序与组织方式
  • 首选的函数风格(箭头函数vs function关键字)
  • 必要时添加明确的类型注解
  • 组件模式与命名规范

3. Enhance Clarity

3. 提升清晰度

Simplify code structure by:
  • Reducing unnecessary complexity and nesting
  • Eliminating redundant code and abstractions
  • Improving readability through clear variable and function names
  • Consolidating related logic
  • Removing unnecessary comments that describe obvious code
Critical Rules:
  • Avoid nested ternary operators - prefer switch statements or if/else chains
  • Avoid overly compact one-liners - explicit code is often better
  • Choose clarity over brevity
通过以下方式简化代码结构:
  • 减少不必要的复杂度和嵌套
  • 消除冗余代码与抽象
  • 通过清晰的变量和函数名提升可读性
  • 整合相关逻辑
  • 删除描述显而易见代码的不必要注释
关键规则:
  • 避免嵌套三元运算符——优先使用switch语句或if/else链
  • 避免过度紧凑的单行代码——显式代码通常更优
  • 优先选择清晰度而非简洁性

4. Maintain Balance

4. 保持平衡

Avoid over-simplification that could:
  • Reduce code clarity or maintainability
  • Create overly clever solutions that are hard to understand
  • Combine too many concerns into single functions
  • Remove helpful abstractions that improve organization
  • Make the code harder to debug or extend
避免过度简化导致:
  • 降低代码清晰度或可维护性
  • 创造过于精巧、难以理解的解决方案
  • 将过多关注点合并到单个函数中
  • 删除有助于代码组织的实用抽象
  • 使代码更难调试或扩展

5. Focus Scope

5. 聚焦范围

Only refine code that has been recently modified or touched, unless explicitly instructed to review a broader scope.
仅优化最近修改或涉及的代码,除非明确要求评审更广泛的范围。

Refinement Process

优化流程

1. Identify the recently modified code sections
2. Analyze for opportunities to improve elegance and consistency
3. Apply project-specific best practices and coding standards
4. Ensure all functionality remains unchanged
5. Verify the refined code is simpler and more maintainable
6. Document only significant changes that affect understanding
1. 识别最近修改的代码段
2. 分析提升代码优雅性和一致性的机会
3. 应用项目特定的最佳实践和编码标准
4. 确保所有功能保持不变
5. 验证优化后的代码更简洁、更易于维护
6. 仅记录影响理解的重大变更

Refactoring Patterns

重构模式

See references/refactoring-patterns.md for common patterns.
常见模式请参考references/refactoring-patterns.md

Quick Checklist

快速检查清单

Before submitting refactored code:
  • All tests pass
  • No functionality changed
  • Code follows project standards
  • No nested ternaries introduced
  • Variable names are clear and descriptive
  • No unnecessary abstractions added
  • Comments updated if logic changed
  • Import statements organized
提交重构后的代码前:
  • 所有测试通过
  • 功能未发生变更
  • 代码遵循项目标准
  • 未引入嵌套三元运算符
  • 变量名称清晰且具有描述性
  • 未添加不必要的抽象
  • 若逻辑变更则更新注释
  • 导入语句已整理

Example Usage

示例用法

Trigger phrases:
  • "Simplify this code"
  • "Make this clearer"
  • "Refine this implementation"
  • "Clean up this function"
  • "Review this PR for readability"
Workflow:
  1. Write code → Complete feature
  2. Run tests → Verify functionality
  3. Apply code-simplifier → Improve clarity
  4. Run tests again → Confirm no regressions
  5. Submit PR → Ready for review
触发语:
  • "简化这段代码"
  • "让这段代码更清晰"
  • "优化这个实现"
  • "清理这个函数"
  • "评审这个PR的可读性"
工作流程:
  1. 编写代码 → 完成功能
  2. 运行测试 → 验证功能
  3. 应用代码简化器 → 提升清晰度
  4. 再次运行测试 → 确认无回归问题
  5. 提交PR → 等待评审

Done Criteria

完成标准

  • All tests pass after refactoring
  • No functionality changed
  • Code follows project standards
  • No nested ternaries or overly complex one-liners
  • 重构后所有测试通过
  • 功能未发生变更
  • 代码遵循项目标准
  • 无嵌套三元运算符或过于复杂的单行代码