1

They used to say it was about memory management. However, having a look at things I find that failure to dimension a variable will default it to a variant data type which allocates 22 bytes of memory.

Let's consider just a single megabyte of memory. 1,000,000 bytes. This means I would require 45,454 variables just to chew through a single MB of memory. Most modern systems have several GB of memory. So the question is should I really care about carefully setting the right dim for all of my variables? Or is it a waste of time by modern standards?

Johnson Jason
  • 671
  • 1
  • 13
  • 29

2 Answers2

7

Memory usage is only pleasant a side effect. The real reason I'd recommend using Option Explicit is that it allows the compiler to protect you from coding mistakes that compile and run, but don't do what you intend.

Example 1: It keeps typos from turning into unexpected disasters.

Dim variableNamedFoobar As String
variableNamedFoobar = "Something"

If varableNamedFoobar <> "Something" Then
    Debug.Print "Did you catch that? The compiler would..."
End If

If you aren't explicitly declaring variables, the compiler happily gives you a 22 byte empty string to compare against.

Example 2: It also protects against leaking scope. Consider a huge project with multiple levels of variable scope. Hope you remember what your globals are:

Private x As Integer

Private Sub First()
    'I remembered here that x is a global.
    x = 2
    Debug.Print x
    Second
    Debug.Print x
End Sub

Private Sub Second()
    'I forgot here.
    For x = 1 To 5
    Next x
End Sub

Adding Dim x as Integer for use as the loop counter ensures that it is scoped to Second().

Example 3: It allows the compiler to detect type mismatches.

Set foo = New Collection
foo.Add "Bar"
Debug.Print TypeName(foo)

'... 100 lines of code later...

foo = 6
Debug.Print TypeName(foo)

If you had Dim foo As Collection in there somewhere, you get a compile time error letting you know that you already had a foo and you shouldn't be assigning 6 to it.

There are lots of other examples of where you can shoot yourself in the foot (or higher) with implicit variable declarations. While these examples are easy to spot in the isolation of the code blocks above, any one of them can cause subtle and extremely difficult to debug errors in a large code base. Do yourself (and everyone that might need to maintain your code later) a favor and type the extra line of code.

Comintern
  • 21,855
  • 5
  • 33
  • 80
  • Type safety is definitely the main reason in my experience. Declare the right type for the task at hand and life is a whole lot easier. I wouldn't know the last time I had to care about memory usage in a managed environment. – DeanOC Jun 12 '15 at 03:22
  • @DeanOC - Yep. I'll take anything I can get, although without `Option Strict` you can still get burned occasionally. – Comintern Jun 12 '15 at 03:25
  • Whilst I completely agree and I dim everything as it makes it a lot easier to understand the code if I know what type variables are and it structures the flow of data within the code avoiding potential issues shown in other posts here, I wouldn't ever dim something as integer in VBA, always use a long instead. Here is an explanation: http://stackoverflow.com/questions/26409117/why-use-integer-instead-of-long – Dan Donoghue Jun 12 '15 at 04:24
  • @DanDonoghue - I'm well aware of that, but I find it makes sample code easier to comprehend because it is more recognizable as a numeric type. Also, the types really don't matter in this context as much as the *lack* of types. – Comintern Jun 12 '15 at 04:31
  • Very true, just wanted to throw it out there for the OP, if they are questioning why we even bother then it's probably a good link to give them an insight into some of the other dimension based traps :). – Dan Donoghue Jun 12 '15 at 04:33
1

The most helpful reason I've found is it's just nice to be able to see what are variables and what are controls right away (that's more in Access than Excel), and also to just know what type of variable it's really supposed to be. This is mostly useful for other people looking through your code, as I've inherited some projects where the variables were not defined at all, and projects where they were very well defined, and the latter definitely makes things much easier. In regards to memory though, I wouldn't say it doesn't matter but the difference is negligible so it's personal choice.

hypetech
  • 166
  • 1
  • 12