To read the article online, visit http://www.4GuysFromRolla.com/webtech/071801-1.2.shtml

Optional Arguments in VBScript, Part 2

By Francis Chau


  • Read Part 1

  • 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 NULL-checking approach!

    Using the NULL-Checking Approach
    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 so:

    Sub MySub(arg1, arg2, arg3)
       ...
    End Sub
    

    Then, to determine whether or not a particular value was passed in, use the IsNull 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:

    If isNull(arg2) Then
    	'User did not pass in a value for arg2
    Else 
    	'The user passed in a value for arg2
    End If
    

    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 arg1 and arg3, we could call the subroutine like so:

    MySub NULL, Value2, NULL

    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 this and this ASPMessageboard.com posts.

    Here are some of the Pros and Cons of the Array-based approach:

      Pros:
    1. 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.
    2. 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.
    3. The number arguments at run-time may be unknown. It is possible to create a robust argument processing routine for customized purposes.

      Cons:
    1. Maintenance by other developers becomes more cumbersome without documentation
    2. Your code clarity will suffer as the number of arguments and their values are hidden in an array
    3. Debugging becomes a nightmare as you try to track down what values are being passed in and from where
    4. 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 and 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).

    Happy Programming!

  • By Francis Chau


    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.


  • Article Information
    Article Title: Optional Arguments in VBScript, Part 2
    Article Author: Francis Chau
    Published Date: Wednesday, July 18, 2001
    Article URL: http://www.4GuysFromRolla.com/webtech/071801-1.2.shtml


    Copyright 2017 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers