c - Linux内核编码风格

标签 c linux coding-style linux-kernel

我是内核编程的新手,所以我想了解哪种编码风格更容易被接受。例如,在错误处理的情况下,以下哪个更好?

这个:

/* some stuff */
if(error) {
    /* error handling */
    return -(errorcode);
}
/* normal actions */

或者这个:

/* some stuff */
if(!error) {
    /* normal actions */
} else {
    /* error handling */
    return -(errorcode);
}

我在哪里可以找到关于内核编码标准的文档?

最佳答案

Linux 内核有一个编码风格指南:

https://www.kernel.org/doc/Documentation/process/coding-style.rst

Nicer Formatted Version

关于你的例子,我个人更喜欢第一种风格。使用第二种样式,您将很快违反此 Linux 内核样式规则(内核样式有 8 个字符的缩进):

if you need more than 3 levels of indentation, you're screwed anyway, and should fix your program.

从上到下(相对于水平方向)编写代码有时被称为duffing。我可以向您推荐有关该主题的优秀读物:

Reading Code From Top to Bottom

关于c - Linux内核编码风格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12772253/

相关文章:

c - 二叉树;按频率递增的顺序打印; C语言

linux - 如何将文本添加到同一行?

linux - 根据不同的列值将多列合并为行

php - 在 php 变量中使用花括号是个好习惯

javascript - 我应该在删除之前检查 'disabled' 属性是否存在吗?

连接两个字符以创建转义序列

c - 将字符串分配给 char 数组时类型不兼容

php - 如何在 Linux Fedora 上的 Apache 中启用 PHP?

c# - 使用 'Common Type System' 作为编码标准

java - 编程中的动态链接器是什么?