Naming your variables properly is an important, but often overlooking, building block that, once learned, will improve your codes readability.
Contents:
What Does Naming a Variable Mean?
In programming, naming a variable refers to the process of giving a name to a value. This name is used to refer to the value stored in the variable throughout the program, and it can be used to access and manipulate the value as needed.
Variable names should be descriptive and meaningful. A good variable name should clearly and concisely describe the purpose of the variable, so that other developers (or even yourself, if you come back to the code later) can easily understand what the variable represents.
For example, in the following code, the variable "customer_name" is used to store the name of a customer:
customer_name = "John Doe"
In this case, the variable name "customer_name" is a clear and descriptive choice, because it immediately tells us what the variable represents. In contrast, a less meaningful variable name, such as "x", would make the code more difficult to understand and maintain.
Most Common Variable Naming Conventions
Variable naming conventions are a set of rules that developers follow when choosing names for their variables.
Which naming convention you choose is largely up to personal choice. Though, often programming communities will gravitate towards a specific convention. For instance a lot of VBA tutorials you may come across will use Hungarian notation, but this has fallen out of favor and is quite outdated.
Some of the most common naming conventions include, snake case, camel case, Pascal case, and Hungarian notation.
Snake Case
Snake case is a less common naming convention than camel case, but is my preferred convention as it offers the most readability in my opinion. As you can see in the examples above, snake case follows the convention of using underscores to separate words, and using all lowercase letters.
Here's an example of a simple VBA function that uses snake case for its name and its variable names:
Function calculate_total(price As Double, quantity As Integer) As Double
Dim gross_total As Double
gross_total = price * quantity
calculate_total = gross_total
End Function
In this example, the function is named "calculate_total", and has two parameters, "price" and "quantity". The variable "gross_total" is also named using snake case. Following the snake case convention helps to make the code more consistent and easier to read.
Camel Case
Camel case is often used for naming purposes in VBA. As you can see in the examples above, camel case follows the convention of capitalizing the first letter of each word, except for the first word, which is lowercase.
Here's the same VBA function, using in camel case instead:
Function calculateTotal(price As Double, quantity As Integer) As Double
Dim grossTotal As Double
grossTotal = price * quantity
calculateTotal = grossTotal
End Function
Pascal Case
Pascal case is similar to camel case, but with the additional requirement that the first letter of each word must be capitalized.
Here's the same VBA function, using Pascal case:
Function CalculateTotal(Price As Double, Quantity As Integer) As Double
Dim GrossTotal As Double
GrossTotal = Price * Quantity
CalculateTotal = GrossTotal
End Function
Hungarian Notation
Hungarian notation is a naming convention that was popular in the early days of programming, but has largely fallen out of favor in modern software development.
In Hungarian notation, the names of variables include a prefix that indicates the data type of the variable. For example, "str" is used as a prefix for string variables, "i" for integer variables, "bln" for Boolean values, "db" for doubles, and "dtp" for Date/Time variables.
Here's the same VBA function, using Hungarian notation:
Function dbCalculateTotal(dbPrice As Double, inQuantity As Integer) As Double
Dim dbGrossTotal As Double
dbGrossTotal = fbPrice * inQuantity
dbCalculateTotal = dbGrossTotal
End Function
As you can see, the function is much less readable. By including the prefix every single time we use a variable, we're not adding new information to our code, but just making it harder to understand what the code is actually doing.
We already know what data type each variable when they were defined in first couple lines. Repeating the same prefix over and over again is redundant.