textBoxEmployeeName vs employeeNameTextBox

2019-01-23 15:51发布

问题:

Which naming convention do you use and why?

I like to use employeeNameTextBox, because:

  • It seems more natural from an English language perspective.
  • I find it's easier to look up with Intellisense.
  • The convention is similar to the convention used for events (MouseClickEvent, MouseClickEventHandler) and dependency properties (VisiblityProperty).

Note: I am using the full name rather than an abbreviation (such as "tb"), because it is in line with MS's naming conventions that say to avoid using abbreviations.

http://msdn.microsoft.com/en-us/library/ms229045.aspx

回答1:

The only reason to use the control type in the name first (textBoxEmployeeName) is for easier grouping with Intellisense (All textbox controls would then show up together). Beyond that, there really is no benefit to using that way. I find the second way (employeeNameTextBox) more readable and prefer that way personally, but a lot of people will still go with the control type first, since that is the way it was done for a long time.



回答2:

Naming your variables is so important. Thick client view conventions seem to be given the short end of the stick. Here are my thoughts:

  1. Never put getters and setters for actual business values on your view. Don't do this:

    public Name EmployeeName { get; set; }
    
  2. To get or set an EmployeeName, your stateful code should explicitly call a method. Do it this way because it projects that the state is not stored on the view, but can be derived from or transposed to the view:

    public void SetEmployeeName(Name employeeName);
    public Name GetEmployeeName();
    
  3. Hungarian notation is stupid. It was useful in languages <= VB6 because they used late binding of variable types. You had to protect yourself because type mismatches were runtime errors, not compile time. Only use txtEmployeeName if you also would use strEmployeeName and intEmployeeNumber.

  4. If prefixing the pattern name isn't consistent with your naming convention, don't do it for the control type (which represents a pattern). If you wouldn't create a commandNameFormatting (instead of nameFormattingComamnd), then don't create a textBoxEmployeeName.

  5. You'll probably need a suffix of some sort, since EmployeeName doesn't sufficiently describe the variable's purpose. An EmployeeName text box's purpose is to receive input. You could call it EmployeeNameTextBox if that makes you comfortable, but it might be better to call it EmployeNameInput. This has the added bonus that if you have a label, it's clear that EmployeeNamePrompt (or EmployeeNameLabel) is not the same as the text box. Without some sort of descriptive suffix, you don't have a good way to differentiate.



回答3:

I (almost) always use [controltype][descriptive name]. I want to know right away what type of control I'm dealing with when I look at code, and if I DON'T remember the name, intellisense can help me out.

Just using a descriptive name (EmplyeeName) doesn't work for me. What type of control? Is it a label, a text box, or a combo box? Or a string? Or a file? It's important enough that the type of control or variable is always a part of it.



回答4:

I propose a third option: uiEmployeName. Reasons:

  1. It's not Hungarian. Both of the notations you mention are just flavors of Hungarian.
  2. If you change an employee name text box over to a listbox you don't need to rename your variables.
  3. Everything is grouped nicely in the intellisense without involving the type of the object.
  4. The name of the object closely follows its function. It is a user-facing object that gets the employee name.


回答5:

I generally try to keep the element type short, followed by a distinguishing label. I find that it quickly communicates the type and purpose of the element:

txtEmployeeName;
lblEmployeeName;


回答6:

Why not EmployeeName? Seriously how does the control type as part of the name when it is already provided by your IDE assist in delivering easy to maintain code? Consider Ottenger's Rules for Variable and class Naming

K



回答7:

As I read it, an article linked to in the article mentioned in the question (namely, Names of Resources) does use the control type at the end, in FileMenu (and ArgumentException though it's not a control).

My personal opinion is that this is also more readable, as it's the employee name text box and hence should be named the employeeNameTextBox, just like the words "File menu" are read in that order. (Though I substitute "Edit" for "TextBox" for brevity — I should probably kick that habit to use control names consistently with the environment name for them.)



回答8:

A MUST READ is the XAML Guidelines released by Jaime:

Also read more here



回答9:

WPF-specific Answer: No name at all.

Why? Because if you're developing using WPF you should not be naming your controls. If you are, you are doing something wrong.

WinForms required controls to be named because its data binding was so weak. WPF solves all that: The control itself can specify its data and behavior, so there is no need to name it.



回答10:

I guess it's better to follow Microsoft's Object Naming Convention for naming your controls both in C# as well as Visual Basic.



回答11:

I don't recommend hungarian notation in any form. textBoxEmployeeName is a form of hungarian notation. So I support the use of employeeNameTextBox.

Personally I don't even bother using the word TextBox, because it is not what is important about the variable. What is important is "Employee" and "Name". Not only does adding the word TextBox lock you in to a certain type, it also make it much harder to change that type, because you need to change the name to normalize your code and make it correct. Say for some reason you started this as a TextBox, but you later received a requirement to change this to a DropDownList or some other type, now you have to update all of your code and JavaScript to make it say DropDownList instead of TextBox.

It is much easier to forget about trying to type your variable names, and just simply name them. You have intellisense and compile time error checking for a reason, why not use it.



回答12:

I would go with [controlType][DomainTerm] which in this case is textBoxEmployeeName. The reason is that while coding for the C# code behind you are more care about the UI controls than the domain specific terms.UI(View) side coding we need to identify/recognize the control type faster, which is little more important than the domain specific name in the View side , and since we read from 'Left to right' this naming convention is relevant. I generally use txtEmployeeName or cmpEmployeeType , but textBox instead of txt is preferred as per MS guidelines



回答13:

I have used both txtEmployeeName and employeeNameTextbox. Like many of the posts indicated, this is helpful for grouping. One groups by control types (txtEmployeeName, txtManagerName) while the other can group different related controls together (employeeNameTextbox, employeePhoneTextbox, managerNameTextBox, managerPhoneTextbox). In many cases I find the later more useful while coding.



回答14:

You should do whatever it is that makes your code readable and self-documenting. Following hard and fast rules is always a mistake because they almost never cover all aspects of what needs to be done. There is nothing wrong with having guidelines (such as not using Hungarian notation), but it is more important that you are consistent and clear with your naming convention, whatever it is, than you follow some rules found on the Internet.



回答15:

Ideas:

  1. Avoid encodings/abbreviations.

  2. The name should stand out from similar names in the same scope. Make the unique-most part the left-most part. I suspect you have several text boxes, but only one is the employee name.

  3. Avoid needless context. Are all the names on this page about employees? Is it an "employee" page? Then EmployeeName is redundant. NameBox or NameControl should be plenty.

  4. Avoid needless context: do you have names that are not controls? If so, "Box", or "Control" is useful, otherwise not so much.

Disclosure: I am the "ottinger" from "ottingers naming rules", which also evolved to be chapter 2 of "Clean Code". See short form at http://agileinaflash.blogspot.com/2009/02/meaningful-names.html



回答16:

In VB I actually like to go with [ControlName]_[ControlType]. I can't remember how I started doing that but I suppose it's because it feels like a logical order. It also simplifies coding a bit because the code suggestions are grouped by the control description rather than the control type.

I name controls the same way in C# except I follow C#'s preference for camelCase: [controlName]_[controlType].

I also tend to use my own abbreviations for control types, though they are not vague.

Examples:

VB: Save_Btn and NewFile_MItem (menu item)

C#: save_btn and newFile_mItem

It works for me, but of course every programmer has their style.