WIPIVERSE All articles

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 often carries negative 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 sometimes described as being done "automagically," a portmanteau 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 modern 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, 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 in PHP:

function magic()
{
    global $somevariable;
    echo $somevariable;
}

$somevariable = true;
magic();

While the above code is clear in isolation, in a large project it becomes difficult to understand where the function magic() gets its value from. The preferred approach is to pass the variable explicitly:

function noMagic($myvariable)
{
    echo $myvariable;
}

$somevariable = true;
noMagic($somevariable);

Non-Orthogonality

Larry Wall, the creator of Perl, defined magic in the context of Perl's internal data structures:

"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, Perl 5

This definition 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 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 OS 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.

Related Concepts

Magic is not to be confused with a magic number (programming), which refers to a numeric literal in source code whose meaning is unclear to the reader, or to a special constant used to identify a file format or protocol. Related concepts include the black box model (where internal workings are opaque) and cargo cult programming (where code is used ritualistically without understanding).

Browse

More topics to explore

    Browse all articles