| 416-621-9348 cgreaves@chrisgreaves.com | |
|---|---|
| Home Site About Services Products |
About Self-Testing Functions
I produce self-testing functions.
Here is an example:
Public Function strSplitBefore(strIn As String, lngMaxResult As Long) As String ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ''' Function: strSplitBefore ''' ''' Comments: Uses the leading character as a delimiter. ''' Returns a string split off, including the delimiter at the leading, but not the trailing, end ''' ''' Arguments: STRING Source of data. ''' LONG Maximum length of result string. ''' ''' Returns: STRING ''' ''' Date Developer Action ''' -------------------------------------------------------------------------- ''' 2008/11/27 Chris Greaves Created ''' If Len(strIn) > 0 Then Dim strDelimiter As String strDelimiter = Left(strIn, 1) Dim lngRes As Long If Len(strIn) >= lngMaxResult Then lngRes = lngMaxResult While (Mid(strIn, lngRes, 1) <> strDelimiter) lngRes = lngRes - 1 Wend strSplitBefore = Left(strIn, lngRes - 1) strIn = strDelimiter & Right(strIn, Len(strIn) - lngRes) Else strSplitBefore = strIn strIn = "" End If Else End If 'Sub TESTstrSplitBefore() ' Dim strIn As String ' strIn = " here is a string delimited by spaces" ' Debug.Assert " here is a" = strSplitBefore(strIn, 15) ' Debug.Assert " string" = strSplitBefore(strIn, 15) ' Debug.Assert " delimited by" = strSplitBefore(strIn, 15) ' Debug.Assert " spaces" = strSplitBefore(strIn, 15) ' Debug.Assert "" = strSplitBefore(strIn, 15) 'End Sub End Function
There are three parts to this function:
(1) The header and comments block
(2) The VBA code body
(3) A commented macro
The commented macro can be dragged outside the function, de-commented and run.
The macro should run flawlessly, not halting on a Debug.Assert statement.
Indeed, I wrote the function, built the macro, ran the macro, observed that it ran flawlessly, commented the macro, and dragged it into the procedure.
As long as I don’t change the function without running the macro, the macro should always assert that the function lives up to a set of standards.
What standards? The standards written down in the body of the macro.
I write the body of the macro BEFORE I write the body of the function.
The macro represents the acceptance test for the function.
I state “the function is acceptable if the macro runs flawlessly”.
When it is time to upgrade the function, I drag the macro out and run it. This sets the base mark, and assures me that, unchanged, the function still does what was intended.
I code a new test in the macro, then code a change in the procedure, then re-run the macro and assure myself that it still runs flawlessly.
The macro now has a raised standard of performance. I comment it and drag it back into the function.
What’s in it for you?
You don’t have access to the source code of my functions, but you can run them from the released library.
You can paste the macros into a VBA module and run them, or Debug.Print the results to see what value is returned by the function.
You can then introduce your own data to the macro, and confirm that the function will behave for you as you expect it to.
If you are developing functions, hopefully in a library, I encourage you to use this technique.
You’ll be grateful twelve months from now when you are scratching your head wondering what the function code means. Single-stepping through the code, driven by a standard test macro, is a great way to re-familiarize yourself with your own work!
Loading
Toronto and Mississauga, Friday, March 19, 2010 1:50 PM
Copyright © 1996-2010 Chris Greaves. All Rights Reserved.