patternMinor
DRYing up an initialization pattern
Viewed 0 times
initializationpatterndrying
Problem
I maintain a VBA process that prints a large number of PDFs. It was awful when I inherited it and I've been making improvements over time, but I'm a bit stuck on this one. There are at least 4 functions with this exact same code used to check to see if the program has been initialized properly. They could all be called individually, or together depending on circumstances.
I do not want to initialize again if I don't have to. Hence the check in every subroutine, but I just can't figure out how to do it any differently. Thoughts? Is there a design pattern to deal with this kind of thing that I don't know of?
Please try to focus on the pattern and not the supporting intitializer. I know there are a few issues there and intend to deal with them soon. I included it in case it was relevant.
Main code:
Initializer:
```
Private Sub initialize()
rootDir = folderPicker() & "\"
mCycle = getCycleInput
isInit = True
End Sub
Private Function folderPicker() As String
'***
' returns directory path to be printed to
' does not allow multiple selections,
' so returning the first item in selected
' items is sufficient.
'
' returns empty string On Error or if the
' user cancels
'****
On Error GoTo ErrHandler
Dim folderChosen As Integer
Dim fd As Variant
Set fd = Application.F
I do not want to initialize again if I don't have to. Hence the check in every subroutine, but I just can't figure out how to do it any differently. Thoughts? Is there a design pattern to deal with this kind of thing that I don't know of?
Please try to focus on the pattern and not the supporting intitializer. I know there are a few issues there and intend to deal with them soon. I included it in case it was relevant.
Main code:
Private rootDir As String
Private isInit As Boolean
Private mCycle As String
Public Function printPDF_VoidedNewItems()
If Not isInit Then
initialize
End If
If rootDir <> "" And mCycle <> "" Then
If setPrinterToPDF() Then
' logic specific to printing this pdf
End If
End If
End Function
Public Function printPDF_ComsNotFed()
If Not isInit Then
initialize
End If
If rootDir <> "" And mCycle <> "" Then
If setPrinterToPDF() Then
' logic specific to printing this pdf
End If
End If
End FunctionInitializer:
```
Private Sub initialize()
rootDir = folderPicker() & "\"
mCycle = getCycleInput
isInit = True
End Sub
Private Function folderPicker() As String
'***
' returns directory path to be printed to
' does not allow multiple selections,
' so returning the first item in selected
' items is sufficient.
'
' returns empty string On Error or if the
' user cancels
'****
On Error GoTo ErrHandler
Dim folderChosen As Integer
Dim fd As Variant
Set fd = Application.F
Solution
Down with the underscore
I find the naming of
Avoid underscores in procedure & function (/method) names, the language has a meaning for identifiers with an underscore.
Initializing once
If you scratch that code and implement it in a class instead, and run initialisation in the...
I find the naming of
printPdf_xxxx functions confusing, because if that code is/were in a class module they should be private, and the class would say Implements printPdf, where printPdf would be a class with VoidedNewItems and ComsNotFed members... or it could be that there's a private field called printPdf that's declared WithEvents and the functions are event handlers made public.Avoid underscores in procedure & function (/method) names, the language has a meaning for identifiers with an underscore.
Initializing once
If you scratch that code and implement it in a class instead, and run initialisation in the...
Class_Initialize method, which can be thought of as a parameterless constructor that runs when you create the object. Not too crazy about spawning a UI prompt in that handler though, but then the methods wouldn't have to worry about whether or not Initialize has executed.Context
StackExchange Code Review Q#55896, answer score: 6
Revisions (0)
No revisions yet.