Optional Arguments in VBScript, Part 2By Francis Chau
In Part 1 we looked at how to create a VBScript subroutine
that accepts optional arguments via the array-based technique. In this part we'll examine how
to accomplish the same thing using the
This technique for optional arguments involves assigning the non-used arguments the value of
NULL, then check for the
NULL value in the argument. If you have
a subroutine, say,
MySub, that you wish to allow the user to pass in any of up
to three arguments as optional, you should create the subroutine to expect three arguments, like
Then, to determine whether or not a particular value was passed in, use the
function. For example, if we wanted to determine whether or not the developer had passed in a
value for the second argument (
arg2), we could do:
To call a function or subroutine that employs this
NULL-checking mechanism, simply
call the function like normal, passing in
NULL for any optional arguments that
you wish to omit. For example, to call
MySub and not supply the arguments
arg3, we could call the subroutine like so:
As you can see, this technique of
NULL-checking will be easier for future developers who may
need to maintain your code (this includes yourself!) and provides a more exhaustive and concise
handling of your arguments.
There's plenty of debate about the usage of optional arguments and how it contributes to
indigestible coding practices. In fact, such a staunch argument can be found at
Here are some of the Pros and Cons of the Array-based approach:
- You can create wrap-around functions with optional arguments to extend existing functions. For
example, You have a large application that needs to be retrofitted but you'd rather not modify
thousands of lines of code to extend the application.
- You have a VBScript class where you specify default values and would like to reduce the
amount of code written to override the values when they are passed in.
- The number arguments at run-time may be unknown. It is possible to create a robust argument processing routine for customized purposes.
- Maintenance by other developers becomes more cumbersome without documentation
- Your code clarity will suffer as the number of arguments and their values are hidden in an
- Debugging becomes a nightmare as you try to track down what values are being passed in and
- Code reusablity is questionable as array-based optional argument subs and functions are customized for its implementation.
In summary, while VBScript does not support optional arguments there's two approaches -- Array-based
NULL-checking. Which one you go with will depend largely upon the trade-offs
you're willing to take and whether good coding practices are a high priority. If you're really
serious about using optional arguments, your best bet would be to convert to JScript which
provides more facilities for handling arguments (see this
FAQ for more information).
About the Author:
Francis Chau is a web developer on Bank of America's eCommerce Technology Website Development and Support Team. He focuses on ASP and SQL Server Development, in addition to Web usability and interaction behavior design.