Explicit and implicit declarations

The VBA default data type is the Variant data type. As Table 5.1 shows, a variable of the variant data type consumes 16 bytes plus 1 for each character, and is used to store a data item when the data type is not known. Using a Variant data type can be costly in memory and program execution time. Lomax (see Further reading, page 218) estimates that expressions using only Variant data execute about 30% slower than the same expressions using the correct intrinsic data types. In VBA, the programmer is not required to declare a variable type explicitly, but it is advisable to do so for these reasons:

♦ It gives clarity to program design; the programmer will better understand the purpose of each variable when they are explicitly declared.

♦ It prevents inadvertent creation of new variables by misspelling the name of an existing one; this will make debugging easier since any spelling errors will be identified before program compilation.

♦ It makes code easier to understand by grouping all variable names at the beginning of a program.

♦ Code is executed faster if the correct data type is assigned to each variable. This is because they will consume less memory, and therefore be retrieved more quickly.

♦ Less memory is required if the correct data type is assigned to each variable.

0 0

Post a comment