Magic (programming)

Term for abstraction in computer programming


title: "Magic (programming)" type: doc version: 1 created: 2026-02-28 author: "Wikipedia contributors" status: active scope: public tags: ["computer-programming-folklore", "software-engineering-folklore"] description: "Term for abstraction in computer programming" topic_path: "technology/software-engineering" source: "https://en.wikipedia.org/wiki/Magic_(programming)" license: "CC BY-SA 4.0" wikipedia_page_id: 0 wikipedia_revision_id: 0

::summary Term for abstraction in computer 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

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:

::code[lang=php] function magic() { global $somevariable;

echo $somevariable;

}

$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:

::code[lang=php] function noMagic($myvariable) { echo $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.

  • In ISO C, file handles (of type ) cannot be safely copied as their addresses{{cite book | last = Banahan | first = Mike |author2=Brady, Declan |author3=Doran, Mark | title = The C book: Featuring the ANSI C standard | edition = 2nd | series = The Instruction Set | year = 1991 | publisher = Addison-Wesley Publishers | location = Wokingham, England | isbn = 0-201-54433-4 | page = 234 | chapter = 9.10.3 The stdio.h header file | quote = It is not safe to copy these objects within the program; sometimes their addresses may be 'magic'.
  • In Perl 5, the statement while() implicitly assigns the line read from the file by to the variable , and applies the function to the expression so that any successfully read string, even or the empty string, evaluates as true and continues the loop. This does not happen to anywhere else, or to with any other control expression.
  • 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 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.{{cite book | title = BBC BASIC Reference Manual | chapter-url = http://developer.riscos.com/PRM/BBCBASIC.PDF | accessdate = 9 May 2007 | edition = 1st |date=October 1992 | publisher = Acorn Computers | location = Cambridge, England | isbn = 1-85250-103-0 | pages = 229, 349 | pages = ix, 457, xvi -- | chapter = 27. Keywords
  • Also in BBC BASIC, not only does the numeric variable control print formatting, it accepts direct assignment of ANSI format strings, normally a type mismatch error.
  • In JavaScript, evaluation of the operator succeeds when the operand is an undeclared identifier, which would normally result in a .
  • 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.

References

References

  1. (5 October 2014). "perlguts – perldoc.perl.org".
  2. (7 September 2010). "perlop – perldoc.perl.org".

::callout[type=info title="Wikipedia Source"] This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page. ::

computer-programming-folkloresoftware-engineering-folklore