Monday, May 31, 2010

Mixed declarations and code sucks

C99 allows arbitrary interleaving of code and declarations inside one block. The previous versions of the language demanded that declarations are all at the beginning of the block, followed by the code which uses those declarations. Now, why on Earth did this change? When dealing with some eagerly C99-tified code such as unfortunately in HelenOS, I have to face several inconveniences:
  • the function looks ugly, not quite like the good old C, disturbed and it makes me feel like vomiting
  • if the function is a bit longer, it is difficult to find out where a variable is declared
  • likewise, it is much easier to declare a variable of the same name as is the variable which exists a couple of lines below
  • in general, it is difficult to keep track of all the variable declarations scattered all over the function's body
  • bigger maintainance overhead caused by moving the declaration up if it needs to be used by some code added before its declaration (think of a simple int rc variable)
Who said that a variable must be declared close to its first use, especially closer than is the beginning of the entire block? I am inevitably in deep disagreement with that person and want my good old-style declarations back!