I would have to disagree with Steve McConnell. I am sure it makes no real
difference in efficiency. Declaring variables and setting constants within
executable code, to me, just makes following the code a little more
difficult. If you use a good naming convention that includes data type and
scope, there is no confusion on how a variable is used. One other thing I do
when declaring variables is put a comment in that describes the use of the
variable. So, if there is any question when reading my code, you only have
to go to the top of the procedure or module to see what the variable is for.
I don't use global varialbes.
It is arguable that it is a matter of style. I can't disagree with that. My
intention when writing code is to make it as easy for the person behind me to
understand what I was doing. That includes making code as simple as possible.
I was told by my mentor in 1977 that a beginner's code and a seasoned
expert's code will be very simular. The difference being that the expert's
code would be tighter and more efficient. It is the programmer in the middle
of his learning experience that is hard to follow. He is learning all the
cute tricks and using them because he can, not because the problem requires
it. I have found that to be very true. When you see code that has complex
nested formulas and immediate Ifs, then you know that person has about 2 to 3
years experience. Someday he will have to modify that code and realize what
a nightmare he created.
So, to sum it up, it is all about style and personal preference to a point.
There are somethings, however, almost everyone can agree are bad practices,
like the infamous GoTo. So, the idea is to watch, learn, and share. We all
get better that way.