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”). How can we differentiate (ID-wise), between the logic “UserName” and the view “UserName”. 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. “tbUserName” describes that it is a TextBox, “strUserName” defines that it is a string.
One idea would be to prefix all view related objects with “vwUserName” and keep the logic part as “UserName”. Does that开发者_开发知识库 make sense? What about when we have a situation where you have validators? Would you name them “vwUserNameRequiredValidator”, or “vwEmailAddressFormatValidator”? In that situation would you need to describe what it actually is? Would you give a .NET RequiredFieldValidator object an ID of “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... ;-)
I would use UserName for logic and userName for id.
I tend to use _userName as a variable and UserName as property in code behind, and as ID for a text box UserNameField. I find it easier to work with the intellisense in this way, instead of prefixing as we did in the VB-days with 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.
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
Checkbox - cbIsNameRequired
RequiredFieldValidator - rfvName
For variables:
Name
IsNameRequired
etc..
It is all about getting used to a pattern but always have a pattern..
精彩评论