In the context of computer programming, magic is an informal term for
abstraction
Abstraction is a process where general rules and concepts are derived from the use and classifying of specific examples, literal (reality, real or Abstract and concrete, concrete) signifiers, first principles, or other methods.
"An 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
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Though Perl is not officially an acronym, there are various backronyms in use, including "Practical Extraction and Reporting Language".
Perl was developed ...
'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
In linguistics, a blend—also known as a blend word, lexical blend, or portmanteau—is a word formed by combining the meanings, and parts of the sounds, of two or more words together. 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 Structured programming is a programming paradigm aimed at improving the clarity, quality, and development time of a computer program by making specific disciplined use of the structured control flow constructs of selection ( if/then/else) and repet ...
, 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
In computer science, functional programming is a programming paradigm where programs are constructed by Function application, applying and Function composition (computer science), composing Function (computer science), functions. It is a declarat ...
. 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
In computer science and computer programming, a data type (or simply type) is a collection or grouping of data values, usually specified by a set of possible values, a set of allowed operations on these values, and/or a representation of these ...
, 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
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 , instead.
*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
In formal language theory, the empty string, or empty word, is the unique String (computer science), string of length zero.
Formal theory
Formally, a string is a finite, ordered sequence of character (symbol), characters such as letters, digits ...
, evaluates as
true
True most commonly refers to truth, the state of being in congruence with fact or reality.
True may also refer to:
Places
* True, West Virginia, an unincorporated community in the United States
* True, Wisconsin, a town in the United States
* ...
and continues the loop. This does not happen to anywhere else, or to with any other control expression.
*In an
emulator
In computing, an emulator is Computer hardware, hardware or software that enables one computer system (called the ''host'') to behave like another computer system (called the ''guest''). An emulator typically enables the host system to run sof ...
, especially one in development, the emulated machine's
system call
In computing, a system call (syscall) is the programmatic way in which a computer program requests a service from the operating system on which it is executed. This may include hardware-related services (for example, accessing a hard disk drive ...
points may be magic; when they are called, the emulator may run
native code
In computer programming, machine code is computer program, computer code consisting of machine language instruction set architecture, instructions, which are used to control a computer's central processing unit (CPU). For conventional binary ...
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
BBC BASIC is an interpreted version of the BASIC programming language. It was developed by Acorn Computers Ltd when they were selected by the BBC to supply the computer for their BBC Literacy Project in 1981.
It was originally supplied on ...
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
RISC OS () is an operating system designed to run on ARM architecture, ARM computers. Originally designed in 1987 by Acorn Computers of England, it was made for use in its new line of ARM-based Acorn Archimedes, Archimedes personal computers an ...
equivalent to the system call.
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 format strings, normally a type mismatch error.
*In
JavaScript
JavaScript (), often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior.
Web browsers have ...
, 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
Memory-mapped I/O (MMIO) and port-mapped I/O (PMIO) are two complementary methods of performing input/output (I/O) between the central processing unit (CPU) and peripheral devices in a computer (often mediating access via chipset). An altern ...
addresses and
volatile variables are also magic in this sense, although the term is not normally applied.
See also
*
Magic number (programming)
*
Black box
In science, computing, and engineering, a black box is a system which can be viewed in terms of its inputs and outputs (or transfer characteristics), without any knowledge of its internal workings. Its implementation is "opaque" (black). The te ...
*
Cargo cult programming
*
Nothing-up-my-sleeve number
References
{{Reflist
Computer programming folklore
Software engineering folklore