简体   繁体   English

ASP.NET中的控件/变量命名约定

[英]Control/variable naming conventions in ASP.NET

I think the main distinction in naming is between logic and view-related objects. 我认为命名的主要区别在于逻辑和与视图相关的对象之间。 You might have a variable named “ UserName ” in the code behind file of a page, but then also a .NET TextBox in which a user is supposed to enter their username (a sensible ID would also be “ UserName ”). 您可能在页面文件后面的代码中有一个名为“ UserName ”的变量,但还有一个.NET TextBox,用户应在其中输入其用户名(明智的ID也应为“ UserName ”)。 How can we differentiate (ID-wise), between the logic “ UserName ” and the view “ UserName ”. 我们如何在逻辑“ UserName ”和视图“ UserName ”之间区分(ID方式)。 My question is, what is a sensible distinction to make when coming up with these names? 我的问题是,提出这些名称时有什么明智的区分?

In my opinion, a variable name/control ID should never describe what it IS, only what it DOES. 在我看来,变量名/控件ID绝不应该描述它的含义,而只是描述它的作用。 tbUserName ” describes that it is a TextBox, “ strUserName ” defines that it is a string. “tbUserName”描述,它是一个文本框,“strUserName中 ”定义,它是一个字符串。

One idea would be to prefix all view related objects with “ vwUserName ” and keep the logic part as “ UserName ”. 一种想法是在所有与视图相关的对象之前加上“ vwUserName ”,并将逻辑部分保留为“ UserName ”。 Does that make sense? 那有意义吗? What about when we have a situation where you have validators? 当我们遇到您有验证人的情况时该怎么办? Would you name them “ vwUserNameRequiredValidator ”, or “ vwEmailAddressFormatValidator ”? 您将它们命名为“ vwUserNameRequiredValidator ”还是“ vwEmailAddressFormatValidator ”? In that situation would you need to describe what it actually is? 在这种情况下,您需要描述它的实际含义吗? Would you give a .NET RequiredFieldValidator object an ID of “ rfvUserName ”? 您是否将.NET RequiredFieldValidator对象的ID设置为“ rfvUserName ”?

I really want to get an idea of what other people think on this, because I want to come up with a sensible and consistent naming convention system going forward. 我真的很想了解其他人对此的看法,因为我想提出一个明智且一致的命名约定系统。 I'm interesting to hear arguments for any type of system. 我很高兴听到任何类型的系统的论点。

Hungarian notation is so 1990s... ;-) 匈牙利书写法是1990年代... ;-)

I would use UserName for logic and userName for id. 我将UserName用于逻辑,将userName用于id。

I tend to use _userName as a variable and UserName as property in code behind, and as ID for a text box UserNameField . 我倾向于将_userName用作变量,并将UserName用作后面代码中的属性,并用作文本框UserNameField的 ID。 I find it easier to work with the intellisense in this way, instead of prefixing as we did in the VB-days with txtUserName. 我发现以这种方式使用智能感知更容易,而不是像在VB-day中使用txtUserName那样进行前缀。

Edit: not calling it UserNameTextBox, but UserNameField is also easier to work with if you want to the exchange the field (that is TextBox) to another control (it may not apply to this example of username though. 编辑:不调用它UserNameTextBox,但如果要将字段(即TextBox)交换到另一个控件(它可能不适用于此用户名示例),则UserNameField也更易于使用。

For controls , I always add a prefix like: 对于控件 ,我总是添加如下前缀:

Textbox - txtName - I don't use tb because it can be confusing since I use tbl for HtmlTable 文本框-txtName-我不使用tb,因为我将tbl用于HtmlTable可能会造成混淆

Checkbox - cbIsNameRequired 复选框-cbIsNameRequired

RequiredFieldValidator - rfvName RequiredFieldValidator-rfvName

For variables: 对于变量:

Name 名称

IsNameRequired IsNameRequired

etc.. 等等..

It is all about getting used to a pattern but always have a pattern.. 这都是关于习惯一种模式,但总是有一种模式。

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

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