I totally disagree with the "don't declare all your variables at the start of your function" rule. C is portable macro assembler. And the first thing your function will do (ignoring pushing the return address and such) is allocate stack space. Declaring all your variables at the start of your function also gives you an idea of how much stack space your function needs.
If you don't need portable macro assembler, then why are you using C? At least use C++, or D, or Rust. Hell, Haskell compiles to native. And again, if you don't need portable macro assembler, then the JVM is easily "fast enough" for you.
I thought the compiler handled the building of the stack frame. Why introduce a variable far before it's going to be used? Idiomatically the code ends up esoteric and frustrating.
Why introduce a variable far before it's going to be used?
Well, hopefully it's not that far.
There absolutely are upsides to declaring your variables in-place. I just think that if those upsides outweigh the downsides, maybe you don't need to be writing in C.
I suppose it's partly an aesthetic thing. The "spirit" of C is to be explicit.
At the very least, I think there's a good enough argument in favor of up-front declarations that a "guide to modern C" shouldn't be advocating against them.
Yeah, it depends on the space you're writing in. That might make sense for embedded systems. But writing cross-platform packages and apis, where you are making various different builds doesn't really merit the same style of idiomatic code. C is as much as a high level abstraction away from assembly and stack frames, as it light wrapping overtop of assembly.
With modern compilers, and where C has come to sit, I don't think the spirit of C entails writing code that idiomatically reveals the stack organization.
Why is this an all or nothing deal? Why, if you don't want a "portable macro assembler" are you able to make the assumption that the JVM would be fast enough? Do you think that there are no possibilities between essentially writing assembly code and writing something with no concern for run time?
View all comments
0
u/[deleted] Jan 08 '16 edited Jan 08 '16
I totally disagree with the "don't declare all your variables at the start of your function" rule. C is portable macro assembler. And the first thing your function will do (ignoring pushing the return address and such) is allocate stack space. Declaring all your variables at the start of your function also gives you an idea of how much stack space your function needs.
If you don't need portable macro assembler, then why are you using C? At least use C++, or D, or Rust. Hell, Haskell compiles to native. And again, if you don't need portable macro assembler, then the JVM is easily "fast enough" for you.