Special

Introducing the “Welcome to Xojo” Bundle!

New to Xojo and looking for guidance? We've put together a terrific bundle to welcome you! Xojo Bundle

This bundle includes six back issues of the magazine -- all of year 21 in printed book and digital formats -- plus a one-year subscription (beginning with 22.1) so you'll be learning all about Xojo for the next year. It's the perfect way to get started programming with Xojo. And you save as much as $35 over the non-bundle price!

This offer is only available for a limited time as supplies are limited, so hurry today and order this special bundle before the offer goes away!

Article Preview


Buy Now

Issue 11.5 ('Weeds')
Instant purchase and download via GumRoad!

FEATURE

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
Resource File(s):

Download Icon 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 required that: 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.