简体   繁体   English

C99在开源项目中混合了声明和代码?

[英]C99 mixed declarations and code in open source projects?

Why is still C99 mixed declarations and code not used in open source C projects like the Linux kernel or GNOME ? 为什么C99混合声明和代码没有在Linux内核GNOME等开源C项目中使用

I really like mixed declarations and code since it makes the code more readable and prevents hard to see bugs by restricting the scope of the variables to the narrowest possible. 我非常喜欢混合声明和代码,因为它使代码更具可读性,并且通过将变量的范围限制在最窄的范围内来防止难以看到错误。 This is recommended by Google for C++ . 这是Google for C ++推荐的。

For example, Linux requires at least GCC 3.2 and GCC 3.1 has support for C99 mixed declarations and code 例如,Linux至少需要 GCC 3.2,而GCC 3.1 支持 C99混合声明和代码

You don't need mixed declaration and code to limit scope. 不需要混合声明和代码来限制范围。 You can do: 你可以做:

{
  int c;
  c = 1;
  {
    int d = c + 1;
  }
}

in C89. 在C89。 As for why these projects haven't used mixed declarations (assuming this is true), it's most likely a case of "If it ain't broke don't fix it." 至于为什么这些项目没有使用混合声明(假设这是真的),它很可能是“如果没有破坏就不修复它”的情况。

This is an old question but I'm going to suggest that inertia is the reason that most of these projects still use ANSI C declarations rules. 这是一个老问题,但我将建议惯性是大多数这些项目仍然使用ANSI C声明规则的原因。

However there are a number of other possibilities, ranging from valid to ridiculous: 然而,还有许多其他可能性,从有效到荒谬:

  • Portability. 可移植性。 Many open source projects work under the assumption that pedantic ANSI C is the most portable way to write software. 许多开源项目都假设迂腐ANSI C是编写软件最便携的方式。

  • Age. 年龄。 Many of these projects predate the C99 spec and the authors may prefer a consistent coding style. 其中许多项目早于C99规范,作者可能更喜欢一致的编码风格。

  • Ignorance. 无知。 The programmers submitting predate C99 and are unaware of the benefits of mixed declarations and code. 程序员提交早期C99并且不知道混合声明和代码的好处。 (Alternate interpretation: Developers are fully aware of the potential tradeoffs and decide that mixed declarations and statements are not worth the effort. I highly disagree, but it's rare that two programmers will agree on anything.) (替代解释:开发人员充分意识到潜在的权衡,并决定混合声明和声明是不值得的。我非常不同意,但很少有两个程序员会就任何事情达成一致。)

  • FUD. FUD。 Programmers view mixed declarations and code as a "C++ism" and dislike it for that reason. 程序员将混合声明和代码视为“C ++主义”并因此而不喜欢它。

There is little reason to rewrite the Linux kernel to make cosmetic changes that offer no performance gains. 没有理由重写Linux内核来进行不会提高性能的外观修改。

If the code base is working, so why change it for cosmetic reasons? 如果代码库正在运行,那么为什么要出于美观的原因进行更改呢?

I don't remember any interdictions against this in the style guide for kernel code. 我不记得在内核代码的样式指南中对此有任何阻止。 However, it does say that functions should be as small as possible, and only do one thing. 但是,它确实说功能应该尽可能小,只做一件事。 This would explain why a mixture of declarations and code is rare. 这可以解释为什么声明和代码的混合很少见。

In a small function, declaring variables at the start of scope acts as a sort of Introit , telling you something about what's coming soon after. 在一个小函数中,在范围的开始处声明变量就像一种Introit ,告诉你一些关于即将发生的事情。 In this case the movement of the variable declaration is so limited that it would likely either have no effect, or serve to hide some information about the functionality by pushing the barker into the crowd, so to speak. 在这种情况下,变量声明的移动是如此有限,以至于它可能没有任何效果,或者可以通过将巴克推入人群来隐藏有关功能的一些信息,可以这么说。 There is a reason that the arrival of a king was declared before he entered a room. 有一个原因是他进入房间之前宣布国王的到来。

OTOH, a function which must mix variables and code to be readable is probably too big. OTOH,一个必须混合变量和代码才能读取的函数可能太大了。 This is one of the signs (along with too-nested blocks, inline comments and other things) that some sections of a function need to be abstracted into separate functions (and declared static , so the optimizer can inline them). 这是一个标志(以及过于嵌套的块,内联注释和其他东西),函数的某些部分需要被抽象为单独的函数(并声明为static ,因此优化器可以内联它们)。

Another reason to keep declarations at the beginning of the functions: should you need to reorder the execution of statements in the code, you may move a variable out of its scope without realizing it, since the scope of a variable declared in the middle of code is not evident in the indentation (unless you use a block to show the scope). 在函数开头保留声明的另一个原因是:如果需要重新排序代码中语句的执行,可以将变量移出其作用域而不会意识到,因为在代码中间声明了变量的作用域在缩进中不明显(除非您使用块来显示范围)。 This is easily fixed, so it's just an annoyance, but new code often undergoes this kind of transformation, and annoyance can be cumulative. 这很容易修复,所以这只是一个烦恼,但新代码经常会经历这种转变,烦恼可能是累积的。

And another reason: you might be tempted to declare a variable to take the error return code from a function, like so: 另一个原因是:您可能想要声明一个变量来从函数中获取错误返回代码,如下所示:

void_func();
int ret = func_may_fail();
if (ret) { handle_fail(ret) }

Perfectly reasonable thing to do. 完全合理的事情。 But: 但:

void_func();
int ret = func_may_fail();
if (ret) { handle_fail(ret) }
....
int ret = another_func_may_fail();
if (ret) { handle_other_fail(ret); }

Ooops! 哎呀! ret is defined twice. ret定义了两次。 "So? Remove the second declaration." “那么?删除第二个声明。” you say. 你说。 But this makes the code asymmetric, and you end up with more refactoring limitations. 但这会使代码不对称,最终会产生更多的重构限制。

Of course, I mix declarations and code myself; 当然,我自己混合声明和代码; no reason to be dogmatic about it (or else your karma may run over your dogma :-). 没有理由对它采取教条(否则你的业力可能会超过你的教条:-)。 But you should know what the concomitant problems are. 但你应该知道伴随的问题是什么。

Maybe it's not needed, maybe the separation is good? 也许它不需要,也许分离是好的? I do it in C++, which has this feature as well. 我是用C ++做的,它也有这个功能。

There is no reason to change the code away like this, and C99 is still not widely supported by compilers. 没有理由像这样改变代码,C99仍然没有得到编译器的广泛支持。 It is mostly about portability. 它主要是关于可移植性。

There is no benefit. 没有任何好处。 Declaring all variables at the beginning of the function (pascal like) is much more clear, in C89 you can also declare variables at the beginning of each scope (inside loops example) which is both practical and concise. 声明函数开头的所有变量(像pascal一样)要清楚得多,在C89中你也可以在每个作用域的开头声明变量(在循环示例中),这既实用又简洁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM