A magic string is a string literal (for example, "SELECT", "127.0.0.1") that is used in the middle of a block of code without explanation. It is considered bad practice to use magic strings because:

Replace the magic string with the existing named constant. This overcomes the two problems with magic strings:

The following example shows a magic string internal_ip. 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.