Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are "magic numbers" in computer programming?

When people talk about the use of "magic numbers" in computer programming, what do they mean?

like image 920
ckv Avatar asked Aug 19 '10 05:08

ckv


People also ask

What is magic number in computer?

In programming, a magic number is a constant value used to identify a file format, protocol or error code. In many file formats, the first few bytes identify the file; for example, "PK" in ZIP files and the hex values "F8 D8" in JPEG files.

What is magic number example?

Magic Number in Mathematics For example, 325 is a magic number because the sum of its digits (3+2+5) is 10, and again sum up the resultant (1+0), we get a single digit (1) as the result. Hence, the number 325 is a magic number. Some other magic numbers are 1234, 226, 10, 1, 37, 46, 55, 73, etc.

Which are the magic numbers?

magic number, in physics, in the shell models of both atomic and nuclear structure, any of a series of numbers that connote stable structure. The magic numbers for atoms are 2, 10, 18, 36, 54, and 86, corresponding to the total number of electrons in filled electron shells.

What are magic numbers C++?

A number is said to be magic number, when the recursive sum of the digits is 1. Suppose a number is like 50311 = 5 + 0 + 3 + 1 + 1 = 10 = 1 + 0 = 1, this is magic number. To check whether a number is magic or not, we have to add the digits until a single-digit number is reached.


2 Answers

Magic numbers are any number in your code that isn't immediately obvious to someone with very little knowledge.

For example, the following piece of code:

sz = sz + 729; 

has a magic number in it and would be far better written as:

sz = sz + CAPACITY_INCREMENT; 

Some extreme views state that you should never have any numbers in your code except -1, 0 and 1 but I prefer a somewhat less dogmatic view since I would instantly recognise 24, 1440, 86400, 3.1415, 2.71828 and 1.414 - it all depends on your knowledge.

However, even though I know there are 1440 minutes in a day, I would probably still use a MINS_PER_DAY identifier since it makes searching for them that much easier. Whose to say that the capacity increment mentioned above wouldn't also be 1440 and you end up changing the wrong value? This is especially true for the low numbers: the chance of dual use of 37197 is relatively low, the chance of using 5 for multiple things is pretty high.

Use of an identifier means that you wouldn't have to go through all your 700 source files and change 729 to 730 when the capacity increment changed. You could just change the one line:

#define CAPACITY_INCREMENT 729 

to:

#define CAPACITY_INCREMENT 730 

and recompile the lot.


Contrast this with magic constants which are the result of naive people thinking that just because they remove the actual numbers from their code, they can change:

x = x + 4; 

to:

#define FOUR 4 x = x + FOUR; 

That adds absolutely zero extra information to your code and is a total waste of time.

like image 153
paxdiablo Avatar answered Oct 02 '22 05:10

paxdiablo


"magic numbers" are numbers that appear in statements like

if days == 365 

Assuming you didn't know there were 365 days in a year, you'd find this statement meaningless. Thus, it's good practice to assign all "magic" numbers (numbers that have some kind of significance in your program) to a constant,

DAYS_IN_A_YEAR = 365 

And from then on, compare to that instead. It's easier to read, and if the earth ever gets knocked out of alignment, and we gain an extra day... you can easily change it (other numbers might be more likely to change).

like image 45
mpen Avatar answered Oct 02 '22 05:10

mpen