Improve coding style (super simple practical)

zhaozj2021-02-16  44

It should be said that many of us, the history of programming is not short, but many times, we have no confidence in the code written by yourself, sometimes I have seen it, 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

If you write down the following code in the program: for (i = 0; i <100; i ); for (i = 0; i <99; i ); no one knows 100, 99 What is the meaning, you It may mean that 100 is the boundary (maximum), you should give a definition, the reader of the code can understand what you mean: #define max 100 / * C language Macroembox * / const amount = 100; // C Language constant constant for (i = 0; i

转载请注明原文地址:https://www.9cbs.com/read-24467.html

New Post(0)