Magic (programming) explained

Magic (programming) should not be confused with Magic number (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 often 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 variants which describe arcane techniques that are deliberately hidden or extremely difficult to understand. However, the term can also be applied endearingly, suggesting a "charm" about the code.

The action of such abstractions is described as being done "automagically", a portmanteau of "automatically" and "magically".

Referential opacity

See main article: Referential transparency.

"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

$somevariable = true;

magic;

While the code above is clear, 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)

$somevariable = true;

noMagic($somevariable);

Non-orthogonality

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 or may not be documented.

See also

Notes and References

  1. Book: Banahan , Mike . Brady, Declan . Doran, Mark . The C book: Featuring the ANSI C standard . 2nd . The Instruction Set . 1991 . Addison-Wesley Publishers . Wokingham, England . 0-201-54433-4 . 234 . 9.10.3 The stdio.h header file . It is not safe to copy these objects within the program; sometimes their addresses may be 'magic'. .
  2. Web site: perlop – perldoc.perl.org. 7 September 2010. 17 February 2011.
  3. Book: BBC BASIC Reference Manual . http://developer.riscos.com/PRM/BBCBASIC.PDF . 9 May 2007 . 1st . October 1992 . Acorn Computers . Cambridge, England . 1-85250-103-0 . 229, 349 . 27. Keywords .