"Write beautiful, beautiful code."

Source: Internet
Author: User
Tags goto

Accidentally saw such an article on the 51cto, summed up a few of the programmers to write code considerations, several of them for my formal learning C-language children still need special attention, I hope to be able to adhere to the next days to learn code writing, and write beautiful code.

1. Identifiers naming rules:

Identifiers should be intuitive and can be spelt out, it is best to use English words or their combination, easy to remember and read, should not use Hanyu Pinyin to name.      Long names can better express meaning, so function name, variable name, class name up to more than 10 characters is not surprising, for example: good name int student_age,teacher_age; Bad named int age1,age2;

But is the name longer and better? No, look at the following example: struct student           {                  int   student_age;    /*  bad naming  */                 Char  *student_name;            }      struct student            {                 int  age;               /*  Good naming  */                Char  *name;           }   

Why the former is not good, because it is superfluous, the name student of the structure has already expressed the meaning of the student in front of Student_age. Another example is a string copy function: void Stringcopy (char *str1, char *str2); it's hard to figure out whether to copy str1 to the str2 or just the reverse. The parameter names can be made more meaningful, such as strsource and trdestination. This can be seen from the name of the strsource should be copied to the strdestination. Given's name is also useful, common such as i,j,k,m,n,x,y,z, which can often be used as local variables within a function.

2. Precedence of operators:

Try to use parentheses to control the order of operations, otherwise the program will be difficult to read.

3. Do not use too complex expressions, especially when nesting, be sure to use the appropriate separator separate.

4. Comparison of various data types and 0 values

In Java, for a Boolean variable flag, the way to compare 0 values (Note: not 0) is naturally if (flag== TRUE) or if (flag = FALSE), but this is not the right choice in C + +. The correct choice should be if (flag) or if (!flag), because there is no uniform standard for what the value of true is, for example, Visual C + + defines true as 1, and Visual Basic defines true as-1. if (flag = = TRUE), if (flag = = 1), if (flag = FALSE), if (flag = = 0) are unhealthy styles.

The 0 value of the pointer variable is NULL. Although the value of NULL is the same as 0, the two meanings are different. For the pointer variable p, it is compared to the 0 value if statement as follows: if (p = = null) if (P!= null)

Instead of writing: if (p = = 0)//easy to misunderstand p is an integer variable if (P!= 0)

5. If the multi-layer if statement uses if else if structure, so the structure looks more clear.

6. Improve cycle efficiency: for (i = 0; i < strlen (name); i++)//efficiency is significantly worse than the following loop: n = strlen (name);  for (i = 0; i < n; i++)

7. Use the goto statement sparingly, but not not disabled, because the goto statement can jump out of a lot of loops.

8. Elimination of demons:

The devil number of people, no name of the constant also, if you look at the English material, they say is magic data, some of our works translate it into "magic number", I prefer to translate it into "devil number", because it is a result of the code is very poor readability of the "devil."

If you write the following code in the program: for ( i=0 i < i++);  for (I=0 i < i++);

No one knows what 100 or 99 is, you may mean 100 is the bounds of the boundary (maximum), you should give the definition, the reader of the code to understand what you mean: #define MAX/* C language macro constant/ const int MAX = 100; C + + language const constant for (i=0 i < MAX; i++);  for The meaning of (i=0 i < MAX-1 i++) is clear.

And if a constant is closely related to other constants, the relationship should be included in the definition, and no orphaned values should be given.

9. Function name and return value type are semantically not conflicting, C standard library function GetChar violates this rule.

The GetChar's prototype return value is int, but it accepts a value of type char.

10. To avoid the creation of wild pointers, one solution is: When the pointer variable is created, either place null or give it the appropriate space. The second is that the pointer assigns the value to null after free or delete.      as follows: char *p = NULL; char *str = (char *) malloc (100);

These are some simple things to note in the code-writing style, where 6, 82 I used to make mistakes, and I hope to pay more attention to coding style in the days to come. Write "Beautiful and Beautiful Code". Go from: http://www.muxiaofei.com/write-beautiful-beautiful-code/

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.