subject: Naming Visual Basic Variables - Guidelines to Follow for VB Programming [print this page] Naming Visual Basic Variables - Guidelines to Follow for VB Programming
If you're writing a program in Visual Basic for Applications (VBA) or Visual Basic (VB), you should take care to use good variable names. The author has been programming now for more than 20 years, and has come to realise that giving variables sensible names is vital if you're to write good code. This artcile gives some guidelines to help you!
Names to Avoid
Let's start off with some naming conventions to avoid. The first one is to use single-character or short names. For example:
DIM s AS String
Althought this undoubtedly saves time, it makes code difficult to understand. For example, if you see this at the top of a program, your heart would sink:
DIM s1 AS String, s2 AS String
DIM s3 AS String
Do yourself a favour - use longer names!
Use Camel Case
Camel case is the term used to describe variable names which use capital letters to begin each new word, while not using spaces. Here are some examples of camel case variable names:
DIM InterestRate AS Single
DIM VatRate AS Double
DIM UserName AS String
Notice how in each case the use of multiple words with capital letters for each makes the variable name easier to understand and read.
Don't Specify Variable Type
Many technical people will advise you to use 3 letters to prefix each variable, to denote what sort of data type it is. Thus the 3 variables above should read:
DIM sngInterestRate AS Single
DIM dblVatRate AS Double
DIM strUserName AS String
Some people consider this improves programming - we just thinks it makes your code look cluttered, and harder to understand.
Don't be Afraid of Long Names
If you've got a variable which will hold the name of an Excel workbook used to hold interest rate assumptions, don't be afraid to call it ExcelnterestRateWorkbook. Long variable names make code easier to understand!