textBoxEmployeeName vs employeeNameTextBox

2019-01-23 15:42发布

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

16条回答
Lonely孤独者°
2楼-- · 2019-01-23 16:11

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.

查看更多
放我归山
3楼-- · 2019-01-23 16:13

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.

查看更多
相关推荐>>
4楼-- · 2019-01-23 16:14

A MUST READ is the XAML Guidelines released by Jaime:

Also read more here

查看更多
不美不萌又怎样
5楼-- · 2019-01-23 16:15

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.

查看更多
Summer. ? 凉城
6楼-- · 2019-01-23 16:18

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

查看更多
可以哭但决不认输i
7楼-- · 2019-01-23 16:21

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.

查看更多
登录 后发表回答