code-style
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCode Style: Mirror, Reuse, Symmetry
代码风格:镜像、复用与对称
When writing new code in an existing codebase, follow these principles:
- 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.
- 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.
- 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.
- Maintain symmetry: If adding parallel to
fooB(), ensure naming, parameter order, and structure are symmetric. RenamefooA()if needed.fooA() - 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.
在已有代码库中编写新代码时,请遵循以下原则:
- 完全镜像现有代码风格:先阅读周边代码,确保大括号风格、注释风格、命名规范、空行间距、代码密度以及细节程度完全匹配。
- 复用现有模式:找到最相似的功能模块,复用其结构(方法拆分、控制流、注解、守卫子句)。如果现有模式适用,请勿引入新模式。
- 复用现有代码:在编写新的辅助方法前,先检查是否有可复用或可泛化的现有方法。若确实需要新的辅助方法,请参照最相近的同类方法进行编写。
- 保持对称性:如果添加与对应的
fooA(),请确保命名、参数顺序和结构保持对称。必要时可重命名fooB()。fooA() - 逻辑排序:新方法、switch分支和字段的位置应与现有代码的分组或业务生命周期保持一致,而不是简单地追加到末尾。