Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

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

For example, Linux requires at least GCC 3.2 and GCC 3.1 has support for C99 mixed declarations and code

like image 803
Eduardo Avatar asked Jun 11 '10 22:06

Eduardo


4 Answers

You don't need mixed declaration and code to limit scope. You can do:

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

in 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."

like image 143
Matthew Flaschen Avatar answered Nov 11 '22 23:11

Matthew Flaschen


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.

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.

  • Age. Many of these projects predate the C99 spec and the authors may prefer a consistent coding style.

  • Ignorance. The programmers submitting predate C99 and are unaware of the benefits of mixed declarations and code. (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. Programmers view mixed declarations and code as a "C++ism" and dislike it for that reason.

like image 24
Dan Olson Avatar answered Nov 11 '22 23:11

Dan Olson


There is little reason to rewrite the Linux kernel to make cosmetic changes that offer no performance gains.

If the code base is working, so why change it for cosmetic reasons?

like image 40
Alan Avatar answered Nov 11 '22 22:11

Alan


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.

like image 27
arthurprs Avatar answered Nov 12 '22 00:11

arthurprs