@dragon Not to, like make this a paragraph, but I think I prefer the opposite.
The main benefit of defining your variables later is that they're closer to the context in which they're used/relevant.
It's a bit of a balance between "is my code interesting/hard to understand because of the logic it's driving, or is my code interesting because of the computer things it's accomplishing?"
If it's the latter, I could see having the memory all allocated at the same time as being real helpful, but if it's the former, it can help with local readability. I prefer to work on things that are logically interesting, but computationally boring, so my preferences might make some sense here.
To each is their own, tho. :)
@dragon I think decreasing variable re-use is why most style guides I've used prefer local definitions over defs at the top of the function.
I think most compilers optimize lines like
for(int x = 0; x<3; x++)
{
int i = x +i
print(i)
}
such that i isn't actually re-allocated each time, but like, yeah, most places I've seen would have you define int i right before the loop, but not at the top of the function, and ideally, if i isn't used elsewhere, to wrap the whole thing in something for scope management. That loop does look a bit wasteful.
The little overview of what's to come is neat, though, I totally agree there.
@starless oh yeah absolutely, like I'm sure it's nice in a lot of cases!!