4 ms·
E.g. having to move variables to beginning of block (c89 style) is just miserable. I actually find this style clearer since all the variables are accounted for
by userbinator 11y ago
E.g. having to move variables to beginning of block (c89 style) is just miserable.
I actually find this style clearer since all the variables are accounted for at the beginning and it helps when looking for them (and associated comments, if any). You can always open a new block when you need a new set of variables. Do you happen to have started with a more dynamic language which didn't require variables to be declared? That was the case I've seen with most other programmers who preferred creating (often very many) variables only at the point of use.
- autoreleasepool 11y agoI believe creating variables at the point of use was retroactively inherited from C++ in C99. It can be useful feature when not abused.
- Peaker 11y agoWhat would be considered abuse here? Beginning of block variables are generally always worse than as late as possible variables.
- GFK_of_xmaspast 11y agoI learned c in the c89 days as my 3rd or 4th language (after modula-2 and pascal and maybe fortran) and now, in 2015, I can confirm that beginning-of-block variables are in fact just miserable.
- bluecalm 11y agoI share the view that it's more clear to have all the variables declared at the top. The only exception I am making in my code are loop variables but the point doesn't apply to those as you want the smallest scope possible so declaring them at the top of the block where the loop is located is just inferior. I find the style easier to read as you can instantly see what variables will be operated on, what arrays filled etc. It's also quite important for me to know how much stuff is allocated in given block so I don't blow out the stack and having all the things at the top makes it easier to think about. It's probably about being used to it but I found the code with mixed declarations and logic to be inelegant and more difficult to understand.
- pcwalton 11y ago> It's also quite important for me to know how much stuff is allocated in given block so I don't blow out the stack and having all the things at the top makes it easier to think about. With modern compiler backends you haven't been able to reliably reason about stack usage by looking at the number of local variables for quite some time. Even ignoring IR-level optimizations and register allocation, modern compilers will do coloring on the stack slots that remain, meaning that the number of stack locations you end up using is flow-sensitive (and thus moving to the top is actually technically obscuring things).
- Peaker 11y agoYou're forced to declare them before they have a sensible value to use. You're exposing your code to unnecessary bugs where you user the variable prematurely. Variables tend to be declared further from their use, making it harder to find them and see their types. The stack size argument is wrong, because variables aren't necessarily on the stack, and non variable allocations may be put on the stack (i.e it's neither necessary nor sufficient to predict stack use). You're discouraged from creating intermediate variables that aid readability. You're discouraged from declaring your variables const, losing extra safety. All in all, it's a terrible way to code.
- wnissen 11y agoAll excellent points, and I would add that it changes the semantics of the code when it comes to threading, which is will be a more and more major issue going forward. If the variable is declared outside the parallel section (such as a for loop), it's assumed that it is shared (and therefore synchronized), while if it's declared inside it is clearly private to each thread and no synchronization is required.
- bluecalm 11y agoWe are talking about top of the block. When you are creating a parallel section you have a new block/scope there so that argument doesn't apply. Both styles are the same when it comes to scope of the variables.
- aninteger 11y agoI just wanted to say that I agree with you, but that's because of age. These days I do more C#, but I still use this style even in that language although this is becoming less frequent as I replace code and use C#'s "var" declaration and assignment keyword.