Tuesday, April 7, 2015

Why "Dim"?


I've often wondered why the keyword "Dim" was used in VBA for declaring variables.  VBA is not my first programming language, but it was the first one I remembered using "Dim".  

What other languages did and do

I thought there might be some insight by looking at how different languages declare variables.

Some older languages put the variable declarations in a section before any of the procedural code, e.g. 
  • COBOL has its Data Division, 
  • PL1 begins data declarations with a "declare" statement.
  • Pascal uses the word "var".
In the above, the end of declarations is defined by the beginning of the procedural code: "Procedure Division" for COBOL and "begin" (with a mated "end") for the other two.  This enforced the need to declare the subject and object of the procedural statements before trying to take action on them.  This makes sense because it was easier to write compilers and assemblers that read from front to back than to have them hold each statement until the whole of the program was read in.

The actual declaration of each element hasn't changed much from PL1/Pascal
  • PL/1 and Pascal both used a <variable name> : <data type>;  construct
  • FORTRAN is the reverse of that, using <data type>  <variable name>
  • Java and its variations, as well as c and it's variations, follow the FORTRAN model
OK.  No luck looking for somewhere else Dim is used.


As I was working on some posts on arrays the light went on as I wrote about "dimensions" of arrays and the ReDim command.  Dim is relative to declaring the dimensions of a variable.  ReDim changes those dimensions.

Sure enough, a little reading confirmed my conclusion.  The use of "Dim" started with BASIC as the way to declare arrays of certain dimensions, and now is used to declare all variables.  After all, aren't these the equivalent except for the first you cannot ReDim it and there is no need to specify the index?
  • Dim i As Integer
  • Dim i(0) As Integer
  • Dim i(1 to 1) As Integer

No comments:

Post a Comment