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条回答
萌系小妹纸
2楼-- · 2019-01-23 16:21

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;
查看更多
SAY GOODBYE
3楼-- · 2019-01-23 16:23

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

查看更多
ゆ 、 Hurt°
4楼-- · 2019-01-23 16:24

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.

查看更多
一纸荒年 Trace。
5楼-- · 2019-01-23 16:27

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

查看更多
神经病院院长
6楼-- · 2019-01-23 16:30

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.

查看更多
男人必须洒脱
7楼-- · 2019-01-23 16:31

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.
查看更多
登录 后发表回答