可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
We are in the process of nutting out the design guidelines we would like to use in our development team and got into a discussion today around how ASP.NET controls should be named. I am talking about our good friends Label, TextBox, Button etc.
We came up with the following three possibilities that we voted on: (Example is a TextBox to enter/display a FirstName)
- Add the control type as a postfix to your controls ID: [FirstName
_
TextBox] or [FirstName_
tbx]
- Add the control type as a prefix to your controls ID [tbxFirstName]
- Set the ID of the control to FirstName and name related fields (like a label for the textbox or a validator) as in option 2 [lblTextBox].
We ended up deciding to use option 2. It's not as verbose as option 1 and I like that it specifies what control it is before the name of the control.
My question is whether Microsoft has released any guidelines for these prefixes and or if you have any comments about our decision.
回答1:
The reason Visual Studio adds "TextBox1" when you add it to the page is because Microsoft has no way of knowing how you intend to use it. Naming it "Control1" would be too confusing because it could be any number of controls.
Microsoft provides guidance in general for OO naming conventions, but not specifically for naming UI controls. Since UI controls are ultimately variables used in code, they should follow the same convention as any other variable - no hungarian notation prefix.
- msdn.microsoft.com/en-us/library/xzf533w0(vs.71)
- msdn.microsoft.com/en-us/library/ms229002(VS.80)
The main reasons are...
- Type of control may change from textbox to listbox, then all associated code will have to be fixed (noted earlier)
- Your code should be more concerned with the content of the control and less with what type of control it is. When you are concerned with the type of the control, you start to depend on certain functionalities and you break encapsulation - you should be able to easily swap controls without changing much or any code. (Basic OOP principle)
- It is fairly easy to come up with prefixes for the standard controls, but new controls are being developed every day. You may make your own WebUserControl, or you may purchase a set of third party controls. How will you decide which prefix to use for the custom controls? Instead of focusing on the type of control, your code should be concerned with what information is contained in it.
Examples
- txtFirstName => firstName or FirstName
- txtState => state or State
- cboState => state or State (prime example of changing control types what about lstState or rdoState - they should all have the same name because your code is not concerned about the type of control,rather the state the user selected)
- ctlBilling => billingAddress or BillingAddress (custom control - with hungarian notation it is not very evident what the control even is, but with a meaningful name I begin to understand the information contained in it. i.e. billingAddress.Street, billingAddress.FullAddress etc.)
回答2:
Not sure about Microsoft official standards, but this is what i've done through out my development career.
I generally abbreviate the control type in front of the the name of what the control does.
I keep the abbreviation lower case and the control's name CamelCase.
E.g. A texbox for username becomes tbUserName
Here is a list of standard abbreviations I use:
Abbr - Control
btn - Button
cb - CheckBox
cbl - CheckBoxList
dd - DropDownList
gv - GridView
hl - Hyperlink
img - Image
ib - ImageButton
lbl - Label
lbtn - LinkButton
lb - ListBox
lit - Literal
pnl - Panel
ph - PlaceHolder
rb - RadioButton
rbl - RadioButtonList
txt - Textbox
回答3:
I find that most of the time I care about what kind of information the control is for rather than what control type is currently being used to capture that data, so I prefer the type of information before the control type, so I can find it in a sorted list in the IDE:
- AgeRangeDropDownList
- AgreedToTermsCheckBox
- FirstNameTextBox
- LastNameTextBox
VS:
- chkAgreedToTerms
- ddlAgeRange
- txtFirstName
- txtLastName
回答4:
Microsoft does provide some guidance here.
When you drag a control onto a web form you get something like "TextBox1" automatically. That's the IDE telling you that you should change the "1" part for your specific needs.
In that case, "TextBoxFirstName" seems like the way to go.
回答5:
Two reasons why I prefer option 1:
- FirstNameTextBox more closely matches my business object.
- More usable with IntelliSense.
Having said that I am considering changing to FirstNameCtrl for the reason csgero pointed out about changing control types. So why bother with any postfix or prefix, to reduce/remove the posibility of conflicts with asp/win form properties.
回答6:
I'm not sure of the guidelines regarding ASP.NET, but in the book Framework Design Guidelines from Microsoft, there are several best-practice guidelines about naming of class members. Since ASP.NET Controls in most cases result in a protected field of the appropriate type, I consider these naming guidelines to apply for ASP.NET controls as well. In fact Code Analysis does not differentiate on Control reference fields and other fields.
These guidelines recommend using a naming scheme that implies the logical use rather than a type-descriptive variant. There are several reasons for this. The prefix is implies a type to the developer that might not be correct due to later changes. It adds an extra step in code maintainence. If you change your Button control into a LinkButton control the name also needs to be changed to correct the prefix.
For that reason I would call the control FirstNameEdit etc...
回答7:
I think it is better to use option 1 because it is easy to find the field by its meaning and its usage to understand programming coding down the road . Also,it is more usable with IntelliSense to find where we use this field for in our programming code. Therefore I can find the right control by the name of the meaningful field. I will not remmember what kind of control I use for this field but I can find this field by using the meaningful of field name instead of the type of control example I want to find "City" control , I just typ "City" , Intellisence will show me all information for this control but if I do not remember what kind of control I use for , I do not know what to begin....
回答8:
Abbreviation || ASP.NET Control
STANDARD CONTROLS:
btn Button
cb CheckBox
cbl CheckBoxList
ddl DropDownList
fu FileUpload
hdn HiddenField
lnk Hyperlink
img Image
ibtn(btn) ImageButton
lbl Label
lbtn(btn) LinkButton
lb ListBox
lit Literal
mv MultiView
pnl Panel
ph PlaceHolder
rb RadioButton
rbl RadioButtonList
tbl Table
txt TextBox
v View
DATA CONTROLS
dtl DataList
dp DataPager
dtv DetailsView
ets EntityDataSource
fv FormView
gv GridView
lds LinqDataSource
lv - ListView
ods ObjectDataSource
qe QueryExtender
rpt Repeater
smd SiteMapDataSource
sds SqlDataSource
xds XmlDataSource
VALIDATION CONTROLS
cpv CompareValidator
ctv CustomValidator
rv RangeValidator
rev RegularExpressionValidator
rfv RequiredFieldValidator
vs ValidationSummary
VALIDATION CONTROLS:
cpv // CompareValidator
ctv CustomValidator
rv RangeValidator
rev RegularExpressionValidator
rfv RequiredFieldValidator
回答9:
We also use number 2, however I'm not totally convinced it is a good approach. It is Hungarian notation from the "bad" kind, meaning that the prefixes signal the type (syntax) and not the purpose (semantics). The problem with this is that what starts as a TextBox might later become a DropDown and then a RadioButtonGroup, and you'll have to rename the control each time.
回答10:
Almost everyone uses Hungarian-style prefixes (option 2).
Semantic naming is less useful because "Firstname" is actually the texbox.Text value, not the textbox itself.
回答11:
I tend to go with the control type as a prefix and the name of the control afterwards but I always CamelCase so in your example for different types of controls you might have..
- TxbFirstName
- DdFirstName
- ChbFirstName
For intellisense reasons I also always fully qualify the name of the control so I wouldn't do any of the following...
- TxbFName
- TxbClientNo
- TxbNoOfCpn
But ultimately down to personal preference
回答12:
I've been struggling with this problem too. I used to use the "hungarian style prefix".
Now I take a different approach, I try to see the controls as private fields from my class. I don't pre- of postfix my private fields with their type, so why should I do that to an TextBox?
So what used to be:
var newCustomer = new Customer();
newCustomer.Name = txtName.Value;
newCustomer.Address = txtAddress.Value;
newCustomer.City = txtCity.Value;
newCustomer.HasEnoughMoney = cbHasMoney.Selected;
Becomes:
var newCustomer = new Customer();
newCustomer.Name = name.Value;
newCustomer.Address = address.Value;
newCustomer.City = city.Value;
newCustomer.HasEnoughMoney = hasMoney.Selected;
To be honest, I couldn't care less if the "name" control is a text box or what else, I just want it's value.
And if it's not clear enough if your talking about your control or about another field/variable, I think you should either reconsider the name of that field/variable or the function of you class (meaning it might be a little bit to big?).
回答13:
If you look at it from a code maintainance point of view what would be the best notation to look at after you did the code 2 years ago. Although we try to ensure that forms don't have too many fields on them we all know that this sometimes happens. If we used the Hungarian type notation by prepending the control type I think it would be easier to see where that value is coming from instead of having to figure it out in the event the variable name doesn't make it obvious. If you are using any type of Refactoring tool then changing the name of the control would change the code automatically thereby reducing the change control arguement.
回答14:
I don’t think there’s a right or wrong answer here, whatever you decide, I think the most important aspect is just to be consistent when it actually comes to coding.
回答15:
I use uxCity so that you know it's definitely the User Interface control and not some other object but you don't need to change it if you go from a TextBox to a DropDownList.
However If I had a DropdownList and a Textbox, I'd need to use dlCity and txtCity
Or I'd use a combo cboCity.
Hungarian notation was de rigeur when you were limited to 8-character names and no intellisense or debug highlighting. It was a discipline and you could see that if the coding style was correct, the code was likely to be correct. It was used on variables too so you could read the code and understand it as it was a DIY type enforcement.
However, I do use CityTextbox, CityTextboxLabel
CityUx, CityUxLbl
It all depends on who is setting the standards on the project.
回答16:
I found the only reason to use Hungarian notation was that the IDE did not have intellisense and it was not easy to figure out what was what so iCounter was an integer
However the time of using brief to develop is long gone and the IDE shows you the info in a second
Then you inherit VB.NET code and it is not case sensitive so what do you do?
lblFirstName, txtFirstName
One is a label the other is the textbox
So how do you name these without case sensitivity and actually know what they are?
uxFirstName & uxFirstName does not work
the only answer i have found is to use Hungarian notation, yes i threw up in my mouth. Then again its vb.net and it should be case sensitive since .net is and all of the compiles to IL.
回答17:
Not really sure about any guidelines, i suspect there are, but I always use number 2 as well!
回答18:
these are really just based on your preferences but yes, option 2 as described is less verbose and indicates to you the type of control even before its name is shown.