Chapter 1: Indentation 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!), Which is like trying to define PI as 3 It's even more difficult to accept.
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 2: Location of the big symbol
Another problem with C programming style is the processing of braces. Unlike indentation, there is almost no reason to choose one without choosing another style, but there is a recommended style, it is kernighan and ritchie The classic book brought, it puts the brace of the beginning in the final line, and will end the brace in a row, 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 be non-consistent, but all the normal people understand: (first) K & R is ___ pair ___, (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 brackets are empty, __ In addition to __ which follows the continued symbol of the same statement. Such as "while" in the Do-While loop, or "else" in IF The statement is in the 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 simple. Different from Module-2 and Ascal 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.
If you are afraid to confuse your local variable name, there is another problem, just called Function-Growth-Hormone-ImbalanceSystem.
Chapter 4: Function
The 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. (This is not Unix) Style, 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 have done different situations. Many small things, write a longer function, it doesn't matter.
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: Comment
Note is a good thing, but too much comments are also dangerous, don't try to explain your code is how to get rid of: You should write the code better, rather than spend a lot of time to explain those Worse 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.
Chapter 6: You are already inadvertent.
Don't worry. You have the "Gun Emacs" that you have defended will automatically handle the source code format of C, and you have seen it is true, but, in the default, its role is not satisfactory. (In fact, they must be ugly than casually tapping - Ainfinite Number of Monkeys Typing Into GNU Emacs Would Never make a good program)
So, can you either use Gun Emacs, or let it use SareValules. Use the latter, you need to enter your .EMACS file in your. (Defun Linux-c-mode () "c mode with adjusted defaults For use with the linux kernel. "(SetQ C-Basic-Offset8)) This will define a command for MX Linux-c-mode. When you Hacking a module, how you will - * - linux-c - * - input in the first two lines, this mode will automatically work. And you may want to join the following
(setq auto-mode-alist ("/usr/src/linux.*/." In the file, this way, when you edit the file under / usr / src / linux, it will automatically switch to Linux-C-Mode.
However, if you can't let Emaces go automatically handle file format, don't be nervous, you have the same thing: "indent".
The indentation format of the GNU is also very dead. This is why you need to add a few command options. However, this is not too bad, because the GNU indent format creator also remembers the authority of K & R, (gnu no sin, they Just guided people in this thing), only the input option "-kr -i8" (indicating "K & R, indentation 8 characters).
"Indentation" has a lot of functions, especially when it suggests that you reform your code, you should look at the help. But remember: "indent" is not a very bad style of the program.