Article title: Linux kernel programming style. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
This short article provides suggestions on the Linux kernel programming style. the programming style is very personalized, and I don't want to impose my point of view on anyone, but in order to become a maintenance, I have to raise this point of view.
The details are as follows:
At the very beginning, I should write the GNU programming style standard instead of ignoring it. ignore them. it is just a symbolic expression.
Okay. let's get started!
Chapter 1: indent format
Tab is 8 characters, so indentation is also 8 characters. There are many weird styles, they define the indent format as 4 characters (set to 2 characters !) It is as unacceptable as trying to define PI as 3.
The reason is: the indent size is to clearly define the start and end of a block. especially after you have been in front of the computer for more than 20 hours, you will find that a large indent format makes your understanding of the program easier.
At present, some people say that the code is very close to the right by using 8 characters of indentation, and it is quite uncomfortable to see the program on the terminal screen with 80 characters in width. the answer is yes, but when your program has more than three indentation, you should modify your program.
In short, the 8-character indentation makes the program readable, and an additional benefit is that it can warn you when you change the program into too many layers of nesting. at this time, you should modify your program.
Chapter 2: location of a large symbol
Another issue in the C programming style is the handling of braces. different from the indentation size, there is almost no reason to choose one style instead of another, but there is a recommended style, it is brought by the classic book of Kernighan and Ritchie. it puts the starting braces at the end of a row and the ending braces at the first of a row, as shown below:
If (x is true ){
We do y
}
However, there is also a special case: name function: The starting brackets are placed first in the next row,
As follows:
Int function (int x)
{
Body of function
}
Non-Orthodox people will not be difficult to maintain such inconsistency, but all normal-thinking people understand: (1) K & R is ___ to ___, (2) if K & R is incorrect, see Article 1. (:-))...... in addition, functions are special, and do not have to be consistent.
It should be noted that the ending parenthesis occupies an empty line, __except for _ and it follows the continuation 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 {
....
}
Reason: K & R.
In addition, it is noted that the placement of braces reduces the number of empty rows, but does not reduce readability. therefore, when the screen size is limited, you can have more blank lines to write comments.
Chapter 3: naming system
C is a concise language, so the name should be concise. unlike MODULE-2 and PASCAL, C programmers do not use naming methods such as ThisVariableIsATemporaryCounter. a C-language programmer will name it "tmp", which is easy to write and not so hard to understand.
However, when names of mixed types have to appear, descriptive names are necessary for global variables. it is annoying to call a function named "foo" globally.
A global variable is used only when you must use it. like a global function, a descriptive naming method is required. if you have a function to calculate the number of active users, you should name it -- "count_active_users ()" -- or another similar form. you should not name it "cntusr ()".
There is a kind of naming method called Hungarian, which writes the type encoding of the function into the variable name, which is a mental manifestation-the compiler knows this type and will check it, this will only confuse programmers. -- I know why Micro $ oft has produced so many "bug" programs !!.
The naming of local variables should be short and concise. if you have a random integer cyclic counter, it may have "I". if there is no possibility of misunderstanding, writing "loop_counter" is inefficient. similarly, "tmp" can be a function variable of any temporary value.
If you are afraid to confuse the name of your local variable, another problem is the function-growth-hormone-imbalance syndrome.
Chapter 4: functions
The function should be short and charming, and it only does one thing. it should cover only one to two screens (80*24 screen), and do only one thing, and do it well. (Isn't that UNIX style ).
The maximum length of a function is inversely proportional to the complexity and indentation of the function. therefore, if you have written simple but long functions, and you have done a lot of small things for different situations, it doesn't matter to write a longer function.
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 talking about. at this time, use a helpful function with a descriptive name.
Another consideration is the number of local variables. they should not exceed 5-10; otherwise, you may encounter errors. reconsider this function and divide them into smaller functions. the human brain can easily remember seven different things. exceeding this number will lead to confusion. you know that you are smart, but you may still want to understand what you did two weeks ago.
Chapter 2: Notes
Comments are a good thing, but too many comments are dangerous. don't try to explain how your code is annotated: you should write the code better, instead of spending a lot of time explaining the bad code.
Generally, your comments describe what your code does, rather than how it is done. also, try to avoid inserting comments into a function body: If this function is really complicated, you need to add some comments in it. you should go back to Chapter 4 to see it. you can write some short comments to indicate or warn those parts that you think are particularly intelligent (or extremely ugly), but you must avoid too many. instead, write the comments in front of the function and tell others what it is doing and why it may be.
Chapter 6: You are already deeply immersed.
Don't worry. you may have been told that "GUN emacs" will automatically process the source code format of C for you, and you have seen that it does. However, by default, its role is still unsatisfactory (in fact, they are more ugly than just typing something-a infinite number of monkeys typing into GNU emacs wocould never make a good program)
Therefore, you can either not use GUN emacs or use saner valules. to use the latter, you need to input the following statement into your. emacs file.
(Defun linux-c-mode ()
"C mode with adjusted defaults for use with the Linux kernel ."
(Interactive)
(C-mode)
(C-set-style "K & R ")
(Setq c-basic-offset 8 ))
This defines an M-x Linux-c-mode command. when you hack a module, the mode automatically takes effect when you change-*-linux-c-*-input to the first two lines. in addition, you may want to add the following
To your. emacs file. in this case, when you edit the file in/usr/src/linux, it will automatically switch to linux-c-mode.
However, if you still cannot allow emaces to automatically process the file format, don't be nervous. you have another thing: "indent ".
The GNU indent format is also rigid, which is why you need to add several command lines. however, this is not too bad, because the creators of the GNU indent format also remember the authority of K & R (GNU has no sin, and they just mistakenly guide people in this case ), all you need to do is enter the option "-kr-i8" ("K & R, indent 8 characters ).
"Indent" has many functions, especially when it suggests that you reformat your code, you should look at the help. but remember: "indent" is not the Wan Ling Dan of a poorly styled program.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.