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
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
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
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
Function and pass it as an argument, or scope it solely within the specific
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
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
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
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
Function. But I can definitely envisage places I'll use
static in the future.