[Good Programming habits] avoid using magic numbers

Source: Internet
Author: User

Magic number), that is, when writing a program, you can directly use a number in the program, instead of using a fixed macro or const variable. Figure 1 shows an example program using magic number. Among them, 64 refers to the maximum number of characters of the Msk. From this program, we can see that the minimum number of characters in the kms is MIN_MSK_LEN, that is, 20. The dangers of using magic numbers include:
1) reduced the readability of the program. Someone may ask, isn't it enough to add some comments? If the annotation method is used, why not define it as a macro or a const constant? You must know that the efficiency of viewing comments is certainly not as fast and convenient as directly viewing the code, and there is no problem that the code and comments may not be synchronized ).
2) If the next maximum value is changed from 64 to 128, you have to change each place in adjustMask. In addition, when a project is large, the existence of magic number will make program maintenance very difficult.
From this point of view, the "magic" here should not be understood as "magic)", but as "devil monster.

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 shows the version after the macro is used. The MAX_MSK_LEN size is defined as 64. If the maximum value of Msk is also required in other functions, this macro can also be referenced. If you want to change the maximum value from 64 to 128 next time, you just need to change the definition of MAX_MSK_LEN macro. In addition, the existence of such macro definition facilitates sharing between modules, thus improving reusability to a certain 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 from "to Jane Li cloud" blog, please be sure to keep this source http://yunli.blog.51cto.com/831344/265730

Related Article

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.