What is clean code? It is code that is easy to understand by humans and easy to change or extend.
In this post, I will cover JavaScript clean coding best practices when it comes to variables.
- Use meaningful and pronounceable variables. You should name your variables such that they reveal the intention behind it. This makes it easier to read and understand.
DON'T
let fName = "Stephanie";
DO
let firstName = "Stephanie";
Use ES6 constants when variable values do not change.
At this point, you have interacted with JavaScript ES6 severally/ a few times depending on your level of expertise therefore, keep this in mind.Use the same vocabulary for the same type of variable.
DON'T
getUserInfo();
getClientData();
getCustomerRecord();
DO
getUser();
- Use searchable names. This is helpful when you are looking for something or refactoring your code.
DON'T
setTimeout(blastOff, 86400000); //what is 86400000???
DO
const MILLISECONDS_IN_A_DAY = 60 * 60 * 24 * 1000; //86400000;
setTimeout(blastOff, MILLISECONDS_IN_A_DAY);
- Do not add unneeded context.
DON'T
const Laptop = {
laptopMake: "Dell",
laptopColor: "Grey",
laptopPrice: 2400
};
DO
const Laptop = {
make: "Dell",
color: "Grey",
price: 2400
};
Happy coding!
Top comments (1)
Descriptive variable names are really important for maintenability of code. 👏