IBM®
Skip to main content
    Country/region select      Terms of use
 
 
   
     Home      Products      Services & solutions      Support & downloads      My account     

developerWorks  >  Lotus  >  Forums & community  >  Best Practice Makes Perfect

Best Practice Makes Perfect

A collaboration with Domino developers about how to do it and how to get it right in Domino

I've seen examples and tips instructing people to avoid using a complex expression after the "To" of a For statement, for best performance. For example, they say to use:

looplimit = doc.GetItemValue("ThatField")(0)
For i = 1 To looplimit

...

instead of this:

For i = 1 To doc.GetItemValue("ThatField")(0)

This is an anti-tip, then, because I'm writing to say that you needn't bother. LotusScript only evaluates the loop limit once, when you enter the loop the first time, as you can see for yourself if you run the following code:

Dim i As Long, strResult$
For i = 1 To overshoe(6.4)

          strResult = strResult & ", " & i

Next

Msgbox Mid$(strResult, 3)


Function overshoe(x As Double) As Long

  Static counter As Integer

  counter = counter + 1

  Msgbox "In overshoe, counter=" & counter

  overshoe = x * 2

End Function

So, storing the loop index in a temporary variable yourself is actually a tiny bit slower than letting LotusScript do it for you. If you want best performance, skip it. Only store the loop limit in a variable if it helps readability or simplifies your algorithm.

The other reason I thought I should mention this, is in case some oddball wanted to write a For statement whose limit was a moving target. E.g. you're discovering more work as you go along, and you want to increase the number of iterations. For this, use a different looping statement, such as While.

Which brings up my next point. This anti-tip is true only for the For statement in LotusScript. The For statement in any of our other supported programming languages, and other looping statements in LotusScript, does re-evaluate the termination condition in every iteration. Except for Forall, which totally has its own way of doing things.

Andre Guirard | 18 July 2007 08:00:00 PM ET | Plymouth, MN, USA | Comments (1)

Search this blog 

Disclaimer 

    About IBM Privacy Contact