Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Magic (programming)

From Wikipedia, the free encyclopedia
Term for abstraction in computer programming
Not to be confused withMagic number (programming).
For the Magic programming environment, seeMagic Software Enterprises.

In the context ofcomputer programming,magic is an informal term forabstraction; it is used to describe code that handles complex tasks while hiding that complexity to present a simple interface. The term is somewhattongue-in-cheek, and often carries bad connotations, implying that the true behavior of the code is not immediately apparent. For example,Perl'spolymorphic typing andclosure 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 described as being done "automagically", aportmanteau of "automatically" and "magically".

Referential opacity

[edit]
Main article:Referential transparency

Magic refers toprocedures which make calculations based on data not clearly provided to them, by accessing other modules, memory positions or globalvariables that they are not supposed to (in other words, they are not referentially transparent). According to most recentsoftware architecture models, even when usingstructured programming, it is usually preferredto make each function behave the same way every time the same arguments are passed to it, thereby following one of the basic principles offunctional programming. When a function breaks this rule, it is often said to contain "magic".

A simplified example of negative magic is the following code inPHP:

functionmagic(){global$somevariable;echo$somevariable;}$somevariable=true;magic();

While the code above is clear, if it is seen in a large project, it is often hard to understand where the functionmagic() gets its value from. It is preferred to write that code using the following concept:

functionnoMagic($myvariable){echo$myvariable;}$somevariable=true;noMagic($somevariable);

Non-orthogonality

[edit]

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,[1] Perl 5

This definition ofmagic ormagical can be extended to adata 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.

  • InISO C, file handles (of typeFILE) cannot be safely copied as their addresses[2] may be magic. That is, the runtime environment may place original file handles in ahard-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 typeFILE *, instead.
  • In Perl 5, the statementwhile(<file_handle>) implicitly assigns the line read from the file by<file_handle> to the variable$_, and applies thedefined() function to the expression so that any successfully read string, even"0" or theempty string, evaluates astrue and continues thewhile() loop. This does not happen to<file_handle> anywhere else, or towhile() with any other control expression.[3]
  • In anemulator, especially one in development, the emulated machine'ssystem call points may be magic; when they are called, the emulator may runnative 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, theCALL statement ofBBC BASIC V treats the system call addresses ofAcorn MOS magically; instead of attempting to branch to ARM code at those addresses, it raises asoftware interrupt inRISC OS equivalent to the system call.[4][non-primary source needed] 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 ANSIprintf format strings, normally a type mismatch error.[4]
  • InJavaScript, evaluation of thetypeof operator succeeds when the operand is an undeclared identifier, which would normally result in aReferenceError.
  • Anycomment that has an effect on the code is magic.
  • Memory-mapped I/O addresses andvolatile variables are also magic in this sense, although the term is not normally applied.

See also

[edit]

References

[edit]
  1. ^"perlguts – perldoc.perl.org". 5 October 2014. Retrieved18 February 2015.
  2. ^Banahan, Mike; Brady, Declan; Doran, Mark (1991). "9.10.3 Thestdio.h header file".The C book: Featuring the ANSI C standard. The Instruction Set (2nd ed.). Wokingham, England: Addison-Wesley Publishers. p. 234.ISBN 0-201-54433-4.It is not safe to copy these objects within the program; sometimes their addresses may be 'magic'.
  3. ^"perlop – perldoc.perl.org". 7 September 2010. Retrieved17 February 2011.
  4. ^ab"27. Keywords"(PDF).BBC BASIC Reference Manual (1st ed.). Cambridge, England: Acorn Computers. October 1992. pp. 229, 349.ISBN 1-85250-103-0. Retrieved9 May 2007.[dead link]
Retrieved from "https://en.wikipedia.org/w/index.php?title=Magic_(programming)&oldid=1335802896"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp