Linux OS kernel coding style

Source: Internet
Author: User

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).

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.