是否有变量命名约定的简明目录?
多年来我遇到过许多不同风格的变量名称。
当前关于命名约定的维基百科条目相当简单......
我希望看到变量命名约定的简明目录,通过名称/描述和一些示例来识别它。
如果某个公约特别受到某个平台社区的青睐,那也值得注意。
我正在将其转变为社区 wiki,因此请为每个约定创建一个答案,并根据需要进行编辑。
There are many different styles of variable names that I've come across over the years.
The current wikipedia entry on naming conventions is fairly light...
I'd love to see a concise catalog of variable naming-conventions, identifying it by a name/description, and some examples.
If a convention is particularly favored by a certain platform community, that would be worth noting, too.
I'm turning this into a community wiki, so please create an answer for each convention, and edit as needed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我见过的最好的命名约定集在《Code Complete” Steve McConnell 有一个关于命名约定和大量示例的精彩部分。 他的示例贯穿了不同语言的许多“最佳实践”,但最终由开发人员、开发经理或架构师来决定具体操作。
The best naming convention set that I've seen is in the book "Code Complete" Steve McConnell has a great section in there about naming conventions and lots of examples. His examples run through a number of "best practices" for different languages, but ultimately leave it up to the developer, dev manager, or architect to decide the specific action.
PEP8 是 Python 代码的风格指南,属于标准库的一部分在Python社区中是比较有影响力的。 额外的一点是,除了涵盖 Python 标准库使用的命名约定之外,它还提供了一般命名约定的概述。
根据 PEP8:标准库中的变量、实例变量、函数和方法应该使用小写单词,并用下划线分隔以提高可读性:
为了区分名称和保留字,请附加下划线:
弱私有名称(不由'from M import *') 以单个下划线开头。 通过名称修改强制实施隐私的名称以双下划线开头。:
Python“特殊”方法的名称以双下划线开头和结尾:
PEP8 is the style guide for Python code that is part of the standard library which is relatively influential in the Python community. For bonus points, in addition to covering the naming conventions used by the Python standard library, it provides an overview of naming conventions in general.
According to PEP8: variables, instance variables, functions and methods in the standard library are supposed to use lowercase words separated by underscores for readability:
To distinguish a name from a reserved word, append an underscore:
Names that are weakly private (not imported by 'from M import *') start with a single underscore. Names whose privacy is enforced with name mangling start with double underscores.:
Names that are Python 'special' methods start and end with double underscores:
Sun 在此处发布了 Java 变量名称约定列表。 该列表包括命名包、类、接口、方法、变量和常量的约定。
关于变量状态的部分
一些例子包括
Sun published a list of variable name conventions for Java here. The list includes conventions for naming packages, classes, interfaces, methods, variables, and constants.
The section on variables states
Some examples include
无论您的命名约定是什么,它通常都不允许您轻松区分:
我使用基于 不定冠词(a、an、some)用于局部变量,并且没有用于实例变量的冠词,如本问题中所述关于变量前缀。
所以一个前缀(无论是我的方式,还是列出的任何其他前缀 在这个问题中)可以是改进变量命名约定的一种方法。
我知道你的问题不限于变量,我只是想指出命名约定的那一部分。
Whatever your naming convention is, it usually do not allow you to easily distinguish:
I use a naming convention based on a the usage of indefinite article (a, an, some) for local variables, and no article for instance variables, as explained in this question about variable prefix.
So a prefix (either my way, or any other prefix listed in that question) can be one way of refining variable naming convention.
I know your question is not limited on variables, I just wanted to point out that part of naming convention.
有 Einar Hoest 撰写的“Java 程序员短语手册”。
他分析了方法名称的语法结构以及方法主体的结构,并收集了以下信息:
等等......从数百个开源项目中收集。
有关更多背景信息,请参阅他的 SLE08 论文。
There is, the "Java Programmers Phrasebook" by Einar Hoest.
He analysed the grammatical structure of method names together with the structure of the method bodies, and collected information such as:
etcetera ... collected from hundreds of open-source projects.
For more background information, please refer to his SLE08 paper.