Logo 
Search:

MS Office Answers

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds
  Question Asked By: Aysel Kaya   on Dec 10 In MS Office Category.

  
Question Answered By: Fjodor Bonkob   on Dec 10

Another way of looking at it - don't DIM until the very last minute.

I'm not aware of any gain from putting DIMs all at the top of the code, and
there are at least two drawbacks IMHO:

1) is that you end up with unused variables, over time.

2) is that you have no "circumstantial" protection against using a variable
before it is populated with a value.

When I'm writing C++ (Java, JavaScript, etc), I'll normally declare the
variable in the same statement that I initialise it ...

int daysPerLeapYear = 366;

I use the colon construct in VBA to achieve almost the same result ...

Dim DaysPerLeapYear As Integer: DaysPerLeapYear = 366

In both cases, it is not possible for me to hit the declared variable and
have it not also initialised. The compiler throws out any reference to the
variable that isn't in the correct scope.

In cases when I must declare the variable somewhere else because of scope, I
declare it as close as possible to where it is set, to minimise the
likelihood of trying to use a variable that hasn't been populated with the
correct information.

(Sorry about the stupid example, but it's the syntax I'm talking about, and
my mind is blank at the moment.)

Share: 

 

This Question has 11 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on Code Cleaning Was: Re: Can't open excel file Or get search suggestion and latest updates.


Tagged: