Magic string

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].

Contents

Background

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: A developer writes a program that creates forms asking for various information, filled into various text boxes, for details about customers. The user specification requires that if any form has a blank text box, that the application deletes that particular form. Otherwise the application compiles together all completed forms and saves them into a file (perhaps a database) when the user clicks any of the 'save' buttons. The form has a SaveAllForms button and a SaveAllFormsAndExit button and will prompt a warning if a textbox is empty. Among other things, the buttons, when pressed, do the check for the blank text boxes and perform the necessary warnings and deletions. If the compiler/interpreter cannot handle the empty text boxes at some later stage after the button presses, the developer might simply decide to place a dummy string "BLANK" into a textbox before it reaches the problem area of execution. When the problem area of code now sees the specific string "BLANK" it proceeds with the deletion of the form. The program passes all tests and is released to the user. After some time has passed with the program successfully in the field, a data entry clerk is entering information and is able to fill out all of the details, but for some reason does not have the phone numbers of the customers to put into the forms. Having lots to do, and having all of the other information, as well as knowing that no form can have an empty textbox or the information will be lost, the clever clerk decides to put in a placeholder value until he has the necessary information. He types in "BLANK" and proceeds to fill in the forms, making sure to save regularly along the way. At the end of the day, he does a save/exit and is satisfied knowing that all he will have to do the next morning to finish this task is reload the forms from the saved file and fill in the then-available phone numbers on their respective forms.

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:

  • 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.

References

  1. ^ Chris Falter (2008-03-06) (html), "A Good Solution for Magic String Data", "Egghead Cafe Tuturiols" at Egghead Cafe, http://www.eggheadcafe.com/tutorials/aspnet/b916e3a9-d056-4669-8bf3-aa98ed6669c3/a-good-solution-for-magi.aspx, retrieved 2009-05-11 
  2. ^ yohgaki (2001-07-25) (html), "Protect values (GET/POST/COOKIE) set by PHP Data", "Zend Technologies" at Zend Technologies, http://www.zend.com//code/codex.php?ozid=626&single=1, retrieved 2009-05-11 
  3. ^ Frank Naude (2008-12-06) (html), "NULL", "Oracle Wiki" at Oracle Wiki, http://www.orafaq.com/wiki/NULL, retrieved 2009-05-13 
  4. ^ Wang Lam (2003-05-21) (html), "The Behavior of NULL's in SQL", "Stanford University" at Stanford InfoLab, http://infolab.stanford.edu/~ullman/fcdb/oracle/or-nulls.html, 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 0596007124, ISBN 9780596007126
  6. ^ James McCaffrey (2009) (html), "Test Automation for ASP.NET Web Apps with SSL", "Microsoft" at MSDN, http://msdn.microsoft.com/en-us/magazine/cc163943.aspx, retrieved 2009-05-13 
  7. ^ Andrew Cumming; 2007, SQL Hacks, 1st ed., O'Reilly, pg. 174, Prevent an SQL Injection Attack, ISBN 0596527993, ISBN 9780596527990
  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 0764584359, ISBN 978-0764584350
  9. ^ Sezen, Tonguc Ibrahim; Isikoglu, Digdem (2007-04-27). FROM OZANS TO GOD-MODES: CHEATING IN INTERACTIVE ENTERTAINMENT FROM DIFFERENT CULTURES. p. 8. http://web.mit.edu/comm-forum/mit5/papers/sezen_isikoglu.pdf. Retrieved 2009-01-24. 

Wikimedia Foundation. 2010.

Игры ⚽ Нужна курсовая?

Look at other dictionaries:

  • Usrobotics sportster magic string — The USRobotics Sportster magic string was a modem AT command that exploited the fact that revision 4.1 USRobotics Sportster 14.4 modems had the exact same internal hardware and circuitry as the much more expensive USRobotics Dual HST 16.8 modems …   Wikipedia

  • Magic (Nick Drake song) — Magic Single by Nick Drake from the album Made to Love Magic Released 17 May 2004 …   Wikipedia

  • Magic (fantasy) — Magic in fiction is the endowing of fictional characters or objects with magical powers.Such magic often serves as a plot device, the source of magical artifacts and their quests. Magic has long been a component of fantasy fiction, where it has… …   Wikipedia

  • Magic in Your Eyes (album) — Magic in Your Eyes Studio album by Earl Klugh Released 1978 ( …   Wikipedia

  • Magic Touch (Loose Ends song) — Magic Touch Single by Loose Ends from the album So Where Are You? B side Magic Touch (Instrumental) Released May 1985 (UK) Format …   Wikipedia

  • Magic quotes — is a controversial feature of the PHP scripting language, which was introduced to help newcomers write functioning SQL commands without requiring manual escaping. It was later described and widely misinterpreted as help to prevent inexperienced… …   Wikipedia

  • Magic Time (Van Morrison album) — Magic Time Studio album by Van Morrison Released May 17, 2005 …   Wikipedia

  • Magic (Bruce Springsteen album) — Magic Studio album by Bruce Springsteen Released September 25, 2007 (vinyl record) …   Wikipedia

  • Magic Music Days — is a program put on by the Disneyland Resort, the Walt Disney World Resort, the Disneyland Resort Paris and the Hong Kong Disneyland Resort. It brings in school and community groups to perform in the parks and/or take part in workshops or clinics …   Wikipedia

  • String Theory (band) — String Theory was an electronic music band from Chicago that existed from 1997 until 2005. The members were Joshua R. Davison and Nathan T. Tucker. Contemporaries included Casino vs Japan, Pulseprogramming, and Telefon Tel Aviv.The two now work… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”