Magic (programming)

Not to be confused with Magic number (programming).
"Deep magic" redirects here. For the concept in the Chronicles of Narnia book series, see The Lion, the Witch and the Wardrobe.

In the context of computer programming, magic is an informal term for abstraction; it is used to describe code that handles complex tasks while hiding that complexity to present a simple interface. The term is somewhat tongue-in-cheek and carries bad connotations, implying that the true behavior of the code is not immediately apparent. For example, Perl's polymorphic typing and closure mechanisms are often called "magic". The term implies that the hidden complexity is at least in principle understandable, in contrast to black magic and deep magic (see Variants), which describe arcane techniques that are deliberately hidden or extremely difficult to understand. The action of such abstractions is described as being done "automagically", a portmanteau of "automatically" and "magically".

Referential opacity

"Magic" refers to procedures which make calculations based on data not clearly provided to them, by accessing other modules, memory positions or global variables that they are not supposed to (in other words, they are not referentially transparent). According to most recent software architecture models, even when using structured programming, it is usually preferred to make each function behave the same way every time the same arguments are passed to it, thereby following one of the basic principles of functional programming. When a function breaks this rule, it is often said to contain "magic".

A simplified example of negative magic is the following code in PHP:

function Magic() {
  global $somevariable;

  echo $somevariable;
}

$somevariable = true;

Magic();

While the code above is clear and maintainable, if it is seen in a large project, it is often hard to understand where the function Magic() gets its value from. It is preferred to write that code using the following concept:

function noMagic($myvariable) {
  echo $myvariable;
}

$somevariable = true;

noMagic($somevariable);

Non-orthogonality

Any SV [scalar value] may be magical, that is, it has special features that a normal SV does not have.
Larry Wall, perlguts manual page,[1] Perl 5

This definition of magic or magical can be extended to a data type, code fragment, keyword, or machine address that has properties not shared by otherwise identical objects. The magical properties may be documented or undocumented.

Variants

Deep magic refers to techniques that are not widely known, and may be deliberately kept secret. The number of such techniques has arguably decreased in recent years, especially in the field of cryptography, many aspects of which are now open to public scrutiny. The Jargon File makes a distinction[5] between deep magic, which refers to code based on esoteric theoretical knowledge, and black magic, which refers to code based on techniques that appear to work but which lack a theoretical explanation. It also defines heavy wizardry, which refers to code based on obscure or undocumented intricacies of particular hardware or software. All three terms can appear in source code comments. For example:

// Deep magic begins here...
DeepMagic ();

See also

References

  1. "perlguts - perldoc.perl.org". 5 October 2014. Retrieved 18 February 2015.
  2. Banahan, Mike; Brady, Declan; Doran, Mark (1991). "9.10.3 The stdio.h header file". The C book: Featuring the ANSI C standard. The Instruction Set (2nd ed.). Wokingham, England: Addison-Wesley Publishers. p. 234. ISBN 0-201-54433-4. It is not safe to copy these objects within the program; sometimes their addresses may be 'magic'.
  3. "perlop - perldoc.perl.org". 7 September 2010. Retrieved 17 February 2011.
  4. 1 2 "27. Keywords". BBC BASIC Reference Manual (PDF) (1st ed.). Cambridge, England: Acorn Computers. October 1992. pp. 229, 349. ISBN 1-85250-103-0. Retrieved 9 May 2007.
  5. "Deep Magic". Jargon File.
This article is issued from Wikipedia - version of the 11/22/2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.