C language programming style
Chapter 1: The indent format Tab is 8 characters, so indentation is also 8 characters. There are many weird style, they define the indentation format as 4 characters (set to 2 characters!), This It is difficult to accept PI to be 3 as 3.
The reason is: The size of the indentation is to clearly define the beginning and end of a block. Especially when you have stayed in front of your computer for more than 20 hours, you will find a big indentation format makes you It is easier to understand.
Now, some people say that the use of 8 characters is used to make the code very close to the right. It is very uncomfortable to see the program on the 10-character width terminal screen. Answer Yes, but your program has more than 3 indentation Time, you should modify your program. In short, the indentation of 8 characters makes the program easy to read, and there is an additional benefit, that is, it will give you a problem when you make the program nested layer. At this time, you should modify your program.
Chapter II: The location of the big symbol is another C programming style problem is the processing of braces. Different indenges, there is almost no reason to choose one without choosing another style, but there is a recommended style Style, it is the classic book of kernighan and Ritchie, which puts the braces in one line, and will end the braces in the first place, as shown below:
IF (x is true) {
We do y} However, there is also a special case: Name function: The starting bracket is the first place in the next line, as follows: int function (int x) {body of function} All non-orthodox people will not be difficult Not consistency, but all the normal people understand: (first) K & R is right, (second) If k & r is wrong, see Article 1 (: -)) ... In addition, the function is also Special, not necessarily consistent.
It should be noted that the end of the parentheses is empty, except that it follows the continued symbol of the same statement. For example, "While" in the Do-While loop, or "else" in the IF statement. As follows :
Do {body of do-loop} while (condition); and if (x == y) {..} else if (x> y) {...} else {....
Reasons: K & R. In addition, it is noted that the placement method of this braces reduces the number of spaces, but there is no readability. So, when the screen size is limited, you can have more blank lines. To write some comments.
Chapter 3: Name System
C is a simple language, then naming should also be concise. Unlike Module-2 and Pascal language, C programmers do not use naming methods such as thisvariableisatemporaryCounter. A C language programmer will Name "TMP", it is easy to write, and it is not so difficult to understand.
However, when the name of the mixed type has to appear, the descriptive name is necessary for global variables. The function called a "foo" global is very annoyed. Global variable (only you must use Time to use it), just like global functions, you need a descriptive naming method. If you have a function to calculate the number of active users, you should name this - "count_active_users ()" - or additional similar In the form of, you should not be named "cntusr ()".
There is a kind of hungarian naming method, which writes the type of function to the variable name. This way is a manifestation of the brain with a problem - the compiler knows this type and then check it, and this will only confuse Programmer. - Know why Micro $ OFT produces so many "bug" programs !!.
The naming of local variables should be short. If you have a random integer loop counter, it may have "i", if there is no possibility that it can be misunderstood, write it "loop_counter" is low efficiency. Similarly, "TMP" can be a function variable for any temporary value.
Chapter 4: The function function should be short and charming, and it only makes only one thing. It should only cover one to two screens (80 * 24 screen) and only make a thing, and will do it. Isn't this a Unix style, the translator's note). The maximum length of a function and the complexity of the function and the indentation size are inversely proportional. So, if you have written a simple but long function, and you are already Different situations have made a lot of small things, writing a longer function is also meaningless.
However, if you want to write a very complex function, and you have estimated that if the average person reads this function, he may not know what this function is saying, this time, use a helpful function with a descriptive name.
Another thing to consider is the number of local variables. They should not exceed 5-10, otherwise you may be wrong. Reconize this function, divide them into smaller functions. People's brains can usually be easy 7 different things, more than this quantity will cause confusion. You know that you are very smart, but you may still want to understand what you do before 2 weeks.
Chapter 5: Note Annotation is a good thing, but too many comments are also dangerous, don't try to explain your code is how to comment: You should write the code better, not a lot Time to explain those bad code.
Typically, your comment is what your code is done, not how to do. And, to try to avoid plugging in a function body: If this function is indeed, you need some Note, you should go back to Chapter 4. You can write some short comments to indicate or warn those that you think is particularly smart (or extremely ugly), but you have to avoid too much. Instead, Note Write a function before the function tells others what it does, and why can you do this.