简体   繁体   English

控件的命名约定

[英]Naming conventions for controls

While working with VS by default the editor generates this: 在默认情况下使用VS时,编辑器会生成以下内容:

<asp:Button ID="Button1" runat="server" Text="Button" />

Now the ID generated is Button1 ie First letter capital. 现在生成的ID是Button1即首字母大写。 My questions is what is the best way to name the controls ? 我的问题是命名控件的最佳方法是什么?

  • SubmitButton 提交按钮

  • btnSubmit btnSubmit按钮

or something else? 或者是其他东西?

Which naming convention is considered good ? 哪种命名约定被认为是好的?

btnSubmit is Hungarian notation which I don't personally consider good for UI programming since the type of control is evident directly through its declaration or intellisense in Visual Studio. btnSubmit是匈牙利表示法,我个人认为它不适用于UI编程,因为控件的类型直接通过它在Visual Studio中的声明或智能感知来证明。

SubmitButton is better. SubmitButton更好。 Generally it takes a form - Purpose-TypeOfControl 通常它需要一个表单 - Purpose-TypeOfControl

For example 例如

InputLabel
FirstNameLabel
LastNameLabel
CancelButton

This is a good reference: http://10rem.net/articles/net-naming-conventions-and-programming-standards---best-practices 这是一个很好的参考: http//10rem.net/articles/net-naming-conventions-and-programming-standards---best-practices

Update: Hungarian notation is also discouraged by stylecop/Fxcop. 更新:stylecop / Fxcop也不鼓励匈牙利表示法。 So its better to not to use entirely. 所以最好不要完全使用。

The number one reason is readability . 首要原因是可读性 Names should be readable and SubmitButton is far more readable than btnSubmit . 名称应该是可读的, SubmitButtonbtnSubmit更具可读性。

The style btnSubmit is called Hungarian notation. 样式btnSubmit称为匈牙利表示法。 One of the problems with it is that you have to keep inventing prefixes for a new type of control you create. 其中一个问题是你必须为你创建的新类型的控件继续发明前缀。 For example if you create a custom control called StocksTrendPlotter you will have to have variables such as stpGraph which is hardly readable. 例如,如果您创建一个名为StocksTrendPlotter ,则必须具有stpGraph等变量,这些变量几乎不可读。

Check out the disadvantages of the Hungarian notation. 查看匈牙利表示法缺点。

The main point of a convention is that it's something shared by other developers on your team, and those that might join the team at a later date. 一个约定的要点是,它是团队中其他开发人员共享的东西,以及可能在以后加入团队的东西。 While it's good to make sure you can read your own code 6 months, a year, two years later it's even more important that others can read your code when you've moved to another project. 虽然确保您可以在6个月,一年,两年后阅读自己的代码,但是当您转移到另一个项目时,其他人可以阅读您的代码更为重要。 So while there's debate about which convention is most useful/readable the key is to make sure it's applied throughout the team, and that new team members have a reasonable chance of being able to understand it when they're brought on board. 因此,尽管存在关于哪种约定最有用/可读的争论,关键是要确保它在整个团队中得到应用,并且新团队成员有合理的机会在他们加入时能够理解它。 Hungarian might not be pretty or particularly readable, but its very widespread and we're all familiar with it - even if we don't like it! 匈牙利人可能不是很漂亮或特别可读,但它非常普遍,我们都熟悉它 - 即使我们不喜欢它!

The "ux" or "ui" prefix is a common notation as it nicely groups all UI controls in intellisense. “ux”或“ui”前缀是一种常见的符号,因为它可以很好地对智能感知中的所有UI控件进行分组。 Also removing any indication for the control type (eg button) from the naming is useful should you swap control types at a later point eg uxSalutation which could change from textbox to combobox with limited impact. 如果你稍后交换控制类型,例如uxSalutation,它可能会从文本框变为有限影响的组合框,也可以从命名中删除控件类型(例如按钮)的任何指示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM