Linux kernel programming style

xiaoxiao2021-03-06  75

Linux kernel programming style

(518 words on this post) (read: 1177 times)

This short article is recommended for Linux kernel programming style. The programming style is very personal, and I don't want to put my point of view to anyone, but in order to get maintenance, I have to make this view. Details : At the beginning, I should write the GNU programming style of the GNU programming and don't use it. Don't pay attention to them, it is just a symbolic expression. Ok, let's get started!

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: 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 brace of the beginning in one line, and will end the brace in the first place, as shown below: IF (x is True) {we do y} However, there is also a special situation: Name function: The starting brackets are the first line in the next row, as follows: int function (int x) {body of function} All non-orthodox people Non-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 not to pay attention to the end of the brackets, the line it occupies is empty, __ In addition to __ 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; and if (x == y) {..} Else IF X> Y) {...} else {....} Reason: K & R. In addition, it is noted that the placement method of such braces reduces the number of spaces, but there is no reductionance. 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 simple. Different from Module-2 and Pascal language, C programmers do not use naming methods such as thisvariableisatemporaryCounter. A C language The programmer will name it "TMP", which 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. Calling a function called "Foo" is very annoyed. Global variables (only when you have to use it), just like global functions, you need to have a descriptive naming method. If you have one The function is used to calculate the number of active users, you should be named - "Count_Active_Users ()" - Or additional similar form, you should not name "cntusr ()". There is a kind of hungarian naming method, it will The type of function is written in the variable name, which is a manifestation of the brain with a problem --- The compiler knows this type and then check it, and this will only confuse programmers. - know why Micro $ OFT Why produce so many "bug" procedures !!. The naming of local variables should be short and delicate. If you have a random integer loop counter, it may have "i", if there is no possibility that it can be misunderstood, Writing "loop_counter" is inefficient. Similarly, "" TMP "can be any temporary value of function variables. If you are afraid of confusing your local variable name, there is another question, that is, called Function-Growth- Hormone-ImpalanceSyndrome. Chapter 4: The function function should be short and charming, and it only makes one thing. It should only cover one to two screens (80 * 24 screen), and only make a thing, and put it Do it. (This is not a unix style, the translator's note). The maximum length and function of a function and the complexity of the indentation are inversely proportional. So, if you have written a simple but long-long function, And you have done a lot of things to different situations, and write a longer function. However, if you want to write a very complex function, and you have estimated that if the average person read this function, he I may not know what this function is saying. At 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 Error. Reconside this function, divide them into smaller functions. The human brain can usually remember 7 different things, more than this quantity will cause confusion. You know that you are smart, But you may still want to understand what you did 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. Usually, 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 really Complex, 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 the function, tell others what it is doing, and why you may do this.

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

New Post(0)