Improve coding style (super simple practical)
Author: System Date: 2003-02-28 Read: 238 should be said that many of us who program history is not short, but very often, the code we have written out but there is no self-confidence, and sometimes saw himself I am afraid, although this code achieves the requirements. It is often the reason, it is often the code of code. There is no beautiful code. The purpose of this article is to explain the general style, helping the reader to write "beautiful" code. In advance, the language involved in the text is C , C , Java, and Basic, I used three languages instead of using a language to indicate that the style is common to the language. 1) Identifier (naming rule) identifier should be intuitive and can be spent, and it is preferable that it is best to use English words or its combination, which is easy to remember and read, avoid using Chinese Pinyin to name. Long name can better express me, so the function name, variable name, class name for more than a dozen characters, for example: good name int student_age, teacher_age; bad name Int Age1, age2; but the name is What is the better? No, please see the example below: struct student {int student_age; / * bad name * / char * student_name;} struct student {int agent; / * Good name * / char * name;} Why is the former is not good? Because many, the name of the structure has expressed the meaning of Student in front of Student_age. For example, string copy functions: void stringcopy (char * str1, char * str2); we are hard to find out to copy Str1 to STR2, or just poured. You can make more sense, such as strsource and trdestination. This can be seen from the name, you should copy the strsource to strDestination. The name of a single character is also useful, common as i, j, k, m, n, x, y, z, etc., usually can be used as part variables within the function. 2) Priority of the operator If more operators in the code line should be used to determine the order of operation of the expression, avoid using the default priority. Because the priority of each operator is more difficult, if you are familiar with and correctly, the written code is easily generated and the readability is poor. Good style if ((a | b) && (a & c)) bad style if (A | B && A & C) is the same as the former function, but the latter is terrible, it is difficult to read. 3) Do not write too complex composite expressions. Composite expressions can make the code more concise in an appropriate occasion, but it cannot be complicated by this simple.
For example: max = a> b? (a> c? a: c): (B> C? B: c) // Composite expression is too complicated should be modified to: max = a; if (max
For pointer variables P, it is compared with zero value as follows: if (p == null) if (p! = Null) Do not write into IF (p == 0) // Easy to misunderstand P is integer variable IF (p! = 0) 5) Multi-layer IF statement does not have such a structure: if (condition1) {... if (condition2) ... if (condition3) ...} should be generated in IF-ELSE-IF structure: IF ( Condition1) {...} else if (condition2) {...} else if (condition3) {...} ... This kind of structural conditions clearly, the former is easy to cause the fact that I don't know what to write later. You can replace the nested IF statement with the Switch statement to implement multi-branch selection. 6) Improve the efficiency of the loop for string Name, look at the following cycle: for (i = 0; i