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:

Replace the magic number with the existing named constant. This overcomes the two problems with magic numbers:

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

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