in reply to Where to put my variables...

I was brought up in programming on a strict diet of Pascal, PL/SQL, and COBOL, so I know all about predeclaring my variables. This was still my standard in the C, Perl, and Java code as well. After reading Refactoring, I decided that it was time to make the switch. The problem with having your variables someplace other than where your using them is that it make code maintanence nasty. Say you have a somewhat long function of about 60-80 lines of code (this shouldn't happen after refactoring, but I digress). If all your variables are declared at the top of your procedure, you generally have to scroll up and down to figure out what their doing. If the scope of your variables is narrow, then you should see everyplace its used on one screen. That makes the lives of the programmers that come after you much easier.

Replies are listed 'Best First'.
Re: Re: Where to put my variables...
by Maclir (Curate) on Jun 01, 2002 at 03:15 UTC
    However,the problem of not being able to figure out what a variable is doing is handled by using sensible, descriptive variable names.
      That works much better if cow-orkers never misuse your carefully named variables.

      In the real world, being able to verify that this variable has a scope of 5 lines and isn't misused there does wonders for debugging.