Magic (programming)
2641649
226112426
2008-07-16T21:39:47Z
TomCerul
2309
/* See also */ pulled black magic, it redirects to deep magic, already linked
{{refimprove|article|date=September 2007}}
In the context of computer programming, '''magic''' is an informal term for [[abstraction (computer science)|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 carries good connotations, implying that the interface simplifies an otherwise difficult or tedious task. For example, [[Perl]]'s [[type polymorphism|polymorphic typing]] and [[closure (computer science)|closure]] mechanisms are often called "magic". The term implies that the hidden complexity is at least in principle understandable, in contrast to [[black magic (programming)|black magic]] and [[deep magic]], which describe arcane techniques that are deliberately hidden or extremely difficult to understand.
==Referential non-transparency==
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 [[variable]]s that they are not supposed to (in other words, they are not [[Referential transparency (computer science)|referentially transparent]]). According to most recent [[Software architecture|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, therefore 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]]:
<code>
function Magic() {
global $somevariable;
echo $somevariable;
}
$somevariable = true;
Magic();
</code>
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:
<code>
function noMagic($myvariable) {
echo $myvariable;
}
$somevariable = true;
noMagic($somevariable);
</code>
== Non-orthogonality ==
<blockquote>"Any SV [scalar value] may be magical, that is, it has special features that a normal SV does not have." — [[Larry Wall]], in Perl 5 manual page ''perlguts(1)''</blockquote>
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 magic may be documented or undocumented.
* In [[ANSI C|ISO C]], file handles (of type <code>FILE</code>) cannot be safely copied as their addresses<ref>{{cite book
| last = Banahan
| first = Mike
| coauthors = Brady, Declan; Doran, Mark
| title = The C book: Featuring the ANSI C standard
| edition = 2nd ed.
| series = The Instruction Set
| year = 1991
| publisher = Addison-Wesley Publishers
| location = Wokingham, England
| isbn = 0-201-54433-4
| page = 234
| pages = xiv, 310 pp
| chapter = 9.10.3 The <code>stdio.h</code> header file
| quote = It is not safe to copy these objects within the program; sometimes their addresses may be 'magic'.
}}</ref> 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 user-created copies at another address.
* In [[Perl]] 5, the statement <code>while(<</code><var>file-handle</var><code>>)</code> assigns the line read from the file to the variable <code>$_</code>, and evaluates <code>"0"</code> and the empty string as [[Logical value|true]]. This does not happen to <code><</code><var>file-handle</var><code>></code> anywhere else, or to <code>while()</code> with any other control expression.<ref name="perlop">Perl 5.005, manual page ''perlop(1)''</ref>
* Another Perl example is the auto-increment operator <code>++</code> which treats a string variable as an alphanumeric [[odometer]] when it is not numeric and has never been used in a numeric context. Thus it may behave differently on two variables that are equal at face value.<ref name="perlop"/>
* In [[Microsoft Excel]] 97 the cell range <code>X97:L97</code> is magic inasmuch as it helps enable the [[flight simulator]] [[Easter egg (media)|Easter egg]].
* The <code>CALL</code> statement of [[BBC BASIC]] V ([[RISC OS]]) is magic on certain target addresses; instead of branching to ARM code at those addresses, it issues software interrupts corresponding to the [[system call]]s of [[Acorn MOS]].<ref name="bbcbasic">{{cite book
| title = BBC BASIC Reference Manual
| url = http://developer.riscos.com/PRM/BBCBASIC.PDF
| format = PDF
| accessdate = 2007-05-09
| edition = 1st ed.
| year = 1992
| month = October
| publisher = Acorn Computers
| location = Cambridge, England
| isbn = 1-852-50103-0
| page = 240-1, 349
| pages = ix, 457, xvi pp
| chapter = 27. Keywords
}}</ref>
* Also in BBC BASIC, not only does the numeric variable <code>@%</code> control print formatting, it accepts direct assignment of ANSI <code>printf</code> format strings, normally a type mismatch error.<ref name="bbcbasic"/>
* Any [[comment (computer programming)|comment]] that has an effect on the code is magic.
* In an [[emulator]], especially one in development, the emulated machine's system call 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.
* [[Memory-mapped I/O]] addresses and [[volatile variable]]s are magic, although the techniques are so common that the term is not normally applied.
== See also ==
*[[Magic number (programming)]]
*[[Deep magic (programming)]]
*[[Voodoo programming]]
== References ==
<references/>
[[Category:Computer folklore]]