Magic number (programming)¶
A literal value embedded in program code whose purpose or provenance is not apparent from its local context, making behavior harder to understand and safely change.
Core Idea¶
In its code-smell sense, a magic number is an unexplained literal whose meaning is hidden rather than named. The program uses the raw value correctly only through unstated shared knowledge, so duplicated literals drift and maintainers cannot distinguish coincidental equality from one semantic parameter. The abstraction is therefore identified by a declared carrier, a transformation or constraint over that carrier, and an invariant that tells an analyst whether the named structure is genuinely present.
The load-bearing residual is not the broad topic of software engineering. It is opaque embedded constant whose missing name hides program intent. That residual remains recognizable when examples, notation, scale, or implementation change, but it disappears if the carrier is mistyped, the condition that the literal affects behavior and its intended semantic role cannot be recovered reliably from a nearby name, type or explicit explanation fails, a neighboring object is substituted, or notation and topical resemblance replace the constitutive test.
Scope of Application¶
Magic number (programming) belongs to software engineering and is useful where the analyst can specify source code, a literal numeric or byte value, local expression and control flow, intended domain meaning, named constant or documentation alternative, repeated uses, change requirements and reader interpretation, then evaluate the literal affects behavior and its intended semantic role cannot be recovered reliably from a nearby name, type or explicit explanation. The scope is broad within that domain but bounded by the need for the literal affects behavior and its intended semantic role cannot be recovered reliably from a nearby name, type or explicit explanation. This node covers the unexplained-literal code smell, not every unrelated computing use of the phrase magic number.
Clarity¶
The abstraction clarifies a crowded vocabulary by making the literal affects behavior and its intended semantic role cannot be recovered reliably from a nearby name, type or explicit explanation the center of the account. A claim should name the carrier, the governing operation or relation, the applicable assumptions, and the recognition test. A bare label is insufficient because the name Magic number (programming) can be used for a formal identity, an implementation, or a neighboring result unless carrier and convention are stated.
Manages Complexity¶
Without the abstraction, an analyst must reason directly over many local details: the carrier roles, admissibility assumptions, competing conventions, derived invariants, boundary cases, and proof or validation obligations specific to Magic number (programming). Magic number (programming) compresses them into the roles in the structural signature. That compression permits comparison across instances without erasing the variables that determine validity. It also exposes which details may be varied safely and which are constitutive.
Abstract Reasoning¶
- Identify the carrier. State what the elements, states, objects, or observations are: source code, a literal numeric or byte value, local expression and control flow, intended domain meaning, named constant or documentation alternative, repeated uses, change requirements and reader interpretation. Reject examples whose alleged carrier belongs to a different problem. 2. Lock the constitutive rule. Express the literal affects behavior and its intended semantic role cannot be recovered reliably from a nearby name, type or explicit explanation independently of one notation or implementation.
Knowledge Transfer¶
Knowledge transfers strongly among subfields of software engineering because they reuse source code, a literal numeric or byte value, local expression and control flow, intended domain meaning, named constant or documentation alternative, repeated uses, change requirements and reader interpretation, The program uses the raw value correctly only through unstated shared knowledge, so duplicated literals drift and maintainers cannot distinguish coincidental equality from one semantic parameter., and type the carrier, state every parameter and convention in the definition, test that the literal affects behavior and its intended semantic role cannot be recovered reliably from a nearby name, type or explicit explanation, compare the nearest accepted identity, and report counterexamples, uncertainty, and limiting cases.
Relationships to Other Abstractions¶
Current abstraction Magic number (programming) Domain-specific
Parents (1) — more general patterns this builds on
-
Magic number (programming) is a kind of Representation Prime
The proposed strict upward parent is
prime:representation.
Hierarchy path (1) — routes to 1 parentless root
- Magic number (programming) → Representation → Abstraction
Neighborhood in Abstraction Space¶
Magic number (programming) sits in a moderately populated region (46th percentile for distinctiveness): it has near-neighbors but no dense thicket of look-alikes.
Family — Syntax, Rewriting & Declarative Form (41 abstractions)
Nearest neighbors
- Programming language — 0.89
- Bytecode — 0.89
- Intermediate representation — 0.89
- Interpretation (logic) — 0.88
- Object file — 0.88
Computed from structural-signature embeddings · 2026-09-08