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


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)


1) LOL
Nathan T. Freeman | 7/18/2007 10:09:48 PM

It's funny that you say this. I see this kind of tip all the time, and people who follow it then go on to write stuff like...

loopLimit = doc.loopLimit(0)

For i = 0 to loopLimit

Set db = session.getDatabase(myServer, path)

blah blah blah

Next i

Then, of course, they wonder why the loop is so slow, when they followed the tip.

2) a hard habit to break
Charles Robinson | 7/19/2007 9:01:37 AM

I was surprised when you said this in a previous comment because it's not what I expected. I never tested it because I never questioned that my assumption was correct. Here is my thought process: LotusScript is based on VB (conceptually), VB evaluates the condition on every iteration, therefore LotusScript must behave like VB. :-p

In my own defense, I write code in LotusScript and VB on a daily basis. As I said above, VB does re-evaluate the termination condition on every iteration so it makes my code more portable and me slightly less crazy to handle loop variables with a lowest common denominator approach.

3) didn’t know that :-/
Feri | 7/19/2007 9:27:40 AM

didn't know that, but according to @2, I think it s more readable to use extra variable.

you, or few others understand WHY it is unnecessary in For loop, but for those without this understanding: it is better to set rule "NEVER do it in cycle", than "don't do it in cycles, but don't bother with For..."

@1) LOL, but unfortunately, I know many "programmers" doing the same thing

4) readability and debugging
Joseph LeMay | 7/19/2007 10:28:26 AM

I'll frequently write code like this for readability and to be able to debug it more easily. Just put a stop after the looplimit= line and you can see what looplimit is.

5) For do while until loop lasts ...
Fabian Robok | 7/24/2007 8:36:55 AM

Nathan, I see what you mean: Those people don't even use doc.GetItemValue("loopLimit")(0) ...

Only kidding.

To be honest, I wasn't aware of this behaviour myself. But given the very nature of a for loop, I tend to think that VB is broken, not LS, if it re-evaluates the to condition in every iteration. For loops are intended for situations where the number of iterations can be determined beforehand.

Far more annoying for me is the fact, that LS allows each and every thinkable combination of loopin keywords (see subject) to write either pre- or post-test loops. Do Until ... Loop and Do ... Loop While don't add flexibility, but only potential confusion.

However, in many situations I will keep to using a temporary variable for the reasons mentioned by Charles and Joseph.

6) So in @For it would be quicker?
Martijn de Jong | 8/1/2007 12:05:44 PM

"The For statement in any of our other supported languages..."

If I understand you correctly you're saying that

x := @Elements(fieldname);


Would be faster than



7) re: So in @For it would be quicker?
Andre Guirard | 8/2/2007 9:33:33 AM


 Add a Comment
Comment:  (No HTML - Links will be converted if prefixed http://)
Remember Me?     Cancel

Search this blog 


    About IBM Privacy Contact