Current Location: Home> Latest Articles> Use constants to replace magic numbers to improve code readability

Use constants to replace magic numbers to improve code readability

M66 2025-05-31

When writing programs, we usually encounter a problem: How to make the code easier to understand and maintain? Magic Numbers in PHP refer to those numbers that have no meaning and exist only as numerical values. They often appear in various parts of the code, which can make it difficult to understand why these numbers are used and may even introduce errors in future maintenance. To improve the readability of the code, we usually use constants to replace these magic numbers. This article will explore in-depth why this approach can improve the readability of the code.

What are magic numbers?

Magic numbers refer to values ​​that have no clear meaning. For example:

 $totalAmount = $price * 0.1;

In the above code, 0.1 is a magic number. It represents a specific percentage, which may be a discount rate or a tax rate. However, using 0.1 directly, other developers are not sure what this value represents when reading the code. More importantly, if we need to change this value in the future, we may forget to modify all places using 0.1 , resulting in an error.

Use constants to replace magic numbers

To solve this problem, we can use constants instead of magic numbers. Constants are meaningful names, they represent a fixed value and are not modified throughout the program. In this way, other developers or maintainers can clearly know the meaning of this value when reading the code. Here is an example of how to use constants to replace magic numbers:

 define('TAX_RATE', 0.1); // Define constants TAX_RATE

$totalAmount = $price * TAX_RATE; // Use constants instead of magic numbers

In this way, we not only give 0.1 a specific meaning (i.e., a tax rate of 10%), but when we need to modify this value, we just need to modify the definition of the constant without looking up and replacing magic numbers everywhere in the code.

Why do constants improve the readability of the code?

  1. Added comprehensibility : the name of a constant usually expresses the meaning it represents. Take TAX_RATE as an example, anyone who reads the code can understand it as a constant representing tax rates without spending time guessing the meaning of 0.1 .

  2. Reduced repetitive errors : If the same magic number is used in multiple places, when the requirements change, we only need to modify the definition of the constant, instead of modifying all the places where the magic number appears one by one. This can avoid omissions and reduce errors in code maintenance.

  3. Improves maintainability of the code : constants are usually defined globally, meaning that their values ​​are fixed and non-changeable. Modifying the value of a constant in the code will immediately affect everything that is used. In this way, the code modifications are more concentrated and clear, which helps to reduce misoperation.

  4. Added scalability of the code : using constants can leave more room for possible future changes. If you need to adjust the value of tax rates or other magic numbers in the future, just adjust the definition of the constant, instead of modifying each number in the code.

Applicable scenarios

Constants are suitable for numeric values ​​that are used multiple times in a program and have a fixed meaning. For example, tax rates, discount rates, pi, etc. These values ​​are not easily changed and have a specific business meaning, and using constants instead of them can make the code clearer.

Example: Use constants to replace magic numbers in URLs

Sometimes, some magic numbers or strings may be involved in the code, especially when dealing with URLs. If some parts of the URL have a fixed structure or meaning, we can also use constants to replace those parts. For example, consider the following code:

 $url = "https://example.com/api/v1/resource";

Here https://example.com is a magic string, meaning that if we need to change the domain name, we must modify each URL. In this case, we can use constants instead of domain names to ensure that the code is more flexible and easy to maintain.