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:

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

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