txtFirstName 与 FirstNameTxt
在过去的几年中,当我引用“名字”字段的文本框控件时,我一直使用 FirstNameTxt
的命名约定。但是,我注意到大多数其他开发人员倾向于使用命名约定 txtFirstName
哪个是最好的约定?为什么它比其他方法更受青睐?
Over the past several years I have been using the naming convention of FirstNameTxt
when I'm referring to a TextBox Control for a 'First Name' field. However, I've noticed the majority of other developers tend to use the naming convention txtFirstName
Which is the best convention to use? and why is it preferred over the other method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
一个并不比另一个更好。最重要的是无论您选择哪种约定,都要保持一致。
由于过去几年你一直在使用当前的惯例,我不会改变,因为那样你就会打破惯例。当然,除非您选择返回并为新约定重构所有现有代码。但这引出了一个问题“为什么要麻烦?”。
One isn't better than the other. The most important thing is to be consistent in whichever convention you choose.
Since you have been using your current convention for the past several years, I wouldn't change because then you will have broken convention. Unless, of course, you choose to go back and refactor all of your existing code for the new convention. But that begs the question "why bother?".
我不使用匈牙利表示法。如果我无法记住表单上控件的确切名称,那么我就做错了。最有可能的是表单类做得太多并且有多个职责。
为了解决这个问题,我通常使用用户控件将表单分成几个小节,并在它们之间干净地分离关注点。作为额外的好处,代码变得更容易测试。
I don't use Hungarian notation. If I have trouble remembering the exact names of controls on my form, then I'm doing something wrong. Most likely the form class is doing too much and has several responsibilities.
To fix that I usually split the form into subsections using user controls and cleanly separate concerns across them. As an added bonus the code gets easier to test.
我通常使用“ux”作为“User eXperience”作为控件名称的前缀。这使得在保持相同名称的同时更容易切换控件类型。由于控件具有共享的继承层次结构,因此通常可以从一个控件切换到另一个控件,例如从标签切换到文本框,而无需将前缀从“lbl”更改为“txt”。它还将列表中的所有控件分组在一起,这样我就不必记住我正在寻找的控件的类型。
I generally use "ux" for "User eXperience" as a prefix for control names. This makes it easier to switch control types while maintaining the same name. Since controls have a shared inheritance hierarchy, it's often possible to switch from one to another, for example from a label to a textbox without changing the prefix from "lbl" to "txt". It also groups all controls together in the list so I don't have to remember the type of control I'm looking for.
.NET Framework 指南建议第一个字符为小写。例如“txtFirstName”或“awesomeVariable”等
http ://msdn.microsoft.com/en-us/library/x2dbyw72(v=VS.71).aspx
请参阅此建议列表。 :)
The .NET Framework guidelines recommends that the first character is lower case. Like "txtFirstName" or "awesomeVariable" etc.
http://msdn.microsoft.com/en-us/library/x2dbyw72(v=VS.71).aspx
See this list of recommendations. :)
从我对同一主题所做的研究来看,似乎没有真正的共识或最佳实践。人们会争辩说它应该是 txtFirstName、FirstName 和 FirstNameTxt。我的建议是,只要保持一致就可以了。没有什么比名字互换时编码更糟糕的了。
顺便说一句,我使用 txtFirstName。
From the research I did on the same subject there seems to be no true consensus or best practice. People will argue that it should be txtFirstName, FirstName, and FirstNameTxt. My suggestions is that as long as its consistent then you are fine. Nothing is worse than coding when the names switch around.
As an aside I use txtFirstName.
从 Visual Studio 2010 开始,完全没有理由使用匈牙利表示法。
人们可以想象的唯一原因 - 使用智能感知轻松搜索类似的控件,会随着以全文搜索的新智能感知而消失。
因此,在代码编辑器中输入
Text
会找到所有内容:firstNameText
、lastNameText
、anyText
。而且在代码中读取“firstNameText”比读取“txtFirstName”要容易得多。
Since Visual Studio 2010, there is absolutely no reason to use Hungarian Notation.
The only reason one could imagine - to easily search for similar controls with intellisense, goes away with new intellisense which searches in full text.
Thus typing
Text
in code editor finds you all:firstNameText
,lastNameText
,anyText
.And it's much easier to read "firstNameText" than "txtFirstName" in code.
我喜欢使用匈牙利表示法 - 前面加上 3 个字母的缩写 - 如果出于任何其他原因,所有文本框控件都会在页面的控件列表中分组在一起。
I like using the Hungarian Notation - prepending with a 3-letter abbrev - if for any other reason, all the textbox controls are grouped together in the list of controls for the page.