Magic string

A magic string is an input that a programmer believes will never come externally and which activates otherwise hidden functionality. A user of this program would likely provide input that gives an expected response in most situations. However, if the user does in fact innocently provide the pre-defined input, invoking the internal functionality, the program response is often quite unexpected to the user (thus appearing "magical").[1]

Background

The term comes from the fact that this type of problem is often seen in Windows Forms with editable text boxes for input.[2] Normally such errors occur as a result of time constraints. A developer must find a fast solution instead of delving more deeply into a problem and finding a better solution.

For example, when testing a program that took a user's personal details and verified their given credit card number, a developer may decide to add a magic string shortcut whereby entering the unlikely input of "***" as a credit card number would cause the program to automatically proceed as if the card was valid, without spending time verifying it. If the developer forgets to remove the magic string, and a user of the final program happens to enter "***" as a placeholder credit card number while filling in the form, the user would inadvertently trigger the hidden functionality.

Resolution

Situations/issues of cause

Often there are significant time constraints out of the developer's control right from the beginning of their involvement in a project. Common issues that might lead to this anti-pattern as a result:

Strict formatting

Restricting the format of the input is a possible maintenance (bug fixing) solution. Essentially, this means validating input information to check that it is in the correct format, in order to reduce the possibility of the magic string being discovered by the user. Examples include validating a telephone number to ensure that it contains only digits (and possibly spaces and punctuation to a limited extent) or checking that a person's name has a forename and a surname (and is appropriately capitalised). In any case, an exception is made for the magic string itself in the validation code. The idea is that, since any user would likely quickly realize the enforced strict formatting by trial and error, it would likely not occur to them to try inputting any string whatsoever; as such, the user is almost certain not to get as far as trying the magic string.

As with any input validation process, it is important to ensure that the format is not so restrictive that it unintentionally restricts some users from being able to use the application. An example of this is restricting telephone number or postal code[8] input based on one country's system (e.g. requiring every user to give a five-digit ZIP code), causing problems for legitimate users who are based in other countries.

Purposeful implementation

As is often the case with anti-patterns, there exist specific scenarios where magic strings are a correct solution for an implementation. Examples include cheat codes[9] and Easter eggs.

See also

References

  1. Chris Falter (2008-03-06), A Good Solution for Magic String Data, "Egghead Cafe Tuturiols" at Egghead Cafe, retrieved 2009-05-11
  2. yohgaki (2001-07-25), Protect values (GET/POST/COOKIE) set by PHP Data, "Zend Technologies" at Zend Technologies, retrieved 2009-05-11
  3. Frank Naude (2008-12-06), NULL, "Oracle Wiki" at Oracle Wiki, retrieved 2009-05-13
  4. Wang Lam (2003-05-21), The Behavior of NULL's in SQL, "Stanford University" at Stanford InfoLab, retrieved 2009-05-13
  5. Eric Freeman, Elisabeth Freeman, Kathy Sierra, Bert Bates; 2004, Head First Design Patterns, 1st ed., O'Reilly, Chapter 6, pg. 214, The Command Pattern, ISBN 0-596-00712-4, ISBN 978-0-596-00712-6
  6. James McCaffrey (2009), Test Automation for ASP.NET Web Apps with SSL, "Microsoft" at MSDN, retrieved 2009-05-13
  7. Andrew Cumming; 2007, SQL Hacks, 1st ed., O'Reilly, pg. 174, Prevent an SQL Injection Attack, ISBN 0-596-52799-3, ISBN 978-0-596-52799-0
  8. Brian Knight, Allan Mitchell, Darren Green, Douglas Hinson, Kathi Kellenberger; 2005, Professional SQL server 2005 integration services, 1st ed., John Wiley and Sons, Chapter 5, pg. 129, Handling Dirty Data, ISBN 0-7645-8435-9, ISBN 978-0-7645-8435-0
  9. Sezen, Tonguc Ibrahim; Isikoglu, Digdem (2007-04-27). "FROM OZANS TO GOD-MODES: CHEATING IN INTERACTIVE ENTERTAINMENT FROM DIFFERENT CULTURES". p. 8. Retrieved 2009-01-24.