View Single Post
Old July 5, 2019, 15:00   #86
Derakon
Prophet
 
Derakon's Avatar
 
Join Date: Dec 2009
Posts: 8,869
Derakon is on a distinguished road
Quote:
Originally Posted by PowerWyrm View Post
Not really. I'm used to declare my variables at the beginning of functions even when using C++ or Java and I find the code much easier to read that way. But of course I'm an old developer... In my time, RAD environments didn't even exist, you had to code everything. Heck, at my time we were coding using Pascal...
There's a lot of reasons why on-the-fly declarations is the standard these days. The biggest one IMO is that it allows you to scope a variable to just the block of code it is used in. For example, in many languages this:
Code:
for (int i = 0; i < 10; ++i) {
  ...
}
means that the `i` variable does not exist outside of that for loop. This is a great way to avoid accidentally using your loop iterator in the wrong context. And if you do need it outside of that context, then it's trivial to explicitly declare it, which is then a signal to the reader that "hey, this loop variable will probably be needed later."
Derakon is offline   Reply With Quote