Over the past several years I have been using the naming convention of FirstNameTxt
when I'm referring to a TextBox Control for a 'First Name' field. However,开发者_JAVA百科 I've noticed the majority of other developers tend to use the naming convention txtFirstName
Which is the best convention to use? and why is it preferred over the other method?
I like using the Hungarian Notation - prepending with a 3-letter abbrev - if for any other reason, all the textbox controls are grouped together in the list of controls for the page.
Since Visual Studio 2010, there is absolutely no reason to use Hungarian Notation.
The only reason one could imagine - to easily search for similar controls with intellisense, goes away with new intellisense which searches in full text.
Thus typing Text
in code editor finds you all: firstNameText
, lastNameText
, anyText
.
And it's much easier to read "firstNameText" than "txtFirstName" in code.
From the research I did on the same subject there seems to be no true consensus or best practice. People will argue that it should be txtFirstName, FirstName, and FirstNameTxt. My suggestions is that as long as its consistent then you are fine. Nothing is worse than coding when the names switch around.
As an aside I use txtFirstName.
The .NET Framework guidelines recommends that the first character is lower case. Like "txtFirstName" or "awesomeVariable" etc.
http://msdn.microsoft.com/en-us/library/x2dbyw72(v=VS.71).aspx
See this list of recommendations. :)
I generally use "ux" for "User eXperience" as a prefix for control names. This makes it easier to switch control types while maintaining the same name. Since controls have a shared inheritance hierarchy, it's often possible to switch from one to another, for example from a label to a textbox without changing the prefix from "lbl" to "txt". It also groups all controls together in the list so I don't have to remember the type of control I'm looking for.
I don't use Hungarian notation. If I have trouble remembering the exact names of controls on my form, then I'm doing something wrong. Most likely the form class is doing too much and has several responsibilities.
To fix that I usually split the form into subsections using user controls and cleanly separate concerns across them. As an added bonus the code gets easier to test.
One isn't better than the other. The most important thing is to be consistent in whichever convention you choose.
Since you have been using your current convention for the past several years, I wouldn't change because then you will have broken convention. Unless, of course, you choose to go back and refactor all of your existing code for the new convention. But that begs the question "why bother?".
精彩评论