Magic (programming)

From Infogalactic: the planetary knowledge core
Jump to: navigation, search

<templatestyles src="Module:Hatnote/styles.css"></templatestyles>

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

<templatestyles src="Module:Hatnote/styles.css"></templatestyles>

“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, therefore 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

<templatestyles src="Template:Blockquote/styles.css" />

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.

  • In ISO C, file handles (of type FILE) cannot be safely copied as their addresses[2] may be magic. That is, the runtime environment may place original file handles in a hard-coded address range, and not provide file handle behaviour to a user-created copy at another address. Consequently the standard library routines accept pointers to file handles, of type FILE *, instead.
  • In Perl 5, the statement while(<file_handle>) implicitly assigns the line read from the file by <file_handle> to the variable $_, and applies the defined() function to the expression so that any successfully read string, even "0" or the empty string, evaluates as true and continues the while() loop. This does not happen to <file_handle> anywhere else, or to while() with any other control expression.[3]
  • In an emulator, especially one in development, the emulated machine's system call points may be magic; when they are called, the emulator may run native code for convenience, speed or access to physical hardware, and set up the emulated CPU and memory as if it had executed the original code.
  • For instance, the CALL statement of BBC BASIC V treats the system call addresses of Acorn MOS magically; instead of attempting to branch to ARM code at those addresses, it raises a software interrupt in RISC OS equivalent to the system call.[4] The effect is to emulate Acorn MOS sufficiently for 8-bit BASIC programs not containing assembly language to run without modification.
  • Also in BBC BASIC, not only does the numeric variable @% control print formatting, it accepts direct assignment of ANSI printf format strings, normally a type mismatch error.[4]
  • Any comment that has an effect on the code is magic.
  • Memory-mapped I/O addresses and volatile variables are also magic in this sense, although the term is not normally applied.

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,[when?] especially in the field of cryptography, many aspects of which are now open to public scrutiny. The Jargon File makes a distinction 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. Lua error in package.lua at line 80: module 'strict' not found.
  2. Lua error in package.lua at line 80: module 'strict' not found.
  3. Lua error in package.lua at line 80: module 'strict' not found.
  4. 4.0 4.1 Lua error in package.lua at line 80: module 'strict' not found.