February 5, 2021 10:15 am GMT
Original Link: https://dev.to/stephanieopala/writing-clean-javascript-code-variables-2kij
Writing clean JavaScript code: Variables
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";
Enter fullscreen mode Exit fullscreen mode
DO
let firstName = "Stephanie";
Enter fullscreen mode Exit fullscreen mode
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();
Enter fullscreen mode Exit fullscreen mode
DO
getUser();
Enter fullscreen mode Exit fullscreen mode
- Use searchable names.This is helpful when you are looking for something or refactoring your code.
DON'T
setTimeout(blastOff, 86400000); //what is 86400000???
Enter fullscreen mode Exit fullscreen mode
DO
const MILLISECONDS_IN_A_DAY = 60 * 60 * 24 * 1000; //86400000;setTimeout(blastOff, MILLISECONDS_IN_A_DAY);
Enter fullscreen mode Exit fullscreen mode
- Do not add unneeded context.
DON'T
const Laptop = { laptopMake: "Dell", laptopColor: "Grey", laptopPrice: 2400};
Enter fullscreen mode Exit fullscreen mode
DO
const Laptop = { make: "Dell", color: "Grey", price: 2400};
Enter fullscreen mode Exit fullscreen mode
Happy coding!
Original Link: https://dev.to/stephanieopala/writing-clean-javascript-code-variables-2kij
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To