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,349
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. ;)
 
Top Bottom