# 编程中的命名规范

编程中的命名规范是一套规则，用于选择标识符及相关名称的字符大小写、分词方式和前缀，这些规则并非由编程语言的语法所强制；它是一种句法层面的约束<sup>[1](https://ar5iv.labs.arxiv.org/html/1402.4182)</sup>。规范管束着 `userName`、`UserName`、`user_name` 和 `USER_NAME` 这类名称的写法，开发者们严格执行这些规范，大约三分之一的代码评审中都包含关于遵循规范的反馈<sup>[1](https://ar5iv.labs.arxiv.org/html/1402.4182)</sup>。

| 关键事实 | 详情 |
|---|---|
| 公认的大小写风格 | PEP 8 列出了小写、带下划线的小写、大写、带下划线的大写、首字母大写词（驼峰式）和混合大小写<sup>[2](https://peps.python.org/pep-0008/)</sup> |
| Python 的规定 | 函数、变量和模块用蛇形命名；类用首字母大写词；模块级常量如 `MAX_OVERFLOW` 用带下划线的大写<sup>[2](https://peps.python.org/pep-0008/)</sup> |
| C# 的规定 | 类型和公共成员用帕斯卡命名，参数和局部变量用驼峰命名，私有及内部非常量实例字段用下划线前缀；微软的指南建议避免连续两个下划线，因为这类标识符保留给编译器生成使用<sup>[3](https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/identifier-names)</sup> |
| Java 中的常量 | 接口常量应当、类 `final` 变量按惯例可以全部大写书写，词间以下划线分隔<sup>[4](https://docs.oracle.com/javase/specs/jls/se22/html/jls-6.html)</sup> |
| 执行缺口 | 在一项覆盖 1100 多名职业开发者的调查中，约半数组织并不严格执行方法命名标准<sup>[5](https://www.cs.kent.edu/~jmaletic/papers/ICSE2021.pdf)</sup> |
| 实测收益 | 完整单词的标识符名称使 72 名职业 C# 开发者理解语义缺陷的速度提高了 19%<sup>[6](https://www.se.cs.uni-saarland.de/publications/docs/HoSeHo17.pdf)</sup> |
| 评审压力 | 大约三分之一的代码评审包含关于遵循编码规范的反馈<sup>[1](https://ar5iv.labs.arxiv.org/html/1402.4182)</sup> |

## 常见的大小写风格与记法体系

各风格之间的机械差异在于词边界如何标记，因为标识符内部不允许有空格。技术上的考量促使程序员禁止在名称中使用空格和大多数标点；下划线是允许的，C 程序员常用它在一个名称中分隔多个语素，而 C 语言的一条相关惯例是函数、字段和变量只使用小写字母命名<sup>[7](https://andrewbegel.com/papers/ppig-naming.pdf)</sup>。

- **驼峰命名**（小驼峰、混合大小写）词间无分隔符，从第二个词起每个词首字母大写：`userName`。
- **帕斯卡命名**（首字母大写词、大驼峰）第一个词也首字母大写：`UserName`。
- **蛇形命名**用一个或多个下划线分隔词语：`user_name`<sup>[8](https://weiqin-zou.github.io/papers/APSEC21.pdf)</sup>。
- **大写下划线命名**结合大写字母与下划线分隔符，是常量的标准形态<sup>[9](https://google.github.io/styleguide/javaguide)</sup>。
- **烤肉串命名**被 Biome 的文件名规则接受为 [JavaScript](https://www.edgepedia.org/zh/javascript) 和 [TypeScript](https://www.edgepedia.org/zh/typescript) 文件的文件名风格<sup>[10](https://biomejs.dev/linter/rules/use-filenaming-convention/)</sup>。

**匈牙利命名法**是一种前缀体系，而非大小写风格。微软首席架构师[查尔斯·西蒙尼](https://www.edgepedia.org/zh/charles-simonyi)（Charles Simonyi）博士在 DOS 早期将其引入，作为一种给标识符加前缀以表明其功能类型的惯例；这个名称既来自前缀使名称看起来像外语，也来自西蒙尼的匈牙利出身<sup>[11](https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-6.0/aa260976(v=vs.60))</sup>。典型形式有 `strName`（字符串）、`iCount`（整数）和 `bFlag`（布尔值）<sup>[12](https://www.cs.huji.ac.il/labs/parallel/stud/Sam-MSc.pdf)</sup>。由于微软在大约 15 年间发布的大多数头文件和文档都使用匈牙利式名称，微软之外的许多程序员也采用了该方案的变体<sup>[11](https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-6.0/aa260976(v=vs.60))</sup>。在现代实践中它普遍不被鼓励：有了类型推断和静态分析，类型前缀被认为是冗余的或可能产生误导，尽管它在遗留代码库中仍然存在<sup>[12](https://www.cs.huji.ac.il/labs/parallel/stud/Sam-MSc.pdf)</sup>。

## 按语言和平台划分的规范

**Python。** PEP 8 规定函数、变量和模块名用蛇形命名（带下划线的小写），类用首字母大写词，模块级常量用全大写、下划线分隔的名称<sup>[2](https://peps.python.org/pep-0008/)</sup>。它还禁止使用单字符名称 `l`、`O` 和 `I`，因为在某些字体中它们与数字一和零难以区分，并建议在想用 `l` 时改用 `L`<sup>[2](https://peps.python.org/pep-0008/)</sup>。谷歌的 Python 指南遵从 PEP 8，要求模块名用 `lower_with_under.py`，将单字符名称限制于计数器、迭代器、异常标识符 `e`、`with` 语句中的文件句柄 `f` 以及受约束的类型变量，并且不鼓励在实例变量上使用双下划线（dunder）前缀，因为名称改写会影响可读性和可测试性，而且它并非真正的私有；它更倾向于单下划线<sup>[13](https://google.github.io/styleguide/pyguide.html)</sup>。

**Java。** 根据《Java 语言规范》，接口常量必须写成一个或多个词、首字母缩写或缩略语，全部大写并以下划线连接，类 `final` 变量传统上遵循同样的风格。<sup>[4](https://docs.oracle.com/javase/specs/jls/se22/html/jls-6.html)</sup> 谷歌的 Java 风格指南设定了标准：常量是存放深度不可变内容的静态 final 字段，其方法不含任何可检测的副作用；如果实例可观察状态的任何部分可能改变，就不符合条件，仅仅打算永不修改该对象是不够的。<sup>[9](https://google.github.io/styleguide/javaguide)</sup> 甲骨文的代码惯例建议避免单字符变量名，临时的一次性变量除外，整数常用 `i`、`j`、`k`、`m`、`n`，字符常用 `c`、`d`、`e`<sup>[14](https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html)</sup>。

**C#。** 微软的指南对类型名、命名空间和所有公共成员使用帕斯卡命名；对方法参数、局部变量以及私有和内部非常量字段使用驼峰命名；对私有和内部非常量实例字段使用下划线前缀<sup>[3](https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/identifier-names)</sup>。标识符必须以字母或下划线开头，并可包含 Unicode 字母、数字、连接符、组合符或格式字符<sup>[3](https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/identifier-names)</sup>。微软的命名指南指出标识符不应包含连续两个下划线字符，因为这些名称保留给编译器生成的标识符<sup>[3](https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/identifier-names)</sup>，并且惯例上接口名以大写 `I` 为前缀，特性类型名以单词 `Attribute` 结尾<sup>[3](https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/identifier-names)</sup>。

## 风格指南与执行

风格指南即使在精神上一致，细节上也有分歧。PEP 8 和谷歌的 Python 指南都要求首字母大写词中的首字母缩写全部大写，因此 `HTTPServerError` 优于 `HttpServerError`<sup>[2](https://peps.python.org/pep-0008/)</sup><sup> • </sup><sup>[13](https://google.github.io/styleguide/pyguide.html)</sup>。PEP 8 还指出，如果现有库使用了不同的风格，与该库的内部一致性优先于严格遵守本指南<sup>[2](https://peps.python.org/pep-0008/)</sup>。

执行一部分靠人，一部分靠自动化。一项覆盖 1100 多名职业开发者的调查发现，对于方法命名标准的重要性存在强烈共识，从业年限和编程语言对回答几乎没有影响，但所代表的组织中约有一半并不严格执行方法命名标准，许多开发者转而遵循非正式的标准<sup>[5](https://www.cs.kent.edu/~jmaletic/papers/ICSE2021.pdf)</sup>。关于规范遵循情况的研究发现，开发者严格执行这类规范，大约三分之一的代码评审包含相关反馈<sup>[1](https://ar5iv.labs.arxiv.org/html/1402.4182)</sup>。

**代码检查工具将规则编码为检查项。** [Pylint](https://www.edgepedia.org/zh/pylint) 检查名称是否匹配一组简单的正则表达式，例如变量名必须小写<sup>[1](https://ar5iv.labs.arxiv.org/html/1402.4182)</sup>。typescript-eslint 的 `naming-convention` 规则支持六种格式（驼峰、帕斯卡、蛇形、严格驼峰、严格帕斯卡、大写下划线）；严格驼峰不允许连续大写，因此 `myId` 有效而 `myID` 无效<sup>[15](https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/docs/rules/naming-convention.mdx)</sup>。其 leadingUnderscore 和 trailingUnderscore 选项接受六个值（allow、allowDouble、allowSingleOrDouble、forbid、require、requireDouble），使团队能够强制私有属性以 `_` 开头、全局级常量用大写字母书写，词间以下划线分隔<sup>[15](https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/docs/rules/naming-convention.mdx)</sup>。pep8-naming 是一个 flake8 插件，按照 PEP 8 命名规范检查 Python 代码<sup>[16](https://github.com/PyCQA/pep8-naming/blob/main/CHANGELOG.rst)</sup>。

## 数据说话

实证研究测量命名选择如何影响理解，但结论并不完全一致。

- **驼峰与下划线。** 一项针对 135 名程序员和非程序员的研究发现，无论受训背景如何，驼峰式在所有受试者中都带来更高的准确率，而且接受过驼峰式训练的受试者识别驼峰式标识符比下划线式更快<sup>[17](https://www.cs.loyola.edu/~binkley/papers/icpc09-clouds.pdf)</sup>。一篇 2024 年的重复与评析论文指出，文献中关于缩写对理解的影响存在相互矛盾的发现，并综述了判定驼峰式或蛇形命名孰优孰劣的各种尝试<sup>[18](https://ar5iv.labs.arxiv.org/html/2402.10022)</sup>。
- **完整单词与缩写。** 一项针对 72 名职业 C# 开发者的被试内实验发现，使用完整单词名称时，理解（语义缺陷检测）的速度比使用单字母和缩写时平均快 19%，单字母与缩写之间没有显著速度差异；语法错误检测不受命名风格影响<sup>[6](https://www.se.cs.uni-saarland.de/publications/docs/HoSeHo17.pdf)</sup>。一项更早的、让 100 多名程序员描述 12 个函数的研究也发现完整单词标识符带来最佳理解，但在许多情况下与缩写没有统计学差异，并且发现音节更少的标识符更容易记忆<sup>[19](https://link.springer.com/article/10.1007/s11334-007-0031-2)</sup>。
- **描述性名称。** 在一项基于网络的研究中，88 名 Java 开发者定位一个语义缺陷，使用描述性、较长的标识符名称时快了约 14%；该效应在语法错误上消失，在经验更丰富的开发者中明显，在经验较少的开发者中则不明显<sup>[20](https://dl.acm.org/doi/10.1145/3196321.3196332)</sup>。
- **误导性名称。** 一项针对 9 名职业开发者的对照实验比较了生产代码中原有的变量名与无意义的单字母，发现参数名对理解的影响大于局部变量，并且在 6 个方法中有 3 个两组之间没有显著差异，这归因于原始变量名质量差甚至具有误导性<sup>[21](https://dl.acm.org/doi/10.1109/ICPC.2017.27)</sup>。

从这项工作中得出的实用准则是：遵循编程语言自身的命名惯例，包括在类、接口、方法、变量和常量的名称中使用大小写以及名词和动词，并展开单字母名称<sup>[22](https://ppig.org/files/2017-PPIG-28th-hilton.pdf)</sup>。

## 标识符之外：文件与其他领域

命名规范延伸到了标识符之外。Biome 的 `useFilenamingConvention` 规则自 v1.5.0 起可用，强制 JavaScript 和 TypeScript 文件名采用驼峰、烤肉串、蛇形，或与文件中某个导出的名称相同；设置其 strictCase 选项后，`agentID` 会报错，应重命名为 `agentId`<sup>[10](https://biomejs.dev/linter/rules/use-filenaming-convention/)</sup>。同一规则记录了标识符规范未涵盖的框架特定文件名模式：[SvelteKit](https://www.edgepedia.org/zh/sveltekit) 和 Vike 中的前导加号，TanStack Start 中的前导美元符号，以及 Next.js、SolidStart、Nuxt 和 Astro 中的动态路由语法如 `[...slug].js`<sup>[10](https://biomejs.dev/linter/rules/use-filenaming-convention/)</sup>。

## 历史

三种传统塑造了今天的规范。在 C 语系中，下划线在小写名称内分隔语素，形成了蛇形命名<sup>[7](https://andrewbegel.com/papers/ppig-naming.pdf)</sup>。在驼峰命名的谱系中，《PARC Mesa 语言手册》（1979 年）包含一份编码标准，对大驼峰和小驼峰有具体规则，施乐帕克研究中心的 Mesa 库和 Alto 操作系统严格遵循了该标准<sup>[23](http://exampler.com/blog/2012/05/15/speculation-about-the-origin-of-camelcase/)</sup>。[匈牙利命名法](https://www.edgepedia.org/zh/hungarian-notation)作为第三条脉络，从微软 DOS 时代的头文件和文档中传播开来，大约使用了 15 年<sup>[11](https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-6.0/aa260976(v=vs.60))</sup>。

## 近期发展

若干有记录的变更发生在 2023 年之后。pep8-naming 的变更日志记载，N808 现在允许类型变量名以下划线开头，这是 PEP 8 命名规则对泛型类型变量执行方式的改变<sup>[16](https://github.com/PyCQA/pep8-naming/blob/main/CHANGELOG.rst)</sup>。Biome 的文件名规范规则在 v1.5.0 中推出<sup>[10](https://biomejs.dev/linter/rules/use-filenaming-convention/)</sup>。一篇 2024 年的重复与评析论文重新审视了上述命名实验<sup>[18](https://ar5iv.labs.arxiv.org/html/2402.10022)</sup>。还有一项关于大型语言模型对代码影响的研究报告称，在 C/C++ 代码库中，变量和函数名中蛇形命名的使用随时间呈上升趋势，而小写变量名下降，这一转变部分归因于大型语言模型<sup>[24](https://aclanthology.org/2026.findings-eacl.290.pdf)</sup>。

## References

1. [Learning Natural Coding Conventions (arXiv 1402.4182)](https://ar5iv.labs.arxiv.org/html/1402.4182)
2. [PEP 8 – Style Guide for Python Code](https://peps.python.org/pep-0008/)
3. [Identifier names – rules and conventions – C# | Microsoft Learn](https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/identifier-names)
4. [Chapter 6. Names, Java Language Specification, SE 22](https://docs.oracle.com/javase/specs/jls/se22/html/jls-6.html)
5. [On the Naming of Methods: A Survey of Professional Developers (ICSE 2021)](https://www.cs.kent.edu/~jmaletic/papers/ICSE2021.pdf)
6. [Shorter Identifier Names Take Longer to Comprehend (ICSE 2018)](https://www.se.cs.uni-saarland.de/publications/docs/HoSeHo17.pdf)
7. [Cognitive Perspectives on the Role of Naming in Computer Programs (PPIG)](https://andrewbegel.com/papers/ppig-naming.pdf)
8. [An Empirical Study on the Usage and Evolution of Identifier Styles (APSEC 2021)](https://weiqin-zou.github.io/papers/APSEC21.pdf)
9. [Google Java Style Guide](https://google.github.io/styleguide/javaguide)
10. [useFilenamingConvention | Biome](https://biomejs.dev/linter/rules/use-filenaming-convention/)
11. [Hungarian Notation | Microsoft Learn](https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-6.0/aa260976(v=vs.60))
12. [Comparing Identifier Naming Practices Between Chinese and American Developers (MSc thesis)](https://www.cs.huji.ac.il/labs/parallel/stud/Sam-MSc.pdf)
13. [Style guides for Google-originated open-source projects (Python)](https://google.github.io/styleguide/pyguide.html)
14. [Code Conventions for the Java Programming Language: 9. Naming Conventions](https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html)
15. [typescript-eslint naming-convention rule documentation](https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/docs/rules/naming-convention.mdx)
16. [pep8-naming CHANGELOG](https://github.com/PyCQA/pep8-naming/blob/main/CHANGELOG.rst)
17. [To CamelCase or Under_score (ICPC 2009)](https://www.cs.loyola.edu/~binkley/papers/icpc09-clouds.pdf)
18. [Reproducing, Extending, and Analyzing Naming Experiments (arXiv 2402.10022)](https://ar5iv.labs.arxiv.org/html/2402.10022)
19. [Effective identifier names for comprehension and memory (2007)](https://link.springer.com/article/10.1007/s11334-007-0031-2)
20. [Descriptive compound identifier names improve source code comprehension (ICPC 2018)](https://dl.acm.org/doi/10.1145/3196321.3196332)
21. [Effects of variable names on comprehension: an empirical study (ICPC 2017)](https://dl.acm.org/doi/10.1109/ICPC.2017.27)
22. [Hilton, P. and Hermans, F., PPIG 2017](https://ppig.org/files/2017-PPIG-28th-hilton.pdf)
23. [Speculation about the origin of camelCase, Exploration Through Example](http://exampler.com/blog/2012/05/15/speculation-about-the-origin-of-camelcase/)
24. [code_transformed: The Influence of Large Language Models on Code (EACL 2026 Findings)](https://aclanthology.org/2026.findings-eacl.290.pdf)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026; Oct 25, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Software engineering and development process › Software design and architecture › Titles Mu to Z

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/naming-convention-programming

English original: https://www.edgepedia.org/naming-convention-programming
