code-style

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Code Style: Mirror, Reuse, Symmetry

代码风格:镜像、复用与对称

When writing new code in an existing codebase, follow these principles:
  1. Mirror the surrounding code exactly: Match brace style, comment style, naming conventions, blank line spacing, code density, and level of detail by reading nearby code first.
  2. Reuse existing patterns: Find the closest analogous feature and replicate its structure (method decomposition, control flow, annotations, guard clauses). Don't introduce new patterns when an existing one fits.
  3. Reuse existing code: Before writing a new helper, check if an existing method can be reused or generalized. If a new helper is needed, model it after its closest sibling.
  4. Maintain symmetry: If adding
    fooB()
    parallel to
    fooA()
    , ensure naming, parameter order, and structure are symmetric. Rename
    fooA()
    if needed.
  5. Logical ordering: Place new methods, switch cases, and fields in the order that mirrors the existing grouping or business lifecycle, not just appended at the end.
在已有代码库中编写新代码时,请遵循以下原则:
  1. 完全镜像现有代码风格:先阅读周边代码,确保大括号风格、注释风格、命名规范、空行间距、代码密度以及细节程度完全匹配。
  2. 复用现有模式:找到最相似的功能模块,复用其结构(方法拆分、控制流、注解、守卫子句)。如果现有模式适用,请勿引入新模式。
  3. 复用现有代码:在编写新的辅助方法前,先检查是否有可复用或可泛化的现有方法。若确实需要新的辅助方法,请参照最相近的同类方法进行编写。
  4. 保持对称性:如果添加与
    fooA()
    对应的
    fooB()
    ,请确保命名、参数顺序和结构保持对称。必要时可重命名
    fooA()
  5. 逻辑排序:新方法、switch分支和字段的位置应与现有代码的分组或业务生命周期保持一致,而不是简单地追加到末尾。