ArticleslgStudy

computer science

Magic (programming)

Magic (programming) is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Magic (programming) rather than just read about it. In short: 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.

Key takeaways

  • Magic (programming) belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Magic (programming) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Magic (programming) from memory before moving on to harder problems.

Reference excerpt

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 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, 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:

While the code above is clear, 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:

Non-orthogonality Any SV [scalar value] may be magical, that is, it has special features that a normal SV does not have. 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 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 5, the statement while(<file_handle>) implicitly assigns the line read from the file by <file_handle> to the variable $_, and applies the defined() function to the expression so that any successfully read string, even "0" or the empty string, evaluates as true and continues the while() loop. This does not happen to <file_handle> anywhere else, or to while() 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 CALL 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 printf format strings, normally a type mismatch error. In JavaScript, evaluation of the typeof operator succeeds when the operand is an undeclared identifier, which would normally result in a ReferenceError. 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.

See also Magic number (programming) Black box Cargo cult programming Nothing-up-my-sleeve number

References

Worked examples

Example 1 — a first encounter with Magic (programming)

Start with the simplest possible case. Write down what Magic (programming) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Magic (programming) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Magic (programming) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Magic (programming)

In research
Magic (programming) appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Magic (programming) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Magic (programming) is common in secondary-school and first-year university syllabi. It links to neighbouring topics Computer programming folklore, Software engineering folklore, so understanding it makes those chapters shorter.
In everyday life
Look for Magic (programming) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Magic (programming)” →

How to study Magic (programming) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Magic (programming) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Magic (programming) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Magic (programming) in simple terms?

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 beha…

Why does Magic (programming) matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Magic (programming)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Magic (programming).

Tags

  • Computer programming folklore
  • Software engineering folklore

Keep exploring