How to name variables/controls?

Digitborn.com

Active Member
Joined
Apr 3, 2007
Messages
353
Hello,

Though I've looked about 20-30 internet pages, several ebooks and comments about the topic I'd like to ask you about it

If I use many different controls on a userform what's the best way to name them? I think that it's very good if the name to consists of 3 parts:

1. Control Name
2. Control #
3. The purpose of the control

For example - if you have a ComboBox where you choose fruits from Africa and it's the 3rd ComboBox from left to right & up to bottom, I think it should be best if you name it: Combo3AfricaFruits

Yes, I know it's very long, and it's not good to remember. But this is a rule which make you find everything and gives you a lot of information about the things you're doing in your code and as a whole.

Otherwise what options you have taking the recommendations: AfricaFruits, Fruits, ComboBox3, Africa...short, nice, but not good enough. Why?

1. If you miss to name the control and give a name like AfricaFruits, you won't know if it's for the TextBox or the ComboBox

2. If you miss the number and name it ComboAfricaFruits and you look the design of the form where you have 6 ComboBoxes, 3 TextBoxes, 1 Mulitpage, 5 CheckBoxes, 4 OptionButtons, 10 Labels, CommandButtons etc.. it will be a total mess for you to make a good plan for design and code functionality and validation.

3. And maybe the most important if you name only ComboBox3, it's no good and no need to comment at all.

This is my opinion, let me know what's your point of view
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hello there,

This is really a matter of opinion and what works best for you. I like to use a hungarian-type notation. It is generally case specific and is descriptive to not only the variable/control type, but what it is in descriptive text. The first part is the type expressed as generally 3 lower case characters. After that it is the descriptive text without spaces and in proper-type casing. Here are some examples...

Range variables:
rngStart
rngEnd
rngFilter
rngData

Long variables:
lngRowStart
lngRowEnd
lngNum
lngMaxNum
lngMinNum

Boolean variables:
blnCreated
blnSwitch
blnActive
blnOpen
blnClosed

For UserForm controls, I generally use the same thing...

Command Buttons:
cmbOk
cmbConfirm
cmbCancel
cmbExit

Combo Box's:
cbNames
cbCompany
cbItems

Text Box's:
txbFirstName
txbLastName
txbAccountNum
txbInputVar

List Box's:
lbFiles
lbFolders
lbAccounts
lbNames
lbCompanies


You could query 50 different people and you'll get 50 different opinions. The bottom line is do what works for you, what makes the most sense. A system is better than no system. If you can easily identify what type of control/variable it is and what it is used for, that is what should be used by you. Again, this is only my opinion. :)
 
Upvote 0
Yes, the HUngarian notation is the most popular. I wonder why programmers don't put a number in the names?! If you have more than 10 controls/variables it very helpful. Mostly in controls, because you often move/redesign/delete/add/change rules and it's very good to know where are they. That's why there's a number by default from VBA. I didn't see anybody naming including a number and i thought there's something really no good about the idea.

Another thing is that it's very strange naming CommandButton, ComboBox and CheckBox, there're so many option so you can mistake them:

CheckBox cbx, cb, chb
ComboBox cmb, cb, cob, com
CommmandButton btn, cob, com,
 
Upvote 0
Yup, and you've just got to use one that works for you. Numbers aren't too bad in names. I stray away from them as I can generally name them in an order that the names given are complimentary to what they are [used for]. I have used numbers in the past though.

For me, a command button is cmb whereas a combobox is cb. Why? It's just easier for me to recognize the difference between the two. I've seen some people use cmb for a combobox. It's confusing for me (haven't seen many like that), but not when I write code for myself. I guess a better way would be to not use any less than 3 characters for the type description, and no more than three, use three exactly.

As for the friendly name part, that is also highly subjective. What means apple to me could mean pear to you. Usually you are the only one who knows your project(s) best. So it should be up to you to give it a meaningful name. If that means putting a number in it to make it distinctive to the other controls, so be it. Maybe it's the number of the multipage the control is located on. Maybe it is a tab order for your controls (I like this idea myself). It could be any reason. But if that is what you like and it works for you, then do it. :)
 
Upvote 0
I know that it's importat your program to be 'readable' from other programmers concerning the variables/controls. That's why it's very smart even if you don't fully like the Hungarian notation to use it.

Also I thought that if you use exactly 3 letters for control naming it won't be a problem. For CommandButton, it's good btn.

My general opinion is that we should follow 1 or 2 rules for programming languages concerning the naming. I really like very much the combobox with controls in the VBA Editor.
 
Upvote 0
This is really strange (at least for me) but finally I decided not to name my controls. I like them very much in these default names - ComboBox5, TextBox1, CommandButton3, CheckBox3 etc. Working on a UserForm or placing them on a Worksheet it's easier for my mind to remember where is the position of the control. And with the number of the control you already know what's it's used for. All these abbreviations distract my attention.
 
Upvote 0
If it works for you, more power to you. There are a few things to think about though...

1) Will it help when you troubleshoot code?
2) Will others be using your code?
3) Will you know exactly what you're looking at with that naming convention?
4) If/when you leave (the spreadsheet, employment, whatever) will others need to know what is going on with your code?

A lot of the questions we see on the boards comes from people who have inherited files from others. So, I'm not saying you're wrong, what I am saying is take all of this into consideration. :)

And thanks for keeping us updated as to your progress!
 
Upvote 0
Zack, thanks for your active comments on this topic.

I'll expose my thoughts here. If somebody wants to work on your project as a programmer or project manager he/she should know the following most important stuff:

1. What's this project about
2. What's the go of the program
3. The + and - of the program from other similar programs around
4. How the program works - menues, buttons, options etc.

If we say these are the first things to know, one of the next thing to know are the program structure, variables etc. My point is that if you know the menues, buttons, controls of the program and visualize it in your head it's very simple and easy to know which control is ComboBox7 or TextBox12. I think that everybody from designer to programmer knows how the program looks like very very well. This is my opinion, is that right from your experience?

About the variables I'm very much on the position that the names should look like the Hungarian notation. About the controls I think that the concept is quite different though in the basics it's the same thing - you have an object and you should name it. So, most of my comments will be concerning the controls naming.

What I mean - For example if you have the control name cmbApples = ComboBox5 and you choose to name it cmbApples what info is giving this name to you:

1) ComboBox
2) It's something about "Apples"

If you choose the name ComboBox5, the info is:

1) ComboBox
2) I know where is it's position on the program. So, I know which controls are around it and when I look at the program (the design) i'm thinking how to structure my program best. This means how to validate all my controls to work perfectly, intelligent and save time (make them cascading).

Also I think that it's important 90% for the programer to know the control type and 10% other info whatever it is. I think so, because the programer writes his code pointed towards the object and care less the purpose of the control.

Here it's important to add that we can devide 2 types of programs:

1. The one you have the full concept, structure and complete notes how the program should work.
2. The other where it's more your idea project, where you have the main concept and other examples in the area but you're doing some innovations or different type of program.

The way I choose to name controls, I think it's giving extra points to option 2 where you spend much time on looking at your program and thinking how to make it better as a designer, programmer and project manager all in one.

Other advantages for this kind of control naming:

1) All examples for controls in books, internet, forums are named like the way I choose
2) If I want help and need to post code in a forum I should rename my controls. I know it's recommandable, not necessary.
3) If we say that the human brain is working much like a multiprocessor, here we can say that you save one unnecessary step when you're looking at the control name and seeing something like apple, product, close, etc.

p.s. Maybe I should save many lines and say the same in a shorter way, sorry if it looks too long for you to read my thoughts on the topic.
 
Upvote 0

Forum statistics

Threads
1,225,335
Messages
6,184,335
Members
453,227
Latest member
Slainte

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top