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

  • 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. πŸ—ƒοΈ