My personal opinion is declare the variable at the beginning of the *scope* of where it is needed. If you declare it immediately before its first use, then you have to really hunt to find the definitions of variables that are randomly sprinkled through the code. If you dim them at the start of the block they are used in, at least there are fewer places to look.
Tim > -----Original Message----- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] Behalf Of > [EMAIL PROTECTED] > Sent: Friday, April 06, 2007 7:40 AM > To: [email protected] > Subject: Re: Place of Dim declares ? > > > On Apr 06, 2007, at 13:26 UTC, Eric Pousse wrote: > > > Is there a big advantage to put "Dim declares" into the code instead > > of before the code? > > I ask this because I have old codes and only if advantage is big, I > > will change place of "Dim declares". > > Which kind of advantages can there be? > > 1. It makes the code easier to refactor when a variable is declared > right where it is needed. This way, if you decide to Extract Method, > you can just grab that whole block of code (including its local > variable declarations) and cut it out to paste elsewhere. If the > variable declarations are at the top, you have a lot more work to do > finding out which ones you need. > > 2. When you declare a variable inside a code block (e.g. inside a For > loop or If block), then it goes away as soon as the block exits. This > has the compile-time advantage that you won't inadvertently use a > variable whose value no longer makes sense (e.g. by mistyping some > other similarly-named variable). And it has the run-time advantage > that the object's storage is deallocated right away, freeing up memory > that may be needed by code later in the method, and also releasing > resources like files that later code may want to use. > > I wouldn't bother to go through and change all your existing code if I > were you, but I would recommend getting in the habit of declaring > variables right where they're used for any future code you write. > > Best, > - Joe > > -- > Joe Strout -- [EMAIL PROTECTED] > Verified Express, LLC "Making the Internet a Better Place" > http://www.verex.com/ > > _______________________________________________ > Unsubscribe or switch delivery mode: > <http://www.realsoftware.com/support/listmanager/> > > Search the archives: > <http://support.realsoftware.com/listarchives/lists.html> > -- No virus found in this outgoing message. Checked by AVG Free Edition. Version: 7.5.446 / Virus Database: 268.18.26/748 - Release Date: 4/5/2007 3:33 PM _______________________________________________ Unsubscribe or switch delivery mode: <http://www.realsoftware.com/support/listmanager/> Search the archives: <http://support.realsoftware.com/listarchives/lists.html>
