Black Magic (programming)
   HOME

TheInfoList



OR:

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 black magic and deep magic (see 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

"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 extensive use of the structured control flow constructs of selection ( if/then/else) and repetition ( ...
, 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 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) $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 set of possible values and a set of allowed operations on it. A data type tells the compiler or interpreter how the programmer intends to use the data. Most progra ...
, 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 ANSI C, ISO C, and Standard C are successive standards for the C programming language published by the American National Standards Institute (ANSI) and ISO/IEC JTC 1/SC 22/WG 14 of the International Organization for Standardization (ISO) and t ...
, 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, 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. 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, 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.


Variants

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 where security through obscurity has been increasingly abandoned in favour of security through design which allows, and often encourages, public scrutiny. The ''
Jargon File The Jargon File is a glossary and usage dictionary of slang used by computer programmers. The original Jargon File was a collection of terms from technical cultures such as the MIT AI Lab, the Stanford AI Lab (SAIL) and others of the old ARPANET A ...
'' makes a distinction between deep magic, which refers to code based on esoteric theoretical knowledge, and black magic, which refers to code based on techniques that appear to work but which lack a theoretical explanation. It also defines heavy wizardry, which refers to code based on obscure or undocumented intricacies of particular hardware or software.‌ For an example, the infamous “You are not expected to understand this” comment: many readers of Lions Book, a didactically annotated commentary on the Unix version 6 kernel source code, interpreted this comment, found in the code, to suggest some deep theoretical wisdom was on display in the obscure snippet, but in fact its "magical" function was merely dependant on a peculiarity of the way the C Compiler was optimised for the
PDP-11 The PDP-11 is a series of 16-bit minicomputers sold by Digital Equipment Corporation (DEC) from 1970 into the 1990s, one of a set of products in the Programmed Data Processor (PDP) series. In total, around 600,000 PDP-11s of all models were sold, ...
hardware on which version 6 of Unix was designed to run.


See also

* Magic number (programming) * Black box *
Cargo cult programming Cargo cult programming is a style of computer programming characterized by the ritual inclusion of code or program structures that serve no real purpose. Cargo cult programming is symptomatic of a programmer not understanding either a bug they were ...
*
Nothing-up-my-sleeve number In cryptography, nothing-up-my-sleeve numbers are any numbers which, by their construction, are above suspicion of hidden properties. They are used in creating cryptographic functions such as hashes and ciphers. These algorithms often need rando ...


References

{{Reflist Computer programming folklore Software engineering folklore