World Library  
Flag as Inappropriate
Email this Article

Magic string

Article Id: WHEBN0020096370
Reproduction Date:

Title: Magic string  
Author: World Heritage Encyclopedia
Language: English
Subject: Easter egg (media)
Publisher: World Heritage Encyclopedia

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]


The term comes from the fact that this type of problem is often seen in window 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.


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:

  • NullĀ != null[3][4] or any variation where a data type doesn't compare bitwise to a supposedly identical type. This is an issue that can even occur within the same development environment (same programming language and compiler). This problem has a long history for numerical and boolean types and most compilers handle this well (with applicable warnings and errors, default resolution, etc...). Nullable types such as strings have the difficulty of historically different definitions for NULL. The errors/warnings produced are often general or a 'best fit' default error whose message does not actually describe what's going on. If the developer can't get enough clues to track the issue down through debugging, taking a short cut, and coding in a 'default' string, may be the only way to keep the project on schedule. One solution to this may be the application of the Null Object pattern.[5]
  • Programmed into a corner. Sometimes a design seems straightforward and even simple but turns out to have a logical flaw, dependent upon the possible user inputs, due to an often unforeseen circumstance towards the end of planned development. Thus a developer might feel the need to implement a user input with special security/operational allowances to deal with such circumstances. This can be particularly ironic since it will sometimes become obvious that a more robust design from the beginning would likely have left room to handle the flaw. However this would perhaps have taken too much time to implement and it might have conflicted with the fundamental engineering concept of KISS, keeping a design and implementation simple and meeting only the initial necessary requirements.
  • Allowing external access to a global flag.[6] Over-confidence that a global flag can never be set accidentally or maliciously (often a quite reasonable assumption) justifies such implementation for testing and debug purposes, especially for small applications with simple interfaces. If the distribution of the program is considerable however, it is usually just a matter of time before somebody sets the flag. An obvious solution is to never use a global variable in such a manner. A developer might also make the flag circumstantially accessible. So the magic string by itself would be dealt with by the program as any other input.[7] The user has to then reproduce the setting as well as produce some collection of other events, that the user interface discretely allows, for the flag to accept the setting; a far more unlikely scenario, though notoriously still possible.

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.


This article was sourced from Creative Commons Attribution-ShareAlike License; additional terms may apply. World Heritage Encyclopedia content is assembled from numerous content providers, Open Access Publishing, and in compliance with The Fair Access to Science and Technology Research Act (FASTR), Wikimedia Foundation, Inc., Public Library of Science, The Encyclopedia of Life, Open Book Publishers (OBP), PubMed, U.S. National Library of Medicine, National Center for Biotechnology Information, U.S. National Library of Medicine, National Institutes of Health (NIH), U.S. Department of Health & Human Services, and, which sources content from all federal, state, local, tribal, and territorial government publication portals (.gov, .mil, .edu). Funding for and content contributors is made possible from the U.S. Congress, E-Government Act of 2002.
Crowd sourced content that is contributed to World Heritage Encyclopedia is peer reviewed and edited by our editorial staff to ensure quality scholarly research articles.
By using this site, you agree to the Terms of Use and Privacy Policy. World Heritage Encyclopedia™ is a registered trademark of the World Public Library Association, a non-profit organization.

Copyright © World Library Foundation. All rights reserved. eBooks from Project Gutenberg are sponsored by the World Library Foundation,
a 501c(4) Member's Support Non-Profit Organization, and is NOT affiliated with any governmental agency or department.