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 The idiom tongue-in-cheek refers to a humorous or sarcastic statement expressed in a serious manner. History The phrase originally expressed contempt, but by 1842 had acquired its modern meaning. Early users of the phrase include Sir Walter Scot ...
, and often carries bad connotations, implying that the true behavior of the code is not immediately apparent. For example,
Perl Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offic ...
's
polymorphic typing In programming language theory and type theory, polymorphism is the provision of a single interface to entities of different types or the use of a single symbol to represent multiple different types.: "Polymorphic types are types whose operati ...
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 Variant may refer to: In arts and entertainment * ''Variant'' (magazine), a former British cultural magazine * Variant cover, an issue of comic books with varying cover art * ''Variant'' (novel), a novel by Robison Wells * "The Variant", 2021 e ...
), 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 A portmanteau word, or portmanteau (, ) is a blend of words

Referential opacity

"Magic" refers to
procedures Procedure may refer to: * Medical procedure * Instructions or recipes, a set of commands that show how to achieve some result, such as to prepare or make something * Procedure (business), specifying parts of a business process * Standard opera ...
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 In computer science, functional programming is a programming paradigm where programs are constructed by applying and composing functions. It is a declarative programming paradigm in which function definitions are trees of expressions that ...
. 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 PHP is a general-purpose scripting language geared toward web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementation is now produced by The PHP Group. ...
: 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, 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 Hard coding (also hard-coding or hardcoding) is the software development practice of embedding data directly into the source code of a program or other executable object, as opposed to obtaining the data from external sources or generating it at ...
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 of length zero. Formal theory Formally, a string is a finite, ordered sequence of characters such as letters, digits or spaces. The empty string is the special cas ...
, 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 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 software or use pe ...
, especially one in development, the emulated machine's
system call In computing, a system call (commonly abbreviated to 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, acc ...
points may be magic; when they are called, the emulator may run
native code In computer programming, machine code is any low-level programming language, consisting of machine language instructions, which are used to control a computer's central processing unit (CPU). Each instruction causes the CPU to perform a ver ...
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 a version of the BASIC programming language released in 1981 as the native programming language for the BBC Micro home/personal computer, providing a standardized language for a UK computer literacy project of the BBC. It was wr ...
V treats the system call addresses of
Acorn MOS The Machine Operating System (MOS) or OS is a discontinued computer operating system (OS) used in Acorn Computers' BBC computer range. It included support for four-channel sound, graphics, file system abstraction, and digital and analogue inp ...
magically; instead of attempting to branch to ARM code at those addresses, it raises a
software interrupt In digital computers, an interrupt (sometimes referred to as a trap) is a request for the processor to ''interrupt'' currently executing code (when permitted), so that the event can be processed in a timely manner. If the request is accepted, ...
in
RISC OS RISC OS is a computer operating system originally designed by Acorn Computers Ltd in Cambridge, England. First released in 1987, it was designed to run on the ARM chipset, which Acorn had designed concurrently for use in its new line of Archi ...
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 that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of websites use JavaScript on the client side for webpage behavior, of ...
, evaluation of the operator succeeds when the operand is an undeclared identifier, which would normally result in a . *Any
comment Comment may refer to: * Comment (linguistics) or rheme, that which is said about the topic (theme) of a sentence * Bernard Comment (born 1960), Swiss writer and publisher Computing * Comment (computer programming), explanatory text or informa ...
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. An alternative approach is using dedicated I/O pr ...
addresses and
volatile variable In computer programming, particularly in the C, C++, C#, and Java programming languages, the volatile keyword indicates that a value may change between different accesses, even if it does not appear to be modified. This keyword prevents an opti ...
s 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 Cryptography, or cryptology (from grc, , translit=kryptós "hidden, secret"; and ''graphein'', "to write", or ''-logia'', "study", respectively), is the practice and study of techniques for secure communication in the presence of adver ...
where
security through obscurity Security through obscurity (or security by obscurity) is the reliance in security engineering on design or implementation secrecy as the main method of providing security to a system or component. History An early opponent of security through o ...
has been increasingly abandoned in favour of security through design which allows, and often encourages, public scrutiny. The '' Jargon File'' 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 This page is intended to list all current compilers, compiler generators, interpreters, translators, tool foundations, assemblers, automatable command line interfaces ( shells), etc. Ada Compilers ALGOL 60 compilers ALGOL 68 compilers cf. ...
was optimised for the PDP-11 hardware on which version 6 of Unix was designed to run.


See also

*
Magic number (programming) In computer programming, a magic number is any of the following: * A unique value with unexplained meaning or multiple occurrences which could (preferably) be replaced with a named constant * A constant numerical or text value used to identify a ...
*
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