Donβt Use Magic Numbers
Improve code clarity and searchability by replacing magic numbers with constants. Make your code more expressive and self-explanatory.
Usage
π Guideline
Don't Use Magic Numbers: Avoid using literal values in your code; instead, assign them to constants with meaningful names. Magic numbers make code hard to understand and maintain.
Numbers that appear in the code without explanation are known as "magic numbers." They make the code less readable and searchable. By assigning these values to constants with meaningful names, you can make the code more expressive and self-explanatory.
π οΈ How to Apply
- Define constants to represent magic numbers. π§ββοΈ
- Use meaningful names for the constants. π
- Replace magic numbers with the corresponding constants. β¨
- Update constants if the values need to change. π
Pros and Cons
π Pros
- Improved Readability: Constants with meaningful names make the code easier to understand. π
- Enhanced Maintainability: Updating constants instead of scattered magic numbers simplifies code maintenance. π§
- Searchability: Constants are easily searchable, allowing other developers to quickly locate and comprehend the purpose of the numbers used in the code. π
π Cons
- Increased Indirection: The use of constants introduces an additional layer of indirection, requiring developers to refer to the constants instead of the actual numbers. π
- Potential Overhead: Defining constants for every numerical value may result in an increase in the number of variables in your code, which can have a minor impact on performance and memory usage. β οΈ
Examples
β Bad
// Calculate the discount amount
function calculateDiscount(totalAmount: number): number {
return totalAmount - (totalAmount * 0.15);
}
β Good
const DISCOUNT_PERCENTAGE = 0.15;
// Calculate the discount amount
function calculateDiscount(totalAmount: number): number {
return totalAmount - (totalAmount * DISCOUNT_PERCENTAGE);
}
References
π Related Principles
- Use Descriptive Names: Meaningful names for constants are essential for code readability. π
- Avoid Magic Strings: Similar to magic numbers, using magic strings should be avoided. πͺ
- Avoid Hard-Coding: Don't hard-code values directly in your code; use configurable options instead. π«
- Separation of Concerns: By using constants, you separate the data from the logic. ποΈ