API
@-Formulas
JavaScript
LotusScript
Reg Exp
Web Design
Notes Client
XPages
 
Defining LotusScript Variants
I keep seeing examples of LotusScript variables being defined in a way that they end up being Variant data types, so I thought I'd bring this up again as a reminder. Take a look at the following variable definition:

Dim x, y As Integer

This actually defines the variable x as a Variant and the variable y as an Integer. Those that have been coding in LotusScript for a while should already know this, but I see it so often.

What's the issue?

Well, storage is one concern - a Variant is the largest data type you can define (16 bytes). A variable defined as an Integer takes up 2 bytes. But with today's computers, that's really nit-picking. Saving a few bytes here and there most of the time won't help too much unless your code is large enough that you're running into memory problems.

My two biggest concerns are readability and performance. The code definition above gives the appearance that you're wanting the variable x to be an Integer. Because it isn't, the actual result doesn't match the appearance. If you want to save lines and define both x and y the same, you can write this code:

Dim x As Integer, y As Integer

The performance is my biggest concern, however. When that code is running, the 16 bytes of storage is allocated for the variable. If you place an integer value into that variable, Notes will internally do a conversion of the data type so it's "kind of" an integer. But that conversion takes time and can happen multiple times based on how often the variable is used. Just defining the variables as the intended data type can save processing time. Consider the following code:

For x = 1 To 300
   For y = 1 To 300
      For z = 1 To 300
         ' Do nothing
      Next
   Next
Next

If the variables x, y, and z are all defined as Variant variables, the code takes on average a minute and a half to complete. Leaving x and y as Variant variables and defining z (the inner most loop, so the one that's used the most often) as an Integer speeds the code up to around 7 seconds. Defining all the variables as Integer variables cuts down the time to around 6 and a half seconds.

Since z is the most used variable in the code above, just defining it incorrectly can cause a performance problem. If x and y are both Integer variables and z as a Variant, the code now will take over 84 seconds. The exact same code can be dropped from 84 seconds to under 7 seconds!

Please, please, define the proper data types for your variables!