Saturday, March 13, 2010

Domino Development Best Practice

Over the next few weeks, I will share best practice as a developer when I am developing Notes/Domino applications.

I will start the series by sharing some basic development styles which I adhere to:

- Always declare variables in a hierarchy and keep Domino object declaration different from other variables, i.e.,

Dim session as NotesSession
Dim db as NotesDatabase
Dim vwCustomer as NotesView
Dim customerDoc as NotesDocument

Dim strFirstName as string, strLastName as string
Dim varNames as Variant

As you can see - session comes first, then db, then view etc etc. Also notice that the string and other variables are kept separate from Notes objects.


- Make sure to tab and comment your code properly for your own benefit - trust me, when you come back to your own code in a year's time, without proper comments, you will have a hard time understanding why you did something in the first place.


- Try to generalize sub-routines as much as possible. I try to follow a simple rule, when a single method or function gets too big to handle, start breaking it up into smaller pieces - divide and conquer.


- Always try to simplify code in order to hide complexity and make your life simpler, i.e.:

BEFORE:

Sub AttachFile (ndDoc As NotesDocument, strFileName As String, strRichTextFieldName As String)
Dim object

Dim strFilename As String
strFilename = Dir$(filename)
If (strFilename <> "") Then
Dim rtitem As NotesRichTextItem
If ndDoc.HasItem(strRichTextFieldName) Then
Set rtitem = ndDoc.GetFirstItem(strRichTextFieldName)
Else
Set rtitem = New NotesRichTextItem( ndDoc, strRichTextFieldName )
End If
Set object = rtitem.EmbedObject ( EMBED_ATTACHMENT, "", strFileName)
End If
End Sub


AFTER:
Sub AttachFile (ndDoc As NotesDocument, strFileName As String, strRichTextFieldName As String)
Dim object
If FileExists(strFileName) Then
Dim rtitem As NotesRichTextItem
If ndDoc.HasItem(strRichTextFieldName) Then
Set rtitem = ndDoc.GetFirstItem(strRichTextFieldName)
Else
Set rtitem = New NotesRichTextItem( ndDoc, strRichTextFieldName )
End If
Set object = rtitem.EmbedObject ( EMBED_ATTACHMENT, "", strFileName)
End If
End Sub


Function FileExists(Byval filename As String) As Boolean
Dim strFilename As String
On Error Resume Next
strFilename = Dir$(filename)
FileExists = (strFilename <> "")
End Function


As you can see you will have to do some thinking in order to figure out what this line of code does in the before example:
strFilename = Dir$(filename)
If (strFilename <> "") Then
but it's obvious in the after example.