It is a common feature of an application to perform lookups into a view to locate a document based upon one or more keys. The following are some tips for optimizing the LotusScript code when there may be a large number of lookups being performed (e.g. running against all selected documents in a view or all documents in a database).
1) Code the view name as a constant so that it is easier to maintain (and read) the code
2) Make the view static. For OOP define a private variable in the class for the view. For procedural code define a static function.
3) Fetch the view from that database once only by checking to see if the view is "Nothing"
4) To locate documents that have been created after the view was last refreshed it is usually more efficient to do a search first and then refresh the view (and research) only if the initial search failed than to refresh the view first every time.
Procedural Code:-
Static Function getByKeys(Source As variant) As NotesDocument
Dim iView as NotesView
If iView Is Nothing Then Set iView = DB.getView(MY_VIEW_NAME)
If iView Is Nothing Then Exit Function
Set getByKeys = iView.GetDocumentByKey(Source)
If getByKeys Is Nothing Then
Call iView.Refresh
Set getByKeys = iView.GetDocumentByKey(Source)
End If
End Function
OOP Code:
Class MyClass
Private iView As NotesView
Function getByKeys(Source As variant) As NotesDocument
If iView Is Nothing Then Set iView = DB.getView(MY_VIEW_NAME)
If iView Is Nothing Then Exit Function
Set getByKeys = iView.GetDocumentByKey(Source)
If getByKeys Is Nothing Then
Call iView.Refresh
Set getByKeys = iView.GetDocumentByKey(Source)
End If
End Function
End Class
1 Nathan T Freeman Permalink Nice call on point 4, although it doesn't translate to getAllDocumentsByKey, unfortunately.
Personally, I think the "set the view name as a constant" is
overrated. It makes sense, if you're going to refer to the view's
name more than once. But if you aren't, what's the point? It just
becomes another place where the IDE gets annoying because you can't
do something like hover over the constant to see it's assigned
value. No... you have to drag your ass all the way back up to the
top of the declarations section to see what you set it to, then you
have to scroll back down to your particular method in the class
definition.
YUCK.
Why would it be any less reasonably to define the name of the view
right there in the method? It's a static view definition anyway.
The whole point is that you are only going to instantiate the view
object once in the class anyway.
I see this all the time with NotesItems, too, and I never
understand it. A NotesDocument is basically a super-flexible
hashmap in Lotusscript. I can't see why people feel the need to
externalize every string reference. Maybe that will make sense in a
world with an updated LS editor -- but as long as we're still going
to have to manually scroll around the entire (Definitions) section
to find out the name of a view or form or field, then use the
literal. Otherwise, you're just encouraging code redundancy by
making a singleton out of the reference string, instead of the
*actual object.*
2 Peter Presnell Permalink Wouldn't it be great if we had an IDE (like Visual Studio) in which clicking on a constant in a line of code would cause its value to be displayed. Then the argument of programmer convenience would not get in the way of designing an application so there is a clear separation between the business logic layer and the presentation layer. It's personal preference, but I like having a constant block at the top of my LotusScript library that defines the presentation components (views, folders, forms etc) with which my business logic interacts. I have the option to easily bind my business logic to a new presentation without the need to search through all the code to find what/where design elements are referenced.