Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> C99 allows variable declarations anywhere

I actually like declaring my variables at the top. To me, this seems more readable than searching for declaration statements throughout the code.

Why should I not do this?



When you do that, reading your code is like reading Dune for the first time. I have to keep jumping back and forth to the reference to figure out what's going on.


At least I can clearly see which variables are already set, so that I don't redeclare them.


I would like to read more code that was lied out as a piece of literature: first and foremost - introduce the characters of the play!


Because it wastes memory and decreases performance, that's why.

If there's a section of code in your function that may or may not be executed, depending on a conditional, then allocating the memory for variables used within that code block will be a waste if you end up not executing it. Now, you could argue that that block should be turned into a separate function, but now you're doing another function call (unless it gets optimized out) which wastes performance, and you're making the code more cluttered if the code block is relatively small and not really worth turning into a separate function.


Are not all the local variables allocated on the stack in the function prologue anyway? no matter where in the function they are defined. A typical function starts with subtracting the stackpointer to make space for the local variables if i'm not mistaken.


> allocating the memory for variables

Allocation of N variables on the stack is O(1) since it just moves the stack pointer by the total size of all variables.


You wrongly assume here that your C code will have to run on a stack machine.


Do you have a counterexample?


locality.

It makes the code more readable.


C90 allows declarations (almost) anywhere; you just have to use an explicit binding construct, which in C consists of a statement block. That is, instead of this:

  {
    int x = 3;

    do_something();

    int y = 4;

  }
you write this:

  {
    int x = 3;

    do_something();

    {
      int y = 4;
    }
  }
As a Lisp programmer, I prefer clear binding constructs which put the variables in one place. The (define ...) in Scheme that you can put anywhere makes me cringe; you have to walk the code to expand that into proper lambdas before you can analyze it. Other Lisp programmers don't agree with me. The CLISP implementation of Common Lisp, whose core internals are written in C and which historically predates C90, let alone C99, uses a "declarations anywhere" style on top of ANSI C. This is achieved with a text-to-text preprocessor called "varbrace" which basically adds the above braces in all the right places.

You get better locality with explicit binding blocks, because their scope can end sooner:

   {
     foo *ptr = whatever();
     /* this is a little capsule of code */
     /* scope of ptr ends */
   }

   {
     foo *ptr = whatever_else(); /* different ptr */
     /* this is another little capsule of code */
   }
These tighter scopes are much more tidy than the somewhat scatter-brained "from here to the end of the function" approach.

I can look at that capsule and know that that ptr is meaningful just within those seven lines of code. After the closing brace and before the opening one, any occurrence of the name "ptr" is irrelevant; any such occurrence refers to something else.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: