是否有变量命名约定的简明目录?

发布于 2024-07-07 16:40:48 字数 195 浏览 11 评论 0原文

多年来我遇到过许多不同风格的变量名称。

当前关于命名约定的维基百科条目相当简单......

我希望看到变量命名约定的简明目录,通过名称/描述和一些示例来识别它。

如果某个公约特别受到某个平台社区的青睐,那也值得注意。

我正在将其转变为社区 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

埋情葬爱 2024-07-14 16:40:48

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.

情归归情 2024-07-14 16:40:48

PEP8 是 Python 代码的风格指南,属于标准库的一部分在Python社区中是比较有影响力的。 额外的一点是,除了涵盖 Python 标准库使用的命名约定之外,它还提供了一般命名约定的概述。

根据 PEP8:标准库中的变量、实例变量、函数和方法应该使用小写单词,并用下划线分隔以提高可读性:

foo
parse_foo
a_long_descriptive_name

为了区分名称和保留字,请附加下划线:

in_
and_
or_

弱私有名称(不由'from M import *') 以单个下划线开头。 通过名称修改强制实施隐私的名称以双下划线开头。:

_some_what_private
__a_slightly_more_private_name

Python“特殊”方法的名称以双下划线开头和结尾:

__hash__  # hash(o) = o.__hash__()
__str__   # str(o) = o.__str__()

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:

foo
parse_foo
a_long_descriptive_name

To distinguish a name from a reserved word, append an underscore:

in_
and_
or_

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.:

_some_what_private
__a_slightly_more_private_name

Names that are Python 'special' methods start and end with double underscores:

__hash__  # hash(o) = o.__hash__()
__str__   # str(o) = o.__str__()
蘸点软妹酱 2024-07-14 16:40:48

Sun 在此处发布了 Java 变量名称约定列表。 该列表包括命名包、类、接口、方法、变量和常量的约定。

关于变量状态的部分

<块引用>

除了变量之外,所有实例、类和类常量都是大小写混合且首字母小写。 内部单词以大写字母开头。 变量名称不应以下划线 _ 或美元符号 $ 字符开头,即使两者都是允许的。

变量名称应该简短而有意义。 变量名的选择应该是助记性的——也就是说,旨在向不经意的观察者表明其使用的意图。 除临时“一次性”变量外,应避免使用单字符变量名。 临时变量的通用名称是 i、j、k、m 和 n(表示整数); c、d 和 e 表示字符。

一些例子包括

int             i;
char            c;
float           myWidth;

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

Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital letters. Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed.

Variable names should be short yet meaningful. The choice of a variable name should be mnemonic- that is, designed to indicate to the casual observer the intent of its use. One-character variable names should be avoided except for temporary "throwaway" variables. Common names for temporary variables are i, j, k, m, and n for integers; c, d, and e for characters.

Some examples include

int             i;
char            c;
float           myWidth;
茶色山野 2024-07-14 16:40:48

无论您的命名约定是什么,它通常都不允许您轻松区分:

  • 实例(通常是私有)变量
  • 局部变量(用作参数或函数的本地变量)

我使用基于 不定冠词(a、an、some)用于局部变量,并且没有用于实例变量的冠词,如本问题中所述关于变量前缀

所以一个前缀(无论是我的方式,还是列出的任何其他前缀 在这个问题中)可以是改进变量命名约定的一种方法。

我知道你的问题不限于变量,我只是想指出命名约定的那一部分。

Whatever your naming convention is, it usually do not allow you to easily distinguish:

  • instance (usually private) variable
  • local variables (used as parameters or local to a function)

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.

淡水深流 2024-07-14 16:40:48

有 Einar Hoest 撰写的“Java 程序员短语手册”

他分析了方法名称的语法结构以及方法主体的结构,并收集了以下信息:

add-[名词]-* 这些方法通常具有参数并创建对象。
它们很少返回字段值。
这句话几乎出现在所有
应用程序。

等等......从数百个开源项目中收集。

有关更多背景信息,请参阅他的 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:

add-[noun]-* These methods often have parameters and create objects.
They very rarely return field values.
The phrase appears in practically all
applications.

etcetera ... collected from hundreds of open-source projects.

For more background information, please refer to his SLE08 paper.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文