DEV Community

Himanshu Kanojiya
Himanshu Kanojiya

Posted on • Updated on

Writing Clean Code in JavaScript - Variables

Prerequisite: This blog assumes, you have basic knowledge of JavaScript.

This blog covers writing good variables names to maintain code cleanability in JavaScript. What is Variable?, Why writing good names variables crucial for program & Software? How to write a good name for variables?

Did you know writing bad codes leads to a lack of productivity and unnecessary delays, and difficulties to add functionality in the software later.

It might be easy to write quickly messy code but later it will add unnecessary delays and difficulties.

What are Variables?

Variable's are like a container where we store values or program-related data and then access them later to use that data.

Why writing good variable names crucial for program/software?

  1. With a good name, you and other developers can easily distinguish what kind of value it has.
  2. With a good naming, you don't need to read variable full-body or scroll to that position where it has initialized to find what kind of value it has.
  3. With a good naming, you can have an idea about what variable is for what value.
  4. With a good naming, you don't have to write unnecessary comments in the code. For, an example: What value variable has, and it is for what

In the end, good naming will save your lot of time in future.

So, how to write a good name for variables?

Well, it depends on the value you are storing, but here are a few checks which you can use to write a good variables name in JavaScript:

  1. If the value is an object, then use some descriptive name for it. An object can have many data about the users or person or can be a group of data. Example: userInfo, database, authenticatedUserData, etc.
  2. If the value is a number or string, use a descriptive name, like username, age.
  3. If the value is a boolean, use names that answer a true or false to questions. Example: isUserActive, isUserAuthenticated, isLoggedIn.
  4. If your value is constant, then follow the descriptive name pattern and declare it in uppercase.
  5. Don't write too long name where you have to scroll to read

Note: The ability to write clean codes instantly comes with experience and practice. Don't force yourself too hard to do it, try to follow this principle slowly to adapt to these things.

Top comments (0)