Magic Number#
A “Magic Number” is a common code smell that refers to the use of raw, hardcoded numerical values in the code without any explanation or meaning. These numbers are often used in calculations, comparisons, or other operations, but their significance is not immediately clear to someone reading the code.
For example consider the number 7. What does it represent? Is it the number of days in a week? Does it mean Sunday? The 7th day of the month? Seventh Month? Part of the answer to Live the universe and everything? The maximum number of retries, or some other specific value? Without context or explanation, it’s hard to determine the purpose of the number, making the code less readable and maintainable.
Solution#
Extract variable and give it a name