1. 程式人生 > >[中英對照]Linux kernel coding style | Linux內核編碼風格

[中英對照]Linux kernel coding style | Linux內核編碼風格

ril views har func Language config req bing evel

Linux kernel coding style | Linux內核編碼風格

This is a short document describing the preferred coding style for the linux kernel. Coding style is very personal, and I won‘t force my views on anybody, but this is what goes for anything that I have to be able to maintain, and I‘d prefer it for most other things too. Please at least consider the points made here.
本文是一個簡短的文檔,描述了Linux內核的首選的編碼風格。編碼風格是非常個人化的東東,不強迫任何人接受我的觀點。但是,這是我必須能夠堅持的東西,也希望它對其他大多數事情也有幫助。請至少考慮一下本文談及的要點。



First off, I‘d suggest printing out a copy of the GNU coding standards, and NOT read it. Burn them, it‘s a great symbolic gesture.
首先,建議把GNU的編碼標準打印一份出來,然後不要閱讀,直接燒掉。這是一個了不起的象征姿態。

Anyway, here goes:
言歸正轉:

1) Indentation | 縮進

Tabs are 8 characters, and thus indentations are also 8 characters. There are heretic movements that try to make indentations 4 (or even 2!) characters deep, and that is akin to trying to define the value of PI to be 3.
XXXX

Rationale: The whole idea behind indentation is to clearly define where a block of control starts and ends. Especially when you‘ve been looking at your screen for 20 straight hours, you‘ll find it a lot easier to see how the indentation works if you have large indentations.
XXXX

Now, some people will claim that having 8-character indentations makes the code move too far to the right, and makes it hard to read on a 80-character terminal screen. The answer to that is that if you need more than 3 levels of indentation, you‘re screwed anyway, and should fix your program.
XXXX

In short, 8-char indents make things easier to read, and have the added benefit of warning you when you‘re nesting your functions too deep. Heed that warning.
XXXX

The preferred way to ease multiple indentation levels in a switch statement is to align the switch and its subordinate case labels in the same column instead of double-indenting the case labels. E.g.:
XXXX

switch (suffix) {
case G:
case g:
        mem <<= 30;
        break;
case M:
case m:
        mem <<= 20;
        break;
case K:
case k:
        mem <<= 10;
        /* fall through */
default:
        break;
}

Don’t put multiple statements on a single line unless you have something to hide:
XXXX

if (condition) do_this;
  do_something_everytime;

Don’t put multiple assignments on a single line either. Kernel coding style is super simple. Avoid tricky expressions.
XXXX

Outside of comments, documentation and except in Kconfig, spaces are never used for indentation, and the above example is deliberately broken.
XXXX

Get a decent editor and don’t leave whitespace at the end of lines.
XXXX

2) Breaking long lines and strings | 打破過長的代碼行和字符串

Coding style is all about readability and maintainability using commonly available tools.
XXXX

The limit on the length of lines is 80 columns and this is a strongly preferred limit.
XXXX

Statements longer than 80 columns will be broken into sensible chunks, unless exceeding 80 columns significantly increases readability and does not hide information. Descendants are always substantially shorter than the parent and are placed substantially to the right. The same applies to function headers with a long argument list. However, never break user-visible strings such as printk messages, because that breaks the ability to grep for them.
XXXXX

3) Placing Braces and Spaces | 括號和空格的位置

The other issue that always comes up in C styling is the placement of braces. Unlike the indent size, there are few technical reasons to choose one placement strategy over the other, but the preferred way, as shown to us by the prophets Kernighan and Ritchie, is to put the opening brace last on the line, and put the closing brace first, thusly:
XXXX

if (x is true) {
        we do y
}

This applies to all non-function statement blocks (if, switch, for, while, do). E.g.:
XXXX

switch (action) {
case KOBJ_ADD:
        return "add";
case KOBJ_REMOVE:
        return "remove";
case KOBJ_CHANGE:
        return "change";
default:
        return NULL;
}

However, there is one special case, namely functions: they have the opening brace at the beginning of the next line, thus:
XXXX

int function(int x)
{
        body of function
}

Heretic people all over the world have claimed that this inconsistency is ... well ... inconsistent, but all right-thinking people know that (a) K&R are right and (b) K&R are right. Besides, functions are special anyway (you can‘t nest them in C).
XXXX

Note that the closing brace is empty on a line of its own, except in the cases where it is followed by a continuation of the same statement, ie a while in a do-statement or an else in an if-statement, like this:
XXXX

do {
        body of do-loop
} while (condition);

and

if (x == y) {
        ..
} else if (x > y) {
        ...
} else {
        ....
}

Rationale: K&R.
XXXX

Also, note that this brace-placement also minimizes the number of empty (or almost empty) lines, without any loss of readability. Thus, as the supply of new-lines on your screen is not a renewable resource (think 25-line terminal screens here), you have more empty lines to put comments on.
XXXX

Do not unnecessarily use braces where a single statement will do.
XXXX

if (condition)
        action();

and

if (condition)
        do_this();
else
        do_that();

This does not apply if only one branch of a conditional statement is a single statement; in the latter case use braces in both branches:
XXXX

if (condition) {
        do_this();
        do_that();
} else {
        otherwise();
}

3.1) Spaces | 空格

Linux kernel style for use of spaces depends (mostly) on function-versus-keyword usage. Use a space after (most) keywords. The notable exceptions are sizeof, typeof, alignof, and __attribute__, which look somewhat like functions (and are usually used with parentheses in Linux, although they are not required in the language, as in: sizeof info after struct fileinfo info; is declared).
XXXX

。。。未完待續。。。。

Nothing can be accomplished without norms or standards.

// https://www.cnblogs.com/wang_yb/p/3532349.html

[中英對照]Linux kernel coding style | Linux內核編碼風格