Magic Numbers and Magic strings

Source: Internet
Author: User

Many types of files start with a few bytes of content that are fixed (either intentionally populated, or this is the case). Depending on the contents of these bytes, the file type can be determined, so the contents of these bytes are called magic number. Also in some program code, programmers often refer to numeric constants or strings that appear in code but are not interpreted as magic numbers or magic strings.

The so-called magic numbers and Magic strings refer to numbers or strings that appear in the code but are not interpreted. If you use magic numbers in a program, you will probably not know what his meaning is in a few months or years.

The so-called magic numbers and Magic strings refer to numeric constants or strings that appear in the code but are not interpreted. If you use magic numbers in a program, you will probably not know what it means after a few months (or years).

Magic number, that is, when writing a program directly in the program to apply the numbers, rather than the way to define macros or const variables, Figure 1 is the use of magic number of an example program. 64 of these refer to the maximum number of bytes of MSK. As can be seen from this procedure, the minimum number of bytes in MSK is Min_msk_len, which is 20. The hazards of using magic numbers are:
1) Reduce the readability of the program. Someone might have suggested that it would be over with some comments? If it's really annotated, why not define it as a macro or const constant? Be aware that the efficiency of viewing annotations is certainly not as quick and convenient as looking at the code, and there is no problem with the different steps (the code and comments may not be synchronized).
2) If the next time the maximum value is changed from 64 to 128, the change should be made to each place in Adjustmask (). In addition, when a project is large, the presence of magic numbers makes the program maintenance very, very difficult.
From this, the "demon" should not be understood as magical as "magic", but rather as monster as "devil".

Example.c
00290: #define Min_msk_len 20
00291:
00292:int Adjustmsk (mskcontext* Context)
00293: {
00294:char Temp [64] = {0};
00295:
00296:if (Context->lenmsk > 64) {
00297:memcpy (temp, Context->msk + (context->lenmsk-64), 64);
00298: ...
00399:memcpy (Context->msk, temp, 64);
00300:}
00301:else if (Context->lenmsk < Min_msk_len) {
00302:return ERROR;
00303:}
00304: ...
00305:} Figure 1

Figure 2 is the version that follows the macro. It defines the size of the Max_msk_len as 64, which can also be referenced if the maximum value of the MSK is also required in other functions. If the next time you want to change the maximum from 64 to 128, just change the Max_msk_len macro definition. In addition, the existence of this macro definition facilitates sharing between modules and modules, thus improving reusability to some extent.

Example.c
00289: #define Min_msk_len 20
00290: #define Max_msk_len 64
00291:
00292:int Adjustmsk (mskcontext* Context)
00293: {
00294:char temp [Max_msk_len] = {0};
00295:
00296:if (Context->lenmsk > Max_msk_len) {
00297:memcpy (temp, Context->msk + (Context->lenmsk-max_msk_len),
00298:max_msk_len);
00399: ...
00300:memcpy (Context->msk, temp, max_msk_len);
00301:}
00302:else if (Context->lenmsk < Min_msk_len) {
00303:return ERROR;
00304:}
00305: ...
00306:} Figure 2

This article is from the "to Jianli" blog, be sure to keep this source http://yunli.blog.51cto.com/831344/265730

For example, a simple way to calculate salary based on a positionEditpublic int getsalary (String title, int grade) {if ("Programmer". Equals (title)) return grade * + 700;else if ("Tester". E Quals (title) return grade * + 800;else if ("Analyst". Equals (title)) Return Grade * 800 + 1000;} In this method, "Programmer", "Tester" and "Analyst" are the so-called Magic strings (Magic string), while 500, 700,800 and 1000 are the so-called magic numbers. At first glance, there's no problem with the code writing, but if you think about it, you'll find that if the strings and numbers that come in handy are scattered around the program, there are a lot of drawbacks. Let's start by analyzing three magic strings. Although the meaning of the three magic string is obvious and does not affect the readability of the code, it increases the probability of error and ignores the specific semantic context. It's easy to think that a word like "Programmer" is scattered across multiple methods, and a clerical error can create a bug. At the same time, "Programmer" represents a position in the method of calculating the salary, but in the method of counting the magazine that the company subscribes to, perhaps it will represent a magazine's name. However, this semantic environment can not be reflected by a simple "Programmer". Magic numberEditThe problem with magic number is even greater, first of all, it affects the readability of the code, who will know that 500 and 800 is the salary base, 700 is the subsidy? And what's worse, if the salary base changes, it's a depressing thing to find someone to update the 500,700,800 numbers one by one. If we had a constant-defined interface, the code would look beautiful: public int getsalary (String title, int grade) {if (Constants.title_ Programmer.equals (title)) return grade * Constants.base_salary_low + constants.allowance_low;else if (constants.title_ Tester.equals (title)) return grade * Constants.base_salary_low + constants.allowance_medium;else if (constants.title_ Analyst.equals (title)) return grade * Constants.base_salary_high + Constants.allowance_high;} From the above analysis, it is quite necessary to avoid using magic number and magic string in a project. The use of defined constants to access specific strings and numbers is also standard for software development. So should all numbers and strings be defined as constants? Maybe a friend would think that all numbers and strings should be defined as constants, but I think that each string really should be defined as a constant, but for numbers, if the semantics of the numbers themselves are not extended, they should not be defined as constants. For example, the index of an array should not be defined as a variable. Code like this: codeEditString building = address[constants.one];//in Constants the interface, one is defined as final int one = 1; You must think that this code is the superfluous, because it is 1. , it has no other special meaning, unlike the 500 and 700 in the code above. And if you really want to define this, there are hundreds of elements of the array, then you have to define hundreds of meaningless constants. Isn't it very ft? In the program in addition to 0,1,2 these few special-purpose numbers, the others are declared as constants. In short, the use of any strategy, or a degree of the most important. Magic number 2EditMany types of files start with a few bytes of content that are fixed (either intentionally populated, or this is the case). So the contents of these bytes are also known as magic number, because the file type can be determined based on the contents of these bytes. For example 1) The magic number of the ELF file on FreeBSD is the first four bytes of the file followed by "7f 4c 46", the corresponding ASCII string "^?" ELF. " 2) The magic number of the tar file is "Ustar" from the No. 257 byte. 3) PE file, after dos-root is a 32-bit signature and magic number 0x00004550 (image_nt_signature) (meaning "NT signature", that is, the PE signature; hexadecimal numbers 45 and 50 represent the ASCII code letters p and E, respectively. It makes any PE file a valid MS-dos executable file. The Unix command "file" should be used to work with this principle. Nuclear ScienceEditThe magic number is also known as the "magic number" of stable nuclides.

Magic Numbers and Magic strings

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.