有关于如何命名变量的教程吗?
正如您可能从我之前的帖子中看出的那样,我有可怕的命名约定。您知道有关如何命名事物的教程吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
正如您可能从我之前的帖子中看出的那样,我有可怕的命名约定。您知道有关如何命名事物的教程吗?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(12)
我建议您查看这本书
http://www.amazon.com/Code-Complete-Practical-Handbook-Construction/ dp/0735619670/ref=sr_1_1?s=books&ie=UTF8&qid=1281129036&sr=1-1
i will recommend to check this book
http://www.amazon.com/Code-Complete-Practical-Handbook-Construction/dp/0735619670/ref=sr_1_1?s=books&ie=UTF8&qid=1281129036&sr=1-1
我认为不会有任何好的教程,因为没有任何硬性规则。这里有一些提示:
符合约定:循环变量为
i
、j
和k
;*args
和**kwargs
中包含可变数量的参数;使用驼峰命名法或下划线命名法。保持一致。
要简洁。
list_of_drugs_used_in_this_program
比drugs
更不清楚。同样,您不需要在名称中包含变量的数据类型:drugs_list
是多余的。不要过度使用下划线。我从来不需要超过一个。 2+ 正在推动它。
永远不要永远使用元句法变量 (
foo
,spam
...) 在任何快速而肮脏的例子中。method1
也已过时。但你可以总结这一切:
别傻了。
嘻嘻。
http://www.oreillynet.com/onlamp/blog/2004/ 03/the_worlds_two_worst_variable.html
I don't think there will be any good tutorials, because there aren't any hard-and-fast rules. Here are some tips:
Conform to convention: Loop variables are
i
,j
, andk
; variable numbers of arguments go in*args
and**kwargs
; use camelCase or underscored_names.Be consistent.
Be concise.
list_of_drugs_used_in_this_program
is much less clear thandrugs
. Similarly, you don't need to include the datatype of the variable in the name:drugs_list
is redundant.Don't go overboard with the underscores. I've never needed more than one. 2+ is pushing it.
Never ever ever use metasyntactic variables (
foo
,spam
...) in anything but quick-and-dirty examples.method1
is also out.But you could summarise all of that with:
Don't be silly.
Tee hee.
http://www.oreillynet.com/onlamp/blog/2004/03/the_worlds_two_worst_variable.html
这是我唯一会声明为任何规则的两条。除此之外,约定的偏好是一个很快就会变成咆哮的意见问题。这篇文章的其余部分正是如此,不应被解读为其他内容。
Those are the only two I would state as any sort of rule. Beyond that convention preferences are a matter of opinions quickly turning into rants. The rest of this post is exactly that, and shouldn't be read as anything else.
这里的所有答案都是非常有效的。最重要的是:保持一致。
也就是说,这是我的规则 (C#):
发现这比
下划线
Public
属性开始带有大写字母
永远不应该碰以 a 开头的
下划线——例如,支持
属性的字段只能是
从财产上感动。如果我有
在别处强调,我知道我是
错误的
适当的——描述行的整数
ID也许可以被命名
rowSelected
、rowNextUnread
等等等。这不同于
匈牙利系统,这将标志着
它们作为整数,例如
iSelected
,iNextUnread
。匈牙利系统并没有增加太多,如果有的话,在哪里
Apps Hungarian 提供信息
type 没有:它告诉我添加
rowItemsPerPage
和colSelected
是无意义的操作,尽管
编译得很好。
All the answers here are quite valid. Most important: be consistent.
That said, here are my rules (C#):
find this much easier to read than
underscores
Public
properties startwith a capital letter
should never touch starts with an
underscore -- example, the backing
field to a property should only be
touched from the property. If I have
underscores elsewhere, I know I'm
wrong
appropriate -- ints describing row
IDs perhaps could be named
rowSelected
,rowNextUnread
, etcetera. This is different than
Systems Hungarian, which would mark
them as ints such as
iSelected
,iNextUnread
. Systems Hungariandoesn't add much if anything, where
Apps Hungarian gives information the
type doesn't: it tells me adding
rowItemsPerPage
andcolSelected
is ameaningless operation, even though it
compiles just fine.
不是教程...更像是指南/最佳实践之类的东西:
http://msdn.microsoft.com/en-us /library/xzf533w0(VS.71).aspx
Not a tutorial... more like a guide/best practice kind of thing:
http://msdn.microsoft.com/en-us/library/xzf533w0(VS.71).aspx
我建议购买一本 Robert C. Martin 所著的《Clean Code》。它充满了很好的建议,从命名约定到如何编写易于理解的函数等等。绝对值得一读。我知道自从阅读它以来它影响了我的编码风格。
I'd recommend purchasing a copy of "Clean Code" by Robert C. Martin. It is full of great suggstions ranging from naming conventions to how to write easy-to-understand functions and much more. Definitely worth a read. I know it influenced my coding style since reading it.
嗨,对我来说,您应该始终使用最明确的名称:
并尊重 pep8 风格指南。那么您的代码应该非常容易阅读。
hi for me you should always put the most explicit names:
and respect the pep8 style guide. You code should then be very easy to read.
关于命名约定的具体细节有许多不同的观点,但总体要点可以概括为:
所以一个主要的禁忌是
单字母变量(有些人
使用 i 和 j 进行索引循环,其中
没关系,因为每个程序员都知道
它们是什么。尽管如此,我还是更喜欢
'idx' 而不是 'i')。还出的是
像“method1”这样的名称,没有任何意义
- 它应该表明变量包含什么。
另一个(不太常见)约定是“匈牙利”表示法,其中数据类型以变量名称为前缀,例如“int i_idx”。这在现代面向对象的编程语言中毫无用处。更不用说公然违反 DRY 原则了。
第二点,一致性,也同样重要。驼峰式、大驼峰式,等等 - 只是不要无缘无故地在它们之间切换。
您会发现命名约定因语言而异,并且公司通常会有自己的命名规则。
正确命名变量是一项值得的投资,因为当您稍后维护代码并且忘记了所有内容的含义时,它将带来回报。
There are many different views on the specifics of naming conventions, but the overall gist could be summed up as:
So a major no-no would be
single letter variables (some people
use i and j for indexing loops, which
is OK because every programmer knows
what they are. Nevertheless, I prefer
'idx' instead of 'i'). Also out are
names like 'method1', it means nothing
- it should indicate what the variable holds.
Another (less common) convention is the 'Hungarian' notation where the data type is prefixed to the variable name such as 'int i_idx'. This is quite useless in modern, object oriented programming languages. Not to mention a blatant violation of the DRY principle.
The second point, consistency, is just as important. camelCase, UpperCamelCase, whatever - just don't switch between them for no reason.
You'll find that naming conventions vary from language to language and often, a company will have their own rules on naming.
Its a worthwhile investment to properly name your variables because when you come to maintain your code much later on and you have forgotten what everything means, it will pay dividends.
您没有指定您要查找的语言。由于您的问题被标记为 .NET,因此这是我在编写 C# 代码时遵循的文档: http://weblogs.asp.net/lhunt/pages/CSharp-Coding-Standards-document.aspx。
You didn't specify what language are you looking for. Since your question is flagged .NET, here is a document I follow when writing C# code: http://weblogs.asp.net/lhunt/pages/CSharp-Coding-Standards-document.aspx.
你读过《代码大全》吗?他在书中对此进行了完整的论述。这绝对是我在印刷品中见过的最好的命名策略...并且很容易找到大约 1000 名程序员将其命名为程序员和程序设计的 5 个顶级资源之一。
只是我的 0.05 美元
Have you read Code Complete? He does a full treatise on this in the book. Definitely the best naming strategy I've seen in print... And it's easy to find like 1000 programmers at the drop of a hat who name this one of the top 5 resources for programmers and program design.
Just my $.05
我可以为我的书“数据库设计的理智方法”中的“名称”章节制作一个无耻的插件吗?我专门谈论数据库中事物的名称,但大多数相同的考虑因素也适用于程序中的变量。
Can I make a shameless plug for the "Names" chapter in my book, "A Sane Approach to Database Design" ? I'm specifically talking about names for things in databases, but most of the same considerations apply to variables in programs.
目前尚不清楚您的问题是否与 Python 命名约定有关。
如果是这样,对于初学者来说,我会尝试遵循这些简单的规则:
ClassName
- 类名大写variable_name
- 变量小写和下划线(我尝试保留它们最多两个字)It's not clear if your question relates to Python naming conventions.
If so, for starters I would try to follow just these simple rules:
ClassName
- upper case for class namesvariable_name
- lower case and underscore for variables (I try to keep them at two words maximum)