The solution is to declare the number of books as a Variant. The only commitment that is made is about the meaning of the parameter-that it contains a number of books-and no restriction is placed on that number. As much flexibility as possible is maintained, and the cost of those account code upgrades will diminish.
Function ReadBooks(ByVal numBooks As Variant) ' Code in here to read books End Function
Suppose we want to upgrade the function so that we can pass An unknown number of books as a valid input. The best way of doing this is to pass a Variant of subtype Null. Null is specifically set aside for the purpose of indicating not known.
If the parameter had not been a Variant, you would have had some choices:
- Add another parameter to indicate that the number is unknown. A drawback of this approach is that a modification would be required everywhere this function is called. That way lies the million-dollar upgrade. If the parameter were Optional, you would get away with this approach, but only the first time.
- Allow a special value to indicate unknown-perhaps -1 or maybe 32768. We might create a constant of this value so that the code reads a little better-Const bkNotKnown = -1-and use that. This approach leads to bugs. Sooner or later, you or another programmer will forget that -1 is reserved and use it as an ordinary value of number of books, however unlikely that may seem at the time you choose the value of the constant.
If the parameters are Variants, you avoid these unsatisfactory choices when modifying the functions. In the same way, parameters and return types of class methods, as well as properties, should all be declared as Variants instead of first-class data types.
HUNGARIAN NOTATIONThe portion of Hungarian notation that refers to data type has little relevance when programming with Variants. Indeed, as variables of different data types can be freely assigned and interchanged, the notation has little relevance in Visual Basic at all.
I still use variable prefixes, but only to assist in the categorization of variables at a semantic level. So, for example, "nCount" would be a number that is used as a counter of something. The n in this instance stands for a general numeric, not an Integer.