This short article describes the coding style preferred by the Linux kernel. Coding style is a very personal thing, I will not impose my opinion on anyone. However, the Linux kernel code is after all I have to be able to maintain, so I would rather its coding style is my favorite. Please at least consider this point.
First, I propose to print a copy of the GNU Code Standard and not read it. Burn it down, it's just a symbolic gesture. Then, see:
1th Chapter: Indentation
Tabs (tab) is a size of 8 characters, so indents should also be 8 characters in size. Some rebel advocates try to turn the indentation into 4 (or even 2!). The length of the character, it is as if trying to define PI (case, pi) as 3 is the same.
Rationale: The idea behind indentation is to clearly define where a control block starts and ends. Especially after you keep staring at the 20-hour screen, if you have a large indent. You will be more likely to find the benefits of indentation.
Now, some people say that 8-character indentation causes the code to be too right and looks uncomfortable on a 80-character wide terminal screen. The answer to this question is: if you have more than 3 levels of indentation, you are a bit confused and should revise your program.
In short, a 8-character indent makes the program easier to read, and when you hide the feature too deep, the multi-level indentation gives you a visual warning. Be mindful of this warning message.
2nd Chapter: Placing curly Braces
Another idea in the C program is the placement of curly braces. Unlike indented dimensions, there is no technical justification for how to place curly braces. However, the preferred approach is to display the opening parenthesis at the end of the line and the closing parenthesis at the beginning of the line, as shown by the Prophet Brain Kernighan and Dennis Ritchie. Is
if (x is true) {
we do y
} there is,
However, another case in which the function: functions should place the left and right brackets at the beginning of the line. That is:
int function (int x) {Body of
function
}
The rebellious people are everywhere. They say this will lead to ... Well, inconsistencies (case, the use of curly braces for functions are not uniform with other situations). But everyone who thinks correctly knows: (1) K&r is Right, (2) K&r is right. Also, the function is not the same as anything else (you can't hide it in C language).