IT lexicon › Programming › Magic numbers

Magic numbers

Programming På svenska → Updated: 2026-05-24

Hard-coded numeric constants without a name or explanation in the code. Classic code smell. if (status == 7) instead of if (status == STATUS_DELETED).

The refactoring advice: extract into a named constant. Two exceptions traditionally accepted: 0 (loop start) and 1 (increment/initial size). Other meaning of the term: file format signatures — bytes at the start of files that identify the format (0x89504E47 = PNG, 0x504B0304 = ZIP). The file command identifies via magic numbers + the libmagic database.

← Back to the lexicon