When to Dim
Should you reserve variables early or late?
Issue: 11.5 (September/October 2013)
Author: Marc Zeedar
Author Bio: Marc taught himself programming in high school when he bought his first computer but had no money for software. He's had fun learning ever since.
Article Description: No description available.
Article Length (in bytes): 8,397
Starting Page Number: 45
Article Number: 11508
project11508.zip Updated: 2013-09-03 12:57:55
Related Link(s): None
Excerpt of article text...
Recently I had a discussion with a subscriber over the practice of when to dimension variables. The traditional practice has been to reserve all your variables at the beginning of a routine, but the new trend is to do so later, just before you use them. Which is better?
As with all subjective things, there is no correct answer. Or rather, all answers are correct. Neither method is a guarantee of success or failure. I personally use a combination of both methods and feel that's a good balance.
Arguments for DIMs at the Top
The idea behind reserving your variables at the top of a routine comes from ancient languages that
requiredthat: the compilers couldn't handle DIM statements later in the code and needed everything declared up front. Later, compilers were more flexible, but the trend stuck.
But there are also practical benefits for doing it that way, chiefly that you get to see all the variables at the beginning, allowing you, the human, to get a better grasp of the code that follows.
Think about a simple routine, such as the classic "bubble sort" algorithm:
...End of Excerpt. Please purchase the magazine to read the full article.