Best place to declare variables
I know hardly anything about XL's memory utilization, but I'll take
liberty with your use of "etc." in your question. It's personal
opinion only:
Variable memory is no longer a concern for me in any but the most
extreme circumstances. Nor is speed (of accessing variables, at
least). What I focus on is maintenance - it's cheaper for you to add
a GB of RAM than to have me spend an extra few hours trying to
decipher and debug your code.
I use three rules in commercial work:
1) All variables are local to their procedure. Variables must be
passed between procedures as arguments. There are no exceptions to
this rule.
2) In extreme circumstances, or if I can't think of any other way to
do it and the deadline for delivery is within 12 hours, Rule 1 may
be violated and a global variable used. In this case, procedures
sharing the global must be grouped in the same module and globals
must be private to that module. There must be no duplication of
global variable names between modules. Procedure variable names must
not override globals. There are *absolutely* no exceptions to this
rule.
3) At gunpoint, or when forced by customer specifications, Rule 2
may be violated, but *all* globals must be public and declared in a
separate module. Global variable names are perforce not duplicated.
These rules assure me that when I go to modify a client's code six
months or a year from now, I (or another developer) won't spend any
more time than necessary chasing variable names and scope.
In article ,
"Taras" wrote:
Where is the best place to declare variables, in terms of
memory utilization, etc. if the same variable names are
used in different modules, although their values do not
(always) need to be passed between modules.
ex:
Module A is used to summarize data on Sheet1 of a
Workbook into Sheet2
Module B is used to summarize data on Sheet3 of a
Workbook into Sheet4
There are no common functions/subs between Module A and
Module B (Module C has functions/subs called by both
Module A and B - but that could be a red herring)
In Module A and Module B I use the same variable names -
just for consistency.
Is it more efficient to declare the variables with DIM in
Module A & Module B or to declare them as Public
elsewhere?
|