Magic (programming)
Encyclopedia
In the context of computer programming, magic is an informal term for abstraction
Abstraction (computer science)
In computer science, abstraction is the process by which data and programs are defined with a representation similar to its pictorial meaning as rooted in the more complex realm of human life and language with their higher need of summarization and categorization , while hiding away the...

 - 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
Tongue-in-cheek
Tongue-in-cheek is a phrase used as a figure of speech to imply that a statement or other production is humorously intended and it should not be taken at face value. The facial expression typically indicates that one is joking or making a mental effort. In the past, it may also have indicated...

 and carries good connotations, implying that the interface simplifies an otherwise difficult or tedious task. For example, Perl
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...

's polymorphic typing
Type polymorphism
In computer science, polymorphism is a programming language feature that allows values of different data types to be handled using a uniform interface. The concept of parametric polymorphism applies to both data types and functions...

 and closure
Closure (computer science)
In computer science, a closure is a function together with a referencing environment for the non-local variables of that function. A closure allows a function to access variables outside its typical scope. Such a function is said to be "closed over" its free variables...

 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
Deep magic
In computer programming, 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, many aspects of which are now open to public scrutiny.The Jargon...

, which describe arcane techniques that are deliberately hidden or extremely difficult to understand.

Referential opacity

In recent years, however, a negative interpretation of the term has been gaining popularity. In this usage, "magic" refers to procedures which make calculations based on data not clearly provided to them, by accessing other modules, memory positions or global variables
Variable (programming)
In computer programming, a variable is a symbolic name given to some known or unknown quantity or information, for the purpose of allowing the name to be used independently of the information it represents...

 that they are not supposed to (in other words, they are not referentially transparent). According to most recent software architecture models
Software architecture
The software architecture of a system is the set of structures needed to reason about the system, which comprise software elements, relations among them, and properties of both...

, even when using structured programming
Structured programming
Structured programming is a programming paradigm aimed on improving the clarity, quality, and development time of a computer program by making extensive use of subroutines, block structures and for and while loops - in contrast to using simple tests and jumps such as the goto statement which could...

, it is usually preferred to make each function behave the same way every time the same arguments are passed to it, therefore following one of the basic principles of functional programming
Functional programming
In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state...

. 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
PHP is a general-purpose server-side scripting language originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document...

:


function Magic {
global $somevariable;

echo $somevariable;
}

$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) {
echo $myvariable;
}

$somevariable = true;

noMagic($somevariable);

Non-orthogonality

This definition of magic or magical can be extended to a data type
Data type
In computer programming, a data type is a classification identifying one of various types of data, such as floating-point, integer, or Boolean, that determines the possible values for that type; the operations that can be done on values of that type; the meaning of the data; and the way values of...

, code fragment, keyword, or machine address that has properties not shared by otherwise identical objects. The magic may be documented or undocumented.
  • In ISO C
    ANSI C
    ANSI C refers to the family of successive standards published by the American National Standards Institute for the C programming language. Software developers writing in C are encouraged to conform to the standards, as doing so aids portability between compilers.-History and outlook:The first...

    , file handles (of type FILE) 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 FILE *, instead.
  • In Perl
    Perl
    Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...

     5, the statement while(<file-handle>) assigns the line read from the file to the variable $_, and applies the defined function in the test so that any successfully read string, even "0" or the empty string, evaluates as true
    Logical value
    In logic and mathematics, a truth value, sometimes called a logical value, is a value indicating the relation of a proposition to truth.In classical logic, with its intended semantics, the truth values are true and false; that is, classical logic is a two-valued logic...

    . This does not happen to <file-handle> anywhere else, or to while with any other control expression.
  • In an emulator
    Emulator
    In computing, an emulator is hardware or software or both that duplicates the functions of a first computer system in a different second computer system, so that the behavior of the second system closely resembles the behavior of the first system...

    , especially one in development, the emulated machine's system call
    System call
    In computing, a system call is how a program requests a service from an operating system's kernel. This may include hardware related services , creating and executing new processes, and communicating with integral kernel services...

     addresses may be magic; when they are called, the emulator may run native code for increased speed or to talk to physical hardware, and set up the emulated CPU and memory as if it had executed the original code without actually doing so.
  • For instance, the CALL statement of BBC BASIC
    BBC BASIC
    BBC BASIC is a programming language, developed in 1981 as a native programming language for the MOS Technology 6502 based Acorn BBC Micro home/personal computer, mainly by Sophie Wilson. It is a version of the BASIC programming language adapted for a U.K...

     V is magic on the system call addresses of Acorn MOS
    Acorn MOS
    Acorn's Machine Operating System or OS was a computer operating system used in the Acorn BBC computer range. It included support for four-channel sound and graphics, file system abstraction, and digital and analogue I/O including a daisy-chained fast expansion bus...

    ; instead of branching to ARM code at those addresses, it raises a RISC OS
    RISC OS
    RISC OS is a computer operating system originally developed by Acorn Computers Ltd in Cambridge, England for their range of desktop computers, based on their own ARM architecture. First released in 1987, under the name Arthur, the subsequent iteration was renamed as in 1988...

     software interrupt
    Interrupt
    In computing, an interrupt is an asynchronous signal indicating the need for attention or a synchronous event in software indicating the need for a change in execution....

     equivalent to the system call. The effect is to emulate Acorn MOS sufficiently for 8-bit BASIC programs not containing assembly language to run unmodified.
  • Also in BBC BASIC, not only does the numeric variable @% control print formatting, it accepts direct assignment of ANSI printf format strings, normally a type mismatch error.
  • Any comment
    Comment (computer programming)
    In computer programming, a comment is a programming language construct used to embed programmer-readable annotations in the source code of a computer program. Those annotations are potentially significant to programmers but typically ignorable to compilers and interpreters. Comments are usually...

     that has an effect on the code is magic.
  • Memory-mapped I/O
    Memory-mapped I/O
    Memory-mapped I/O and port I/O are two complementary methods of performing input/output between the CPU and peripheral devices in a computer...

     addresses and volatile variable
    Volatile variable
    In computer programming, particularly in the C, C++, C#, and Java programming languages, a variable or object declared with the volatile keyword usually has special properties related to optimization and/or threading...

    s are magic, although the techniques are so common that the term is not normally applied.
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK