3 ms·
You'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.
by Peaker 11y ago
You'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.
- bluecalm 11y ago>>You're forced to declare them before they have a sensible value to use. As I said I like it because I like seeing what will be operated on when I start reading a block. I don't need to initialize them just yet. >>You're exposing your code to unnecessary bugs where you user the variable prematurely. The compiler warns when you use uninitialized variable. Just don't initialize them to random things, leave them uninitialized. >>Variables tend to be declared further from their use, making it harder to find them and see their types. I mean, I can see how that could be a problem but I don't have blocks bigger than one screen so I can alway see them anyway. I like short functions/blocks. It's also way easier to visualize what is happening if I allocate memory in my brain for the variables so to speak. >>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). It might be technically wrong but the real problems are arrays as they are the only thing which could blow out the stack (especially in recursive functions) so yeah, I don't see it as a problem although I imagine it could be for some. >>You're discouraged from creating intermediate variables that aid readability. I am not discouraged. Again, maybe it sounds super strange to you but I am actually discouraged if I had to put them in the middle of the code logic as I find it ugly and I need to readjust my picture of what is going on once I encounter some new variable I didn't know is going to be there when glancing at the block. >>You're discouraged from declaring your variables const, losing extra safety. I think const is as good as useless in C when it comes to declaring variables. I don't think it gives any extra safety it's just more typing and additional pain. >>All in all, it's a terrible way to code. I don't know. I think you lost the perspective. One way or the other it won't be a big difference and a lot of good code is written in the old/traditional style. You may think it's worse but it is not terrible.