3 ms·
I 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 tel
by mmcgaha 4y ago
I 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)