the-art-of-naming
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseThe Art of Naming
命名的艺术
Naming things is hard. This guide provides a structured methodology for naming variables, functions, classes, interfaces, and everything in between — producing code that is self-documenting, consistent, and readable.
命名是一件很难的事。本指南为变量、函数、类、接口及所有代码元素的命名提供了一套结构化方法,帮助你写出自解释、统一且易读的代码。
When to Apply
适用场景
Reference these guidelines when:
- Naming new variables, functions, classes, interfaces, or types
- Reviewing code for naming consistency
- Refactoring code to improve readability
- Setting up linting rules for a new project
- Onboarding new team members to the codebase conventions
在以下场景参考这些指南:
- 为新变量、函数、类、接口或类型命名时
- 评审代码的命名一致性时
- 重构代码以提升可读性时
- 为新项目配置lint规则时
- 向新团队成员同步代码库约定时
Core Principles
核心原则
- S-I-D — Every name must be Short, Intuitive, and Descriptive
- No Contractions — , never
onItemClickonItmClk - Correct Casing — camelCase for members, PascalCase for types, UPPER_CASE for constants
- Meaningful Prefixes — for interfaces,
Ifor private,_for booleansis/has/should - No Context Duplication — , not
MenuItem.handleClick()MenuItem.handleMenuItemClick() - Structured Patterns — P/HC/LC for variables, A/HC/LC for functions
- Correct Action Verbs — (sync),
get(async),fetch(collection),remove(permanent)delete
- S-I-D — 每个命名都必须Short(简短)、Intuitive(直观)且Descriptive(具描述性)
- 禁止缩写 — 用,永远不要用
onItemClickonItmClk - 正确的大小写规范 — 成员用camelCase,类型用PascalCase,常量用UPPER_CASE
- 有意义的前缀 — 接口加前缀,私有成员加
I前缀,布尔值加_前缀is/has/should - 禁止上下文冗余 — 用,不要用
MenuItem.handleClick()MenuItem.handleMenuItemClick() - 结构化命名模式 — 变量采用P/HC/LC模式,函数采用A/HC/LC模式
- 正确的动作动词 — (同步操作)、
get(异步操作)、fetch(移除集合元素)、remove(永久删除)delete
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Rule | Impact | File |
|---|---|---|---|
| 1 | Casing Convention | CRITICAL | |
| 2 | S-I-D + No Contractions | CRITICAL | |
| 3 | Prefix Conventions | HIGH | |
| 4 | Boolean Naming | HIGH | |
| 5 | Context Duplication | HIGH | |
| 6 | Function Naming (A/HC/LC) | HIGH | |
| 7 | Variable Naming (P/HC/LC) | MEDIUM | |
| 优先级 | 规则 | 影响程度 | 对应文件 |
|---|---|---|---|
| 1 | 大小写规范 | 严重 | |
| 2 | S-I-D原则 + 禁止缩写 | 严重 | |
| 3 | 前缀约定 | 高 | |
| 4 | 布尔值命名 | 高 | |
| 5 | 上下文冗余 | 高 | |
| 6 | 函数命名(A/HC/LC模式) | 高 | |
| 7 | 变量命名(P/HC/LC模式) | 中等 | |
Quick Reference
快速参考
1. Casing Convention (CRITICAL)
1. 大小写规范(严重级)
- - camelCase for variables/functions, PascalCase for classes/enums/types, UPPER_CASE for exported constants
naming-casing-convention
- - 变量/函数用camelCase,类/枚举/类型用PascalCase,导出常量用UPPER_CASE
naming-casing-convention
2. S-I-D + No Contractions (CRITICAL)
2. S-I-D原则 + 禁止缩写(严重级)
- - Names must be Short, Intuitive, Descriptive — never use contractions
naming-sid
- - 命名必须简短、直观、具描述性 — 永远不要使用缩写
naming-sid
3. Prefix Conventions (HIGH)
3. 前缀约定(高优先级)
- -
naming-prefix-conventionfor interfaces,Ifor private members,_/T/R/U/Vfor genericsK
- - 接口加
naming-prefix-convention前缀,私有成员加I前缀,泛型用_/T/R/U/V作为前缀K
4. Boolean Naming (HIGH)
4. 布尔值命名(高优先级)
- - Prefix booleans with
naming-boolean/is/has/should, keep names positivecan
- - 布尔值加
naming-boolean/is/has/should前缀,保持命名表述为肯定形式can
5. Context Duplication (HIGH)
5. 上下文冗余(高优先级)
- - Don't repeat class/component name in member names
naming-context-duplication
- - 不要在成员命名中重复类/组件的名称
naming-context-duplication
6. Function Naming (HIGH)
6. 函数命名(高优先级)
- - A/HC/LC pattern + correct action verbs (get/set/fetch/remove/delete/compose/handle)
naming-function-pattern
- - 采用A/HC/LC模式 + 正确的动作动词(get/set/fetch/remove/delete/compose/handle)
naming-function-pattern
7. Variable Naming (MEDIUM)
7. 变量命名(中优先级)
- - P/HC/LC pattern for structured, predictable variable names
naming-variable-pattern
- - 采用P/HC/LC模式保证变量命名结构化、可预测
naming-variable-pattern
How to Use
使用方式
Read individual rule files for detailed explanations and code examples:
rules/naming-casing-convention.md
rules/naming-sid.md
rules/naming-prefix-convention.md
rules/naming-boolean.md
rules/naming-context-duplication.md
rules/naming-function-pattern.md
rules/naming-variable-pattern.mdEach rule file contains:
- Brief explanation of why it matters
- Incorrect code examples with explanation
- Correct code examples with explanation
- Summary tables and references
阅读各规则文件获取详细说明和代码示例:
rules/naming-casing-convention.md
rules/naming-sid.md
rules/naming-prefix-convention.md
rules/naming-boolean.md
rules/naming-context-duplication.md
rules/naming-function-pattern.md
rules/naming-variable-pattern.md每个规则文件包含:
- 规则重要性的简要说明
- 带解释的错误代码示例
- 带解释的正确代码示例
- 汇总表格和参考资料
Full Compiled Document
完整编译文档
For the complete guide with all rules expanded:
AGENTS.md如需查看所有规则展开的完整指南,参见:
AGENTS.md