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 NotesSessionDim db as NotesDatabaseDim vwCustomer as NotesViewDim customerDoc as NotesDocumentDim strFirstName as string, strLastName as stringDim 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 objectDim strFilename As StringstrFilename = Dir$(filename)If (strFilename <> "") ThenDim rtitem As NotesRichTextItemIf ndDoc.HasItem(strRichTextFieldName) ThenSet rtitem = ndDoc.GetFirstItem(strRichTextFieldName)ElseSet rtitem = New NotesRichTextItem( ndDoc, strRichTextFieldName )End IfSet object = rtitem.EmbedObject ( EMBED_ATTACHMENT, "", strFileName)End IfEnd Sub
AFTER:
Sub AttachFile (ndDoc As NotesDocument, strFileName As String, strRichTextFieldName As String)Dim objectIf FileExists(strFileName) ThenDim rtitem As NotesRichTextItemIf ndDoc.HasItem(strRichTextFieldName) ThenSet rtitem = ndDoc.GetFirstItem(strRichTextFieldName)ElseSet rtitem = New NotesRichTextItem( ndDoc, strRichTextFieldName )End IfSet object = rtitem.EmbedObject ( EMBED_ATTACHMENT, "", strFileName)End IfEnd SubFunction FileExists(Byval filename As String) As BooleanDim strFilename As StringOn Error Resume NextstrFilename = 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.