5 ms·
Being very much a kid (at heart at least), I'll wager the question: what's the case against declaring variables as you need them? Also, at the top of which scop
by Pepe1vo 4y ago
Being very much a kid (at heart at least), I'll wager the question: what's the case against declaring variables as you need them? Also, at the top of which scope are you supposed to declare variables, function scope, class scope, ... somewhere else?
- saghm 4y ago> Being very much a kid (at heart at least), I'll wager the question: what's the case against declaring variables as you need them? There's not really one, it's just a joke about how some languages used to work. > Also, at the top of which scope are you supposed to declare variables, function scope, class scope, ... somewhere else? In C, you used to have to declare all local variables at the top of the function rather than throughout the body.
- jfoutz 4y agoWhat? you want your compiler to backtrack and recalculate all the stack offsets? Just say how many bytes a call needs up front! Kids today, acting like computer time is free.
- xenomachina 4y ago> In C, you used to have to declare all local variables at the top of the function rather than throughout the body. This is true, but support for block-scoped (as opposed to function-scoped) variable declarations was added to C a long time ago. Every version of ANSI C supports them, and this stack exchange answer, https://softwareengineering.stackexchange.com/a/300274/19196 https://softwareengineering.stackexchange.com/a/300274/19196, suggests that support was added to K&R C since at least 1978.
- saghm 4y agoHmm, interesting! I might be remembering wrong, but I remember within the last decade being told by a coworker that it was required for proper Windows support, but maybe that was just an old version of Visual Studio from XP or something that no longer needs to be supported.
- spc476 4y agoAnd C99 allows you to declare variables anywhere in the function (much like C++).
- saghm 4y agoI remember that the reason my coworker had said that they couldn't use local variables declared anywhere in functions for their Windows code was due to not supporting C99, but maybe that was just due to some external constraint rather than a Windows-specific one (like some customer needing them to support pre-C99 code or something).
- xenomachina 4y agoEven C89 supports block-level variable declarations. I think many C programmers are unaware of this, though, and mistakenly think that declarations are function-level only.
- mmcgaha 4y agoI won't make the case against but I will make the case for declaring them at the top. Think of a recipe letting you know that you need eggs and flour before telling you how to mix it. It lets you get all of your ingredients on the counter before you go reading the instructions. Having the variables up front lets you prepare your mind for the state before reasoning about the code.
- wtetzner 4y agoThat sounds good in theory, but in practice whenever I run into code that declares all of the variables up front, I can't tell what those variables are for until I read the body of the function anyway. And often those variables end up being reused, which makes it even more confusing.
- dinosaurdynasty 4y agoI'll make a case against. Declaring all your variables up-front means that _all_ variables can be altered from _every_ place in the function, making it a lot harder to analyze/find data dependencies/etc. Minimizing the scope of variables decreases the amount of scanning/grokking you have to do to find out how they are related. (Making them constant too also helps a lot as well... you know it won't change thereafter)
- SilasX 4y agoNot an answer but interesting nonetheless: In the nand2tetris course (where you build a computer from logic gates/flip-flops to the OS/compiler), the high level language you implement has this as a requirement, mainly to make the compiler easier to write.
- vbezhenar 4y agoYou declare variables at the top of the function, so compiler can calculate space reserved for those variables and emit `sub esp, N`.