When I first relocated from Australia to the USA one of the many things I had to learn was driving on the "other" side of the road. After a while it began to feel natural but then when I returned to Australia for a visit I had to get used to driving on a different side all over again. The experience is somewhat similar to learning to program SSJS and then returning to LotusScript. Suddenly I find myself making mistakes with my LotusScript code as a result of trying to use SSJS syntax.
What I have found is that there are a few ways to reduce the difference between the syntax of the two languages so that switching between the two does not cause as many issues. The following are a few tips to consider when writing LS/SSJS that can reduce the mistakes made when switching between the two languages. These can also assist when copying code from one to the other:-
1) Try/Catch
I have blogged about this before. In SSJS the error handler is always identified by the catch statement. In LS you can assign any label to to your errorhandler. So why not use On Error Goto Catch.
2) If Then Else
In SSJS the If condition is enclosed in parentheses. In LS this is optional - If (condition) Then statement. So if you get in the habit of doing this with LS code you will be less likely to forget when writing SSJS. The same can also apply to other LS conditional statements such as While.
3) Single Line If
If you have a single statement to execute for an if or a single statement for both the if and else it is possible to write this in LS on a single line and drop the End If statement e.g. If (condition) Then statement1 Else statement2. It is also possible in SSJS to write such statements on a single line.
4) Variable Names
SSJS is case sensitive for many things, INCLUDING variable names. Most JavaScript developers adopt the practice of using camel casing for variable name (eg. employeeNumber). With LS variable names are NOT case sensitive. So consider adopting camel casing for variable names in your LS code to get into the habit.
5) Field Names
In LS field names are somewhat case insensitive. When creating fields in LS the field name is usually created using the casing provided but it is always possible to access the field using any case. e.g. I can create a new field on a document with the field name BODY but still access it by doing NotesDocument.GetItemValue("Body"). When writing SSJS the case always seems to matter so it is important to ensure when writing LS code that you use a convention for the casing of field names so that when writing SSJS code you are not left trying to guess what the true field name is.
6) Comments
When writing lengthy comments in LS consider writing them in the format where the comment starts with '/* and ends with ' */. This provides a degree of consistency with the /* and */ syntax of SSJS. It also has the added benefit of being consistent with the requirements for using Mikkel Hesiterberg's excellent LotusScript.doc tool to document your LS code.
7) Immediate If
SSJS supports an immediate if statement in the form ((condition) ?truevalue : falsevalue). LS does not have a native form of this statement. To get into the habbit of coding using immediate ifs consider creating and immediate if function as part of a standard LS library.
Function IIf(Condition As Boolean,TrueResult As Variant,FalseResult As Variant) As Variant
Try:
On Error Goto Catch
If (Condition) Then
If Isobject(TrueResult) Then Set IIF = TrueResult Else IIF = TrueResult
Else
If Isobject(FalseResult) Then Set IIF = FalseResult Else IIF = FalseResult
End If
Exit Function
Catch:
Stop
ReportError Nothing
Exit Function
End Function
8) Debugger
And finally, for those of you with sadistic tendencies. When writing with LS stop using the LS debugger to debug your code. That way you can endure the same pain we all do when trying to debug our SSJS code :)
Comments (2)
One of the biggest differences between Lotusscript and Javascript is the fact that JavaScript uses lazy evaluation for if conditions and Lotusscript does not.
Great resource for easing context switching between LS and SSJS...