Variables
Avoid global variables
Avoiding global variables can help improve the modularity and maintainability of code, as global variables can be accessed and modified from anywhere in the code, which can lead to unexpected behavior and make it more difficult to understand and debug the code.
Prefer const or let to declare variables
Declaring variables with const or let is preferred because it allows you to explicitly specify the intended scope and mutability of the variable. This can help improve the readability and maintainability of the code, and can also reduce the risk of unintended side effects and errors.
Avoid multiple declarations per statement
Having multiple declarations per statement can make the code harder to read and understand, and it can also increase the risk of mistakes, like creating involuntary global variables.
// 🚫 Bad
let x = 1, y = 2, z = 3;
// ✅ Good
let x = 1;
let y = 2;
let z = 3;
Avoid chain variable assignments
When you chain variable assignments, you are assigning the same value to multiple variables at the same time. Avoid chaining variable assignments in programming, as it can make your code harder to read and understand.
// 🚫 Bad
let x = y = z = 0;
// ✅ Good
let x = 0;
let y = 0;
let z = 0;
Avoid unused variables
Avoiding unused variables can help improve the readability and maintainability of the code, as it reduces clutter and makes it easier to understand the intended purpose and usage of each variable. It can also help catch potential issues and errors, as unused variables may indicate a mistake or oversight in the code.
Avoid magic numbers
Magic numbers can be any type of data, not just numbers. In programming, a magic number is a hard-coded constant value that is used in a program to represent a specific concept or idea.
Avoiding magic numbers can improve the readability and understandability of the code, as it makes it clearer what the purpose and significance of specific numerical values are. Using named constants instead of magic numbers can also make it easier to update and maintain the code, as the meaning of the constant is clear and can be changed in a single location rather than having to search and update the value throughout the code.
const DISCOUNT_25 = 25;
const DISCOUNT_50 = 50;
const DISCOUNT_75 = 75;
const calculateDiscount = (price, discountPercentage) => {
if (discountPercentage === DISCOUNT_25) {
return price * 0.75;
} else if (discountPercentage === DISCOUNT_50) {
return price * 0.5;
} else if (discountPercentage === DISCOUNT_75) {
return price * 0.25;
} else {
return price;
}
};
console.log(calculateDiscount(100, DISCOUNT_25)); // 75
console.log(calculateDiscount(100, DISCOUNT_50)); // 50
console.log(calculateDiscount(100, DISCOUNT_75)); // 25
console.log(calculateDiscount(100, 100)); // 100
Prefer explanatory variables over direct access
Explanatory variables are generally a better choice than direct access, as they can help improve the readability and maintainability of your code.
const address = "One Infinite Loop, Cupertino 95014";
const cityZipCodeRegex = /^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$/;
// Explanatory variables using array deconstruction
const [_, city, zipCode] = address.match(cityZipCodeRegex) || [];
saveCityZipCode(city, zipCode);