Who wrote this code, it's horrible!

Hello everyone, I am Yupi, and I accidentally saw this picture on the Internet:

46a8a4c312b36d55d1575b073814d876.png

When I first saw this code, I was surprised that the author used an English word deadbeefto define macro constants!

I thought it was just a joke by a humorous programmer, but later I found out that the code in the picture above is the hash_map source code of C++! And the author's use of this special English word is also "ulterior motive".

The English literal translation of deadbeef is dead beef, but in the field of programming, it has a deeper meaning. Add this word 0x, convert it to uppercase, and you get a typical hexadecimal number: 0xDEADBEEF. This number is often used to identify newly allocated but uninitialized memory; in embedded systems it is also often used to indicate a program crash or deadlock, such as the IBM RS/6000 system running on a 32-bit PowerPC processor , Mac OS system.

Then I can't help but wonder why such a word was chosen instead of something like "FishPi" (just kidding, hex up to F).

022b67182f8d6e8c337a03c6f83f9028.png

I checked the Internet for a while, and the conclusion I got turned out to be: there is no reason, it is a "magic number" !

The so-called magic number is a constant that appears out of thin air for no reason, and does not need to explain its meaning. It's so capricious!

In addition to deadbeef, I also found a lot of magic numbers on Baidu, such as:

  • 0xBAADF00D ("bad food") is used by Microsoft's LocalAlloc (LMEM_FIXED) to indicate uninitialized allocated heap memory when using the debug heap

  • 0xDEADC0DE ("dead code") is used as a marker in OpenWRT firmware, at the end of static firmware to indicate the start of the jffs2 filesystem to be created

  • 0xDEAD10CC ("dead lock" deadlock) is used to indicate the crash report of the iOS system

Does it feel amazing? Maybe this is the romance of programmers.

Seeing this, I couldn't help it, and I also wrote a few magic numbers. Let's guess what it means:

redisLock.lease(86400);
if (fileSize > 1073741824) {
  ...doSomething
}
if (num > 2147483647) {
  printf("you lose");
}

These values ​​are often used when we write code, 84600 = 3600 * 24 means one day; 1073741824 = 1024 * 1024 * 1024 means 1 GB; and 2147483647 is the maximum value of the int type in programming languages ​​such as Java.

9f27315284108cc92895f8ab22f0f9db.png

I showed these codes to my friend, and he sneered: The magic numbers written by the big guys are called magic numbers, but what you write can only be called bad code.

479398b921bc70ca40d4af25afb65cc1.png

Indeed, unless it is the magic number recognized by the above-mentioned bosses/predecessors, we should try not to use magic numbers when writing code at ordinary times. It will seriously affect the readability of the code. We can "comment" these magic numbers by defining constants, such as:

int ONE_DAY = 86400;
int ONE_GB = 1073741824;
int MAX_INTEGER = 2147483647;

This is much clearer and reduces our risk of typing errors.

In addition to the magic numbers mentioned above, I have also seen some practical magic numbers online, such as the magic number in Quake by John Carmack, the father of modern 3D games:

i = 0x5f3759df - ( i >> 1 );

I can't believe that the above line of code can quickly calculate the reciprocal of the square root of a number!

Looking online, there are many papers devoted to this thing:

cce295013112351eaaf2d6132b097342.png

I have to sigh the charm of programming and the charm of mathematics! When will I be able to create a magic number that everyone knows?

5a325983dc8bbb994ea12ac3f9eb9c6d.png

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326610184&siteId=291194637