C language programming style

xiaoxiao2021-03-06  53

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 unconfiring that the PI is defined as 3. The reason is that 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 It will find a large indentation format makes your understanding of the program. Now, some people say that the use of 8 characters is used to make the code very close to the right side, watch the program very much on the 10-character width terminal screen. Uncomfortable. Answer is, but your program has more than 3 indentation, you should modify your program. In short, the indentation of 8 characters makes the program easy to read, there is an additional benefit, that is, it can Give you a warning when you get the program nested layers. At this time, you should modify your program. Chapter II: Location of the big symbol Di-program programming style problem is the processing of braces Different from the size of the moisture, there is almost no reason to choose one without choosing another style, but there is a recommended style, it is the classic book of kernighan and Ritchie, it will start Brackets are placed in the end of the line, and the end of the brace is placed in a row, as shown below: if (x is true) {we do y} However, there is a special case: Name function: Started parentheses It is the first one in the next line, as follows: int function (int x) {body of function} All non-orthodox people will not be difficult this inconsistency, 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. Need to pay attention to the end Brackets are empty, __ In addition to __ it follows the continued symbol of the same statement. As "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 f f (x> y) {...} else {....} Reason: K & R. In addition, It is noted that the placement method of this braces reduces the number of spaces, but does not reduce readability. So, when the screen size is limited, you can have more blank lines to write some comments. Chapter 3: Naming System C is a simple language, then naming should also be concise. With module-2 Unlike the Ascal language, C programmers do not use naming methods such as thisvariableisatemporaryCounter. A C language is named "TMP", which is easy to write, and not so hard to understand. However, When the name of the mixed type has to appear, the descriptive name is necessary for global variables. Calling a function of "Foo" global function is very annoyed. Global variable (only when you have to use Only 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 be named - "Count_Active_Users ()" - or different similar form You should not name "cntusr ()". There is a 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 you will check it, and this will only confuse programmers. - Why do Micro $ OFT produces so many "bug" procedures !!. Local variables should be short and delicate. If you have a random integer The loop counter, it may have "i", if there is no such thing as it can be misunderstood, write it "loop_counter" is low efficiency. Similarly, "TMP"

It can be a function variable for any temporary value. If you are afraid to confuse your local variable name, there is another problem, that is, called Function-Growth-Hormone-ImbalanceSyndrome. Chapter 4: The function function should be short and charming, and it only I do one thing. It should only cover one to two screens (80 * 24 screen) and only make one thing, and do it well. (This is not a Unix style, translator's note). One The maximum length of the function and the complexity of the function are inversely proportional. So, if you have written a simple but long-length function, and you have done a lot of things to different situations, write one more A longer function is also indispensable. However, if you want to write a very complex function, you have estimated that if the average person reads this function, he may not know what this function is saying, this time, use of descriptive The name has a helpful function. The other needs to consider the number of local variables. They should not exceed 5-10, otherwise you may be wrong. Recise this function, divide them into smaller functions. Human The brain can usually be easy to remember 7 different things. You will cause confusion. You know that you are smart, but you may still want to understand what you do before 2 weeks. Chapter 5: Note Note is a Good things, but too much comments are also dangerous, don't try to explain how your code is how to comment: You should write the code better, not a lot of time to explain those bad code Usually, your annotation is what your code is done, not what to do. And, to try to avoid plugging in a function body: If this function is indeed, you need some Comments, 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, Write the comment before writing, tell others what it does, and why maybe why do you want to do it. Chapter 6: You are already in one. Don't worry. You may have defended "Gun Emacs" will be automatically helped You handle C source code format, and you have seen it is true, but, in the default, its role is still unreasonable (in fact, they are more ugly than casually tapping things - ainfinite number So you can either use the Gun Emacs. Use SareValules. Use the latter, you need to enter the following statement into your .emacs file. (D EFun Linux-c-mode () "c Mode with adjusted defaults for use with the linux kernel." (set-style "k & r") (setq c-basic-offset8)) Will define a MX Linux-c-mode command. When you have a module, how you will - * - linux-c - * - input in the first two lines, this mode will automatically work. And, you Maybe you want to join (setq auto-mode-alist (cons "(" /usr/src/linux.*/.*//sr)) to In your .emacs file, in this case, when you edit the file under / usr / src / linux, it will automatically switch to Linux-c-mode. However, if you still can't let Emaces automatically process file formats Don't be nervous, you have the same thing: "indentation". GNU's indentation format is also dead, this is why you need to add a few command options. However, this is not too bad, because GNU indent format The creator also remembers the authority of K & R, (GNU is not sin, they just guide people in this thing), you have to do only the input option "-kr -i8" (indicating "K & R, indentation 8 characters). "Indentation" has a lot of features, especially when it suggests you re-format your code, you should look at your help. But remember: "indentation" is not a very poor style. Ling Dan.

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

New Post(0)