Magic (programming)

Magic (programming)

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 good connotations, implying that the interface simplifies an otherwise difficult or tedious task. 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, which describe arcane techniques that are deliberately hidden or extremely difficult to understand.

Contents

Referential opacity

In recent years, however, a negative interpretation of the term has been gaining popularity. In this usage, "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

Any SV [scalar value] may be magical, that is, it has special features that a normal SV does not have.
Larry Wallperlguts manual page, 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 magic may be documented or undocumented.

  • In ISO C, file handles (of type FILE) cannot be safely copied as their addresses[1] 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>) assigns the line read from the file to the variable $_, and applies the defined() function in the test so that any successfully read string, even "0" or the empty string, evaluates as true. This does not happen to <file-handle> anywhere else, or to while() with any other control expression.[2]
  • In an emulator, especially one in development, the emulated machine's system call addresses may be magic; when they are called, the emulator may run native code for increased speed or to talk to physical hardware, and set up the emulated CPU and memory as if it had executed the original code without actually doing so.
  • For instance, the CALL statement of BBC BASIC V is magic on the system call addresses of Acorn MOS; instead of branching to ARM code at those addresses, it raises a RISC OS software interrupt equivalent to the system call.[3] The effect is to emulate Acorn MOS sufficiently for 8-bit BASIC programs not containing assembly language to run unmodified.
  • 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.[3]
  • Any comment that has an effect on the code is magic.
  • Memory-mapped I/O addresses and volatile variables are magic, although the techniques are so common that the term is not normally applied.

See also

References

  1. ^ 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'." 
  2. ^ "perlop - perldoc.perl.org". 7 September 2010. http://perldoc.perl.org/perlop.html. Retrieved 17 February 2011. 
  3. ^ a b "27. Keywords" (PDF). BBC BASIC Reference Manual (1st ed.). Cambridge, England: Acorn Computers. October 1992. p. 229, 349. ISBN 1-852-50103-0. http://developer.riscos.com/PRM/BBCBASIC.PDF. Retrieved 9 May 2007. 

Wikimedia Foundation. 2010.

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

Look at other dictionaries:

  • Deep magic (programming) — In computer programming, 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… …   Wikipedia

  • Magic — Contents 1 Computing 2 Gaming 3 Film 4 Music …   Wikipedia

  • Magic 999 — City of license Preston Broadcast area Central Lancashire Slogan Best of the 60 s, 70 s and more Frequency 999 kHz …   Wikipedia

  • Magic Kids (TV channel) — Magic Kids Launched January 12 1995 Closed May 24 2006 Network Pramer Owned by Liberty Media Slogan Algo grande para los chicos Country …   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 105.4 FM — Broadcast area London Frequency 105.4 MHz Sky: 0180 TalkTalk TV: 617 Virgin Media (ex NTL): 928 Freeview: 715 First air date 9 July 1990 …   Wikipedia

  • Magic 1161 — Broadcast area East Riding of Yorkshire Northern Lincolnshire Frequency 1161 kHz First air date 12 February 1997 Format …   Wikipedia

  • Magic 828 — City of license Leeds Broadcast area West Yorkshire Slogan The Best of the 60s, 70s more Frequency …   Wikipedia

  • Magic 1548 — City of license Liverpool Broadcast area Liverpool, North West and North Wales Slogan Best of the 60s, 70s and more …   Wikipedia

  • Magic AM — City of license Sheffield Broadcast area South Yorkshire North Midlands Slogan The Best of the 60s, 70s more …   Wikipedia

Share the article and excerpts

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