Magic number
A magic number is an unexplained numeric literal embedded in code whose meaning is not conveyed by a descriptive name or clear context.
Learn Haskell by Example
A magic number is a distinctive value in a file header that identifies the file format and sometimes its encoding variant.
- How it is used
- Uses header signatures to select PNM parsing and validate image data, then to categorize files before later processing.
- How deep it goes
- Builds parsers from fixed-string matching through remainder-carrying composition and branches header parsing on previously read markers.
- Examples
- Demonstrates PBM and PGM headers, P1–P6 parser cases, and byte signatures for BMP, PNG, JPEG, bzip2, and gzip.
- What you can do after
- Apply it via Data.ByteString and Haskell parsers to recognize supported file formats and reject inconsistent PNM data.
Learn AI-Assisted Python Programming, Second Edition
A magic number is an unexplained numeric literal embedded directly in code instead of being represented by a named parameter or variable.
- How it is used
- Keeps game behavior configurable by moving changeable numeric settings into parameters.
- How deep it goes
- Defines magic numbers and gives a review heuristic, but does not develop broader refactoring techniques.
- Examples
- Uses secret-code length and maximum guesses in a configurable guessing game.
- What you can do after
- Apply it by replacing changeable raw numbers with named parameters or variables.
Read how Learn AI-Assisted Python Programming, Second Edition explains magic number →
Also covered in
- CSS in Depth, Second Edition
- Grokking Software ArchitectureIn early access (MEAP)
Related concepts
- calc()addressed by · The CSS calc() function evaluates an arithmetic expression involving compatible CSS values and units to produce a computed value.
- enumalternative to · An enum is a type whose values are restricted to a finite set of named alternatives, which may carry associated data.
- constantaddressed by · A constant is a value or named program binding defined or treated as fixed, though whether reassignment or mutation is prohibited depends on the language.
- inputaddressed by · An input is information, a value, or an action supplied to a system, program, or computation for processing or to influence its behavior.
- maintainabilitycontrasts with · Maintainability is the degree to which software can be understood, modified, corrected, tested, or kept operational with reasonable effort over time.
Questions about magic number
What is magic number?
A magic number is an unexplained numeric literal embedded in code whose meaning is not conveyed by a descriptive name or clear context.
Which Manning books explain magic number?
Learn Haskell by Example and Learn AI-Assisted Python Programming, Second Edition.
What is the difference between magic number and enum?
A magic number is an unexplained numeric literal embedded in code whose meaning is not conveyed by a descriptive name or clear context. An enum is a type whose values are restricted to a finite set of named alternatives, which may carry associated data.
What is magic number also called?
Magic number is also called magic constant.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.

