Ok...so under your whizzy Linux systems, a number of different binary file types identify themselves by starting with a "magic number"...that is, a code which uniquely identifies that kind of file. Java class files, for instance, start with 0xCAFEBABE. Under inferior operating systems, your box will take its best guess based on the file extension. This can have results that are sometimes humorous, sometimes tragic.

In UNIX, "magic number" may refer to a constant placed at the beginning of a data file to indicate its nature. There is a file (/etc/magic) containing these constants or patterns.

When inventing a new file type, it is not a Good Idea to pick a number at random, because of the Birthday Paradox. If you pick a 32-bit number at random, there are over 2^32 =~ 4x10^9 possibilities, but you expect a collision to occur when just the square root of the space size is reached. This is likely to occur well before 80000 possibilities are chosen -- a far cry from the original space size!

A good idea about magic numbers is to make them into constants and put them prominently in a commented section at the beginning of the code, so that the poor bastard that has to modify your code does not have to discover why you decided that the array was to have exactly 63 elements.

Now don't tell me that your programming language does not allow constants. Even Perl does it now.

In baseball, the combined number of games that a team must win or a team behind it in the standings must lose in order to clinch a spot in the playoffs. Typically only used towards the end of the season. The current bastardized system of wild card teams somewhat dilutes this time honored tradition.

"Magic numbers" are one of the more cutely named phenomenon in atomic physics. Certain numbers of nucleons are preferred over the others, causing isotopes with the correct numbers of neutrons and protons to possess added stability.

The known magic numbers are Z (atomic mass) or N (atomic number) = 2, 8, 28, 50, 82, and 126. The first several of these correspond to the elements Helium, Oxygen and Nickel, all of which are more common in the universe than immediately adjacent elements. Isotopes with magic atomic mass typically have increased stability.

magic cookie = M = magic smoke

magic number n.

[Unix/C; common] 1. In source code, some non-obvious constant whose value is significant to the operation of a program and that is inserted inconspicuously in-line (hardcoded), rather than expanded in by a symbol set by a commented #define. Magic numbers in this sense are bad style. 2. A number that encodes critical information used in an algorithm in some opaque way. The classic examples of these are the numbers used in hash or CRC functions, or the coefficients in a linear congruential generator for pseudo-random numbers. This sense actually predates and was ancestral to the more common sense 1. 3. Special data located at the beginning of a binary data file to indicate its type to a utility. Under Unix, the system and various applications programs (especially the linker) distinguish between types of executable file by looking for a magic number. Once upon a time, these magic numbers were PDP-11 branch instructions that skipped over header data to the start of executable code; 0407, for example, was octal for `branch 16 bytes relative'. Many other kinds of files now have magic numbers somewhere; some magic numbers are, in fact, strings, like the !<arch> at the beginning of a Unix archive file or the %! leading PostScript files. Nowadays only a wizard knows the spells to create magic numbers. How do you choose a fresh magic number of your own? Simple -- you pick one at random. See? It's magic!

The magic number, on the other hand, is 7+/-2. See "The magical number seven, plus or minus two: some limits on our capacity for processing information" by George Miller, in the "Psychological Review" 63:81-97 (1956). This classic paper established the number of distinct items (such as numeric digits) that humans can hold in short-term memory. Among other things, this strongly influenced the interface design of the phone system.

--The Jargon File version 4.3.1, ed. ESR, autonoded by rescdsk.

This is about sense 1 of the Jargon File: a constant that is hardcoded throughout a program. It's so easy to do:

  if n_lines > 65 then
    throw_new_page

I don't care, you don't care, the printer doesn't care, exactly how many lines fit on a page. You know 65 will do. Around about then it's time to throw a new page. This is harmless if you only do it once.

Trouble brews if the exact number starts to become more important. Say it's harder to predict exactly what the structure of the page will be:

  if n_heading_lines + n_data_lines > 65 then
    throw_new_page

At this point you've no longer got one "65" you can find and fix if you really have to. You've got one routine handling a variable number of heading lines, and another doing data lines. Maybe each can overspill separately:

  if n_heading_lines > 10 then
    do_something_about_headings
  // ...
  // vast amounts of intervening code
  // ...
  if n_data_lines > 55 then
    store_current_data_position
  // ...
  // ... then mention 65 as well

Already you're stuffed. You have to know that the numbers 10, 55, and 65 are in there and related that way, or you have to read the whole damn program to find out. You can't just go to the bit about data lines and knock 55 down to 50 because you're introducing footers. The numbers should be linked, but if you use magic numbers like this, they aren't. What you have to do (really, you have to, even in elementary programs, as the most basic of habits) is something like this:

  const max_heading_lines = 10
  const max_data_lines = 55
  const max_footer_lines = 0
  const max_lines_on_page =
    max-heading_lines + max_data_lines + max_footer_lines

Then you've got one place to change them, and the code below still works when you did.

Real bastard programmers from hell will enjoy using what might be called fairy numbers: they're magic, and they disappear when you look for them. So you have to do something on line 55? That means you're allowed to keep going up to line 54:

   if n_data_lines <= 54 then
     print_data_line

If you've got an array of length 100, it's boring to keep testing for 100. Why not render everybody's life impossible after you by occasionally testing for 99 and/or 101? Combine this with the line break-up:

  if n_heading_lines >= 11 then
    do_something_about_headings
  // ...
  // vast amounts of intervening code
  // ...
  if n_data_lines <= 54 then
    store_current_data_position
  // ...

No-one will ever be able to hunt through programs and fix things like this. So it's very commonly used in, to name a language almost at random, COBOL.

Log in or register to write something here or to contact authors.