*

Development Principle 1: How to choose appropriate Variable Names?

15 Apr. 22
405 VIEWS

Variables are one of the most commonly used parts of programming. Variables are used in a number of ways, such as providing instructions for your program, providing the logic for your program, the ability to measure time and analyze your data, as well as the personalization of your programs. 

Given how vital variables are to coding, it’s important to name them accurately and describe their purpose in your program. Even poor variable names are necessary, as they help other developers understand your program at first glance. 

While it can sometimes be difficult to find a concise and accurate name for a variable, it’s important to explain the function of the variable within the name itself, as well as make it unique enough to differentiate it from other variables.

Now, let’s discuss a few tips you can utilize in your code to create clear, concise variables. 

Make it clear

 

 

Many programmers will compromise clarity in order to speed up their programs. It’s important to remember how bad programming can result from sacrificing a precise variable name for a few keystrokes.

Computer programmer Alan Davis once said, 

Some programmers insist on naming variables like N_FLT or worse, something like F. This argument is usually that programmers are more productive due to the reduced key presses. Good programmers should only spend 10 to 15% of their time typing; the majority of their time should be spent thinking. But how much time are you able to save?

A few more keystrokes can make a variable easier to understand in many cases. A few extra seconds of typing can reduce the cognitive load on other developers trying to understand the variable’s purpose and meaning. It is much easier to call a variable “dRange” when it is used for a range of dates than it is to call it “dateRange”.

Avoiding short or ambiguous variable names like “tmp,” x, or “i” is also a good idea. Although it is easy to follow this path, it is much more beneficial to have a variable name that clearly explains the function of the variable. It doesn’t matter if the variable is used only in two lines, or if it is an index in a for loop, it is more helpful to have a name that clearly explains what it does.

It is important to think about whether your variable names can be understood worldwide. Let’s Nurture has offices in Europe, Asia and the USA. Will the same abbreviations that we use in the US translate to foreign developers? Because some larger projects require international collaborations, it is even more critical that variable names are clear.

This sentence is a great way to check if your variable names have been written clearly.

“This is the _____.”

Saying “This is the dateRange” isn’t enough information to describe the variable. You will instantly know what the variable is if you say “This is dateRange”.

In some instances, abbreviations can be useful. Commonly, the famous Fibonacci sequence function recursive function (Fibonacci()) is abbreviated to “fib ().”

It is important to be clear about the number of comments required for variable naming. If a variable has many comments or a large number of them, it is likely that it was not properly named. Some might argue that “good code doesn’t need comments.” Comments are a good thing, but they shouldn’t be used to explain in detail what a poorly named variable does.

Be Concise

 

 

Try to avoid using long variable names. If you have a variable that stores a user’s favourite colour on the object named “user,” you might not want to call that variable “ThisPersonsFavoriteColor.”

Consider the best way to get the information across. Perhaps you name this variable “favoriteColor” so that anyone can access it as “user.favoriteColor” and understand what you’re talking about.

It is possible to also use the “name” test in the previous section to indicate “This user.favoriteColor”.  Abbreviations should be used with caution, as we have already stated. Abbreviations can sometimes be confusing, but they are also useful in keeping the lines short and making the code appear cleaner overall.

Be consistent

 

 

It doesn’t matter whether you use PascalCase or camelCase or snake_case or lowercase or hyphen-case without or with Hungarian Notation naming conventions. It is important to adhere to the current naming conventions for the project.

When you’re starting your project, I recommend that you look at the code of other developers using the same language. What is Python programming? Snake_case is the best choice. Javascript? CamelCase maybe your best option. PHP? StackExchange offers a great solution (they point out how different PHP frameworks recommend or deny specific cases).

Hungarian Notation has its place if it is properly used. Joel Spolsky’s article, “Making Wrong Code Look Wrong” explains how Hungarian Notation was intended to be used and not the current way it is being used.

 

Conclusion

 

Programming is centred around variables and it’s important to take them seriously. You can improve communication between you and developers by using clear, concise and consistent variable names. This will make it easier for them to understand and interpret your code labels.

We use cookies to give you tailored experiences on our website. Talk to us for COVID19 Support
Okay