Answered Naming Convention for Controls

benshaws

Hobbyist Programmer
Joined
Sep 19, 2013
Messages
18
Location
Leeds, UK
Programming Experience
5-10
Does anyone have any recommended advice / best practice regarding the naming of controls. I understand that hungarian notation is now dead.

I have had a good look around the internet and advice seems to differ depending on which site I visit. The top search result, here they recommend prefixes as per hungarian notation but with an initial capital.

Elsewhere the advice has been to prefix with "c" and suffix an identifier or just use a suffix. MSDN does not provide any advice as far as I can see for naming controls, there is advice on other naming conventions.

Although the majority of my code is unlikely to be seen by anyone other than myself, should I post further on this forum it would be better if forum members could understand the code through a consistent naming convention.

I don't think its is just me either. Reading posts, it seems beginners just use the naming style of the snippet they have copied or just assume still that hungarian notation is the correct way.

Thanks.:unsure:
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,570
Location
Sydney, Australia
Programming Experience
10+
I would suggest that you use the same principle to name controls as you do for naming anything and everything else: use a name that clearly describes the purpose so that your code becomes self-documenting. The purpose of a control is to interact with a user so the names should reflect that. Some people balk at using the control type in the name because they figure they can achieve the same result with less using a prefix, but it's not doing the same thing a prefix. For instance, let's say the user needs to enter their first name in a TextBox. The variable to store the first name itself would be named firstName because that is what it represents: it literally is the first name. The TextBox would be named firstNameTextBox, not because it's important to indicate that its type is TextBox but because that is what it represents: it literally is a box into which the user enters the text of the first name. It's not always the case that you would use the actual type name either, e.g. I might name a DateTimePicker dateOfBirthPicker. There's always going to be opinion and personal preference involved in this choice but, whatever you do decide to do, be consistent. Also, do what I said. ;)
 

ProtekNickz

Well-known member
Joined
Oct 22, 2009
Messages
130
Location
UK
Programming Experience
1-3
Hi many people use many different methods to name their Controls and Variables for me it's just a simple case of using the Controls own name in the naming process example:

Textbox Controls = txtFirstName | txtLastName , Button Controls = btnRun | btnClick , ListView Control = lstUserNames | lstAddess, and for Variables String Vaibles = strFirstname | strLastName, integer = intNum | intSum and for Forms = frmMain | frmSettings ect...

These are just a few naming conventions I use, it just helps me in general to know which Controls / Variables I'm using and makes the Coding easy to understand if I come back to it after a while and(or) someone else needs to peruse the code.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,570
Location
Sydney, Australia
Programming Experience
10+
Hi many people use many different methods to name their Controls and Variables for me it's just a simple case of using the Controls own name in the naming process example:



These are just a few naming conventions I use, it just helps me in general to know which Controls / Variables I'm using and makes the Coding easy to understand if I come back to it after a while and(or) someone else needs to peruse the code.
That is known as Hungarian Notation and it dates back to a time when there were only a small number of types in use. Today, there are a small number of types that we use a lot but there are many thousands of types that we could use, so Hungarian Notation becomes far less useful. It also dates from a time when you couldn't just mouse over a variable in the IDE and have Intellisense tell you all about it. Apart from that, such prefixes are generally useless if you use names that actually describe what the variable contains. If you don't know that a variable called firstName contains a person's first name, which couldn't be anything but text, then you probably aren't up to programming in the first place. Obviously anyone can do whatever they want but I strongly recommend against using Hungarian Notation. This is from someone who also used it when starting out.
 

ProtekNickz

Well-known member
Joined
Oct 22, 2009
Messages
130
Location
UK
Programming Experience
1-3
Nice to know a bit of history behind what I have been using, Didn't realise it's a dated mechanism, Maybe it's time for a change on my behalf, I'll probably use the camels hump type with better naming system, so I may adhere to move to a hierarchy of better naming which makes sense since VB.NET is a high level language compared to other out their, thanks for the information.



ProtekNickz xD.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,570
Location
Sydney, Australia
Programming Experience
10+
Nice to know a bit of history behind what I have been using, Didn't realise it's a dated mechanism, Maybe it's time for a change on my behalf, I'll probably use the camels hump type with better naming system, so I may adhere to move to a hierarchy of better naming which makes sense since VB.NET is a high level language compared to other out their, thanks for the information.
I suggest that you just follow Microsoft's recommendations, which basically means camelCase for private fields, parameters and locals and PascalCase for everything else. When it comes to the names, I suggest following Microsoft's recommendations again, which means generally not using abbreviations and using names that describe what the thing is or does. For example, a TextBox that is for a person's given name would be named givenNameTextBox. Some people may wonder why that's better than txtGivenName, which is shorter. The reason it's better is that the name is not about the type but about the contents. For a String that actually contains the given name, you would use givenName rather than strGivenName. Some people like the fact that using prefixes means that they can quickly find all variables of a particular type in Intellisense by typing that prefix but I think that that shows poor thinking. Again, you should be thinking about what the variable contains, not what its type is. That means that typing "giv" to find a variable related to given name makes more sense than typing "txt" and then looking for the TextBox you want. Some people say that they might forget what they named something but, again, that indicates poor thinking. If you have named a variable sensibly based on what it contains then how could you forget what you named it? How could you forget that you named a variable that refers to a TextBox that contains a given name givenNameTextBox?
 

ProtekNickz

Well-known member
Joined
Oct 22, 2009
Messages
130
Location
UK
Programming Experience
1-3
I stand corrected again heh, Is their a particular title to search for on Microsoft so I can do some reading on this, and lets say I'm out of touch with programming for the last few years or so, It's really nice to see some one actually handing out great information.

Cheers,


ProtekNickz xD
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,570
Location
Sydney, Australia
Programming Experience
10+
Is their a particular title to search for on Microsoft so I can do some reading on this
I don't keep a link lying around but I just searched the web for "microsoft .net naming conventions" and this was the second result. You could have found that just as easily as I did. I suggest that you always look first and ask questions later.
 

ProtekNickz

Well-known member
Joined
Oct 22, 2009
Messages
130
Location
UK
Programming Experience
1-3
Cheers for taking the time out to point me in the right direction, Appreciated :).


ProtekNickz xD
 

aaaron123@roadrunner.com

Well-known member
Joined
Jan 23, 2011
Messages
112
Programming Experience
10+
I know that when VS adds a control it suffixes the type but I lie it first.

I may not remember the exact name I gave a TextBox but I always know it's a Textbox so searching a name list I'll find TextBox_NickName quicker than Id I'll find NickName_TextBox because all the names starting with TextBox are together but the names ending with TextBox are not.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,570
Location
Sydney, Australia
Programming Experience
10+
I know that when VS adds a control it suffixes the type but I lie it first.

I may not remember the exact name I gave a TextBox but I always know it's a Textbox so searching a name list I'll find TextBox_NickName quicker than Id I'll find NickName_TextBox because all the names starting with TextBox are together but the names ending with TextBox are not.
Firstly, Intellisense will find matches anywhere in an identifier, not just at the beginning. It does for me, anyway, but I use ReSharper, so maybe vanilla VS Intellisense doesn't do that and I just don't remember.

Secondly - and more importantly - I have heard this argument before and I always wonder who on Earth these people are who can't remember what they named a control. If you can't remember what you named a control then you are almost certainly using bad names in the first place. If a TextBox exists for the purposes of displaying and editing a NickName field/property then I'm naming it nickNameTextBox. If I then want to access that in code, how exactly am I going to forget the name if I know that I'm looking for the TextBox for the NickName field/property? I've always considered that to be a justification for following an existing convention - an excuse to not change what you're already doing - rather than an actual reason to use that convention.
 
Top Bottom