A magic number is a numeric literal (for example, 8080, 2048) that is used in the middle of a block of code without explanation. It is considered bad practice to use magic numbers because:

Assign the magic number to a new named constant, and use this instead. This overcomes the two problems with magic numbers:

The following example shows a magic number timeout. This should be replaced by a new named constant, as shown in the fixed version.

  • R. C. Martin, Clean Code: A Handbook of Agile Software Craftsmanship, §17.G25. Prentice Hall, 2008.