码迷,mamicode.com
首页 > 系统相关 > 详细

[中英对照]Linux kernel coding style | Linux内核编码风格

时间:2018-01-31 18:27:44      阅读:344      评论:0      收藏:0      [点我收藏+]

标签:一个   test   tin   ant   hang   括号   grep   use   ranch   

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内核编码风格

标签:一个   test   tin   ant   hang   括号   grep   use   ranch   

原文地址:https://www.cnblogs.com/idorax/p/8393100.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!