...

6 Rules for Naming Variables in Programming

How can variables be used in programming?

Imagine a world without names! If you don't have a way of identifying your family and friends, how bad could it be?

Or, for a second, how would it sound if we all bear the same name? 

The reason we have several names is for us to be well identified and addressed appropriately. In the same way, computers want to identify values like numbers, texts, and both known and unknown values. 

Variables allow us to achieve this purpose. In computer programming, a variable is an abstract (virtual) storage location paired with an associated symbolic name (in other words, we store values with names).

One common mistake you can run into quickly is to mess up your naming conventions while storing a series of values. While it is easy to store five values with random names, it will become a disaster as your project gets bigger. 

It would help if you had a way to name your variable according to best practices so that your code is clean and easy to read. 

This post will show you six rules you must always consider and follow each time you declare a variable name.

 

1. Variable name must begin with a letter, dollar sign ($) or an underscore (_):

Computers follow the instruction strictly and consider values as defined without assumptions. Conventionally, variable names have been structured to start with a letter, dollar sign or underscore. 

 

2. What you cannot start a variable name with: 

On no occasion should you begin a variable name with a number, period (.) and dash (-)

The keywords that mean specific things in Programming languages and wouldn't sit well in variable names (especially at the beginning)

 

3. You cannot use reserve words to name variables:

Keywords or reserved words are unique words that inform the programming language interpreter to perform a specific task. For instance, words like var are a keyword used in Javascript to declare variables. Search for more keywords on the web, so you are on the safe side.

 

4. Variable are case sensitive:

If you declare your variable as "score", you need to call it to score. You cannot reference that same variable with the capital Score. These are two different values. 

Now that you know that "score" and "Score" are two different values, it is terrible to create two variables with the same name by using other cases. Later on, it will confuse you, so don't use it, but rather combine words (like total_score).

 

5. Use variable names that describe the value:

It makes sense if you use the name computer understands, as I mentioned earlier. You, as the programmer, need to understand and remember what value to expect from the variable whenever you come across it in your code.

For example, to store a user's name, don't use "n"; instead, use "user_name", "first_name" or "last_name"

 

6. Use camelCase or snake_case in naming your variables:

If you have multiple words for your variables, use either camelcase or snake case to name your variables. For instance, use either "firstName" or "first_name" if you need to store the user's first name. 

These are conventional ways of naming variables and make reading and writing easier.

Post Comments(1)

...
Hehe Haha 2 weeks, 1 day ago

Hehehehehehe Hahahahahaha

Leave a reply

+