Clean Code: 8 Best Practices For Naming a Variable

Published: 2 years ago - Updated: 2 years ago

5 min - 943 Words

article 'Clean Code: 8 Best Practices For Naming a Variable' banner

Summary

This article will teach you how to write clean variables, classes or objects as well as 8 simple best practices for writing readable, searchable, and understandable variables in any programming language.

Introduction

Imagine this scenario, you are writing code and implementing features, you understand what each line of our code does, you don’t need comments, it’s all obvious.

This might be the case for today, but tomorrow, a week or a month later it’s most likely that you would forget some things and will need some researching and experimenting to adjust to out code again.

But we need to consider, if you, who wrote the code needed time to get familiar and understand it again,

  • How will the other programmers will do ?
  • How much time will they need to understand it ?
  • How much time will you be spending explaining your code to a new programmer ?

Considering all the above things, writing a clean and understandable code is very important if you want our program to not get absolute.

In this article you will learn about 8 best practices for writing clean variables.

If you want to learn how to write clean functions check the bellow article

Clean Code: 6 Easy Ways to Write Clean Functions

Get Started

1 - Use Descriptive Names (Intention-Revealing Names)

The name of the variable should convey its purpose clearly. you shouldn’t need to read the whole code to understand why a variable exists.

This is bad

// Bad Example

let d = {...,...}
let arr = [...,...,.]
let flag = false;

This is good

// Good Example

let postData = {...,...};
let roleNamesList = [...,...,.]
let isPostPublished = false;

2 - Variable names should be nouns or noun phrases

While naming a variable, Class or Object, it is preferred to use nouns or noun phrases as their names.

// Bad Example

let publish = false ;

Now imagine seeing this variable in the middle of a code

....
if(publish){
	...
}

You will start to wonder what this variable is, is it an object, string or a Boolean. One way to find out, is to scroll up and find the variable definition. This will take a time and will ruin your concentration from the purpose of the code.

Now if the variable was named like this

let isPublished = false ;

At the first glance, you know that this variable is a Boolean and using its descriptive name, you know that it indicates if something is published or not.

3 - Prefer whole-word nouns to abbreviations.

Notice that I said ‘preferred’. This is flexible and arbitrary and one that you can break if needed.

The only reason that you would consider using abbreviations over the whole word is the length of the word, but with modern IDEs and introduction of autocomplete this should not be an issue.

Your IDE will autofill the full name once you entered 3 or 4 letters, so take advantage of this feature and give a full descriptive names to variables.

// Bad Example
let p = {...,..}
// Good Example
let post = {...,...}

4 - Use Pronounceable Names

Using pronounceable names makes the code easy to read and discuss about. Doing so allows discussing/explaining code in plain English.

Bad Examples

// Bad Example
let modDateYYMMDD;

Good Examples

// Good Example
let modificationTimestamp;
let updatedAt;

5 - Remove Noise words

Noise words like Data, Value, Info, Variable, Table, String, Object, etc which are used as a suffix do not offer any meaningful distinction. Noise words are redundant and should be avoided.

6 - Avoid names that are misleading or potentially confusing

A variable shouldn’t have double meaning. The variable age should hold the name of something.

  • If names have a similar meaning, the we should avoid them.

    // Confusing names
    let employeeKey;
    let employeeID;
    

    In the future if we wanted to fix something, we will run in to a lot of issues with working with these variables.

  • If variables have similar name with different meaning

    // Confusing names
    let employeeRecs;
    let employeeReps;
    

    The difference in the spelling of these variables is just 1 letter, which can easily be missed and it will lead to a lot miss calculations in our code and the App.

  • Avoid multiple natural languages

    If your team uses English, then all the names should be English.

  • Avoid misspelled words in names

7 - All uppercase variables are usually imported variables

It is standard practice in programming that variables with all uppercase letters, is usually imported variables from .env or from a global constant variables files in your codebase. To avoid confusions, do not name any other variables with all uppercase.

API_BASE_URL
STRIPE_SECRET_KEY

8 - Avoid Magic Numbers

Don’t use magic number or unnamed numbers in your code

In the bellow code, if we come to our code some times later we will not remember what those numbers are and it will take us some time to figure it out.

const orbPeriod = Math.round(3.1415 * 6367);

But if we name those variables properly like bellow, we will instantly know what they are and what are they for. this will make our code readable for ourselves and others that might use our code in the future.

const PI = Math.PI;
const earthRadius = 6367.447;

const orbPeriod = Math.round(PI * earthRadius );

sometime it is better to show the process or calculation in the number is derived from for example in the code bellow, what do you think the number stands for

const time = 86400000;

The number is the milliseconds in a day. to make this more readable we can do these process

Conclusion

Best practices to write clean variable

  1. Use Descriptive Names (Intention-Revealing Names)
  2. Variable names should be nouns or noun phrases
  3. Prefer whole-word nouns to abbreviations.
  4. Use Pronounceable Names
  5. Remove Noise words
  6. Avoid names that are misleading or potentially confusing
  7. All uppercase variables are usually imported variables
  8. Avoid Magic Numbers

Add Comment

Conversations (0)

Sign up for our newsletter

Stay up to date with the latest news and articles.