LotusScript and The “Static” Keyword

Home » LotusScript and The “Static” Keyword

Moving from language to language, I'm constantly re-evaluating how coding improvements can be made to make the applications I work with more pleasurable and the code I write smarter. Unless a language has a narrow focus or limited power, there are likely to be aspects you've not dug into. That's particularly the case if it's a language you first learned a long time ago, as was the case for me with LotusScript. How many people for example use Lists? Another example is classes. Classes in LotusScript are not as easy to work with in Java, but I found myself using them more after coming from Java.

The recent example I came across was some code that used the static keyword when declaring a variable. Every developer has used dim, the LotusScript equalavent of var in SSJS / JavaScript. In classes developers may have used public and private, and anyone who's gone through XPages to Java will be comfortable with what those two mean. But static may be unfamiliar. Thankfully the documentation is quite helpful.

"Static indicates that the variable's value is saved between calls to the procedure where the variable is declared."

The following bit gets a bit more complicated unless you've got a heavy pro code background:

"You can use the Static keyword in procedure scope, but not in module or class scope."

What this means is that you can use it in normal Subs and Functions in Agents or Script Libraries, but not in classes. But what it does mean is that you can use it to lazy load variables in Subs and Functions that are called in loops and, between calls, the variables will retain their value. This is like a lazy loaded getter in Java, where typically I will code if (myVar == null) setMyVar();return myVar;. In the same way, the LotusScript will need to check if it's nothing and, if so, initialise it. So something like:

If (myView is Nothing) Then Set myView = myDb.getView("myView")

There is probably a religious argument here in some cases for whether you initialise the variable outside the Sub or Function and pass it as an argument, or scope it solely within the specific Sub or Function with the static keyword. Passing the variable in makes a lot of sense if you may want to pass different views depending on the implementation. It also makes sense if you want to use the view outside of the scope of the Sub or Function. Similarly you may wish to abort the code early in the processing if the view doesn't exist in the database and your error handling doesn't allow you to break out of the loop otherwise.

But there are benefits. If you're Sub or Function is being called from lots of places, it avoids duplication of code. It means you can scope much more tightly and avoid either declaring variables more globally or passing lots of variables into a Sub or Function. Lots of globally declared variables can result in name collisions when you then need to call multiple script libraries. Also, the fewer global variables in use, the less risk of typos in functional code that results in using the wrong or uninitialised variable, because you've inadvertently typed a global variable name instead of the more tightly scoped variable name you intended.

If it's a single view or database, I might still initialise the variable in my main code block and pass it into the Sub or Function. But I can definitely envisage places I'll use static in the future.

2 thoughts on “LotusScript and The “Static” Keyword”

  1. Theo Heselmans

    I’ve used it sporadically in the past. But often return to declaring global variables, being initiated in the main code. Often it’s a lot of unnecessary work to pass variable around as arguments.
    Everything depends on the circumstances of course.

  2. Same here, I used it a few times in LotusScript. The static idea comes from the C language: a variable with local scope and global lifetime. Example: a random number generator funtion. In LS, the usual pitfalls are there: a static view or doc, and then the database is closed, effectively destroying the contents of the variable.
    In most cases, instead of using a static variable, I much prefer to use a class with a private variable. The advantages are that, while the scope is local, the lifetime is controlled by me and secondly, a class can hide much more than just a variable. Just my £0.02…

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top