Home / Tutorials / 7+1 tips for naming variables

7+1 tips for naming variables

Here I’m writing some tips for naming variables

Tips for Naming Variables

1.- The variable name has to be as descriptive as possible. Don´t use generic names.

Good example: daysDateRange, flightNumber, carColor.

Bad example: days, dRange, temp, data, aux…

There are a lot of developers who would prioritize short variable names over descriptive names because that will save time when typing the code, but who cares? It doesn’t help to improve the quality of the software, it only saves a few minutes per day (maximum) per developer, by the other hand, when having descriptive variable names, the overall quality of the software will increase because it will be easier to modify and read the code.

2.- The variable name has to be as short as possible.

This rule goes hand by hand with the first rule, the variable names should be as short as possible but as descriptive as possible.

This basically means that while we still try to come up with short names, we prioritize descriptive names over short names, we only pick the shorter one when they are as descriptive as the longer one.

Bad Examples: howLonDoesItTakeToOpenTheDoor, howBigIsTheMaterial…

Good Examples: timeToOpenTheDoor, MaterialSize.

3.- It´s OK to use abbreviations, but explain the abbreviation with a comment.

Sometimes to have a good descriptive name is necessary to make very long variable names, which is fine, but the variable size can be improved by using abbreviations, just make sure that when the variable is declared there’s a comment that explains the meaning.

4.- Use proper Hungarian notation when necessary.

There’s a great post from Joel on software that explains what the proper Hungarian notation is and how to use it. Basically it says that when coding a method where we have variables holding the same data, but with different types, the Hungarian notation can help us.

Example: Imagine a method that receives apples, then it classifies the apples depending in their color, and finally discards the ones that are not matured enough. We could use in this example the following variable names:  incoming_apples, red_apples, yellow_apples, notMaturedYellow_apples, red_apples, maturedGreen_apples…

5.- Don´t use negative logic for your variable names.

Good Example:  IsEnabled.

Bad Example: IsNotEnabled.

It’s always easier to read and undertand an statement that is expressed in a positive language that in a negative language.

6.- Be consistent.

If you have used a variable name called customer in a method, then don’t call it client in the next method, or if you use an abbreviation in one method, use it for all the methods the same.

7.- Map the application domain terminology with your names.

In different domains, different concepts have very specific and different meanings, for example, “order” doesn’t always mean the same in the different domains, try to map these specific concepts with your variable names so that when you name something with a concept from the business domain it means exactly the same.

Example. If your customer just considers “order” an “order” that has been approved, don’t call “order” to a non approved one in your code, call it “nonApprovedOrder”.

Golden rule.- Spend a few minutes thinking about your variable names.

If you find yourself writing variable names as soon as you need them in your code without even thinking on their name for a second, you are probably picking bad names for them.

If you always try to have good variable names, and you have found yourself thinking a couple of minutes for the best name for a variable, even if you don’t follow the other tips, you are likely to have pretty good variable names in your code.

Source: www.makinggoodsoftware.com

About cmadmin

Web Developer & Designer | Android App Developer

Login


Username
Create an Account!
Password
Forgot Password? (close)

Sign Up


Username
Email
Password
Confirm Password
Want to Login? (close)

Forget Password?


Username or Email
(close)
%d bloggers like this:
SHARE
OR
SUBSCRIBE
To get latest new / tutorial / technology / development information subscribe with us.
ARE YOU READY? GET IT NOW!
Lets Get Updated with latest trends & tutorials!
Your Information will never be shared with any third party.
Ready for latest tutorials & tools !
OR SUBSCRIBE WITH