Write Your Own Plug-In Functions

Here are some guidelines to help you write your own plug-in VBScript functions.

2 Min Read
ITPro Today logo in a gray background | ITPro Today

One of the most valuable skills you can develop in VBScript is the ability to write functions you can plug in to a script. The secret to making plug-in functions easy to use is to minimize coupling between functions and the scripts in which they run. Coupling is a measure of how closely a function is tied to a script; minimal coupling lets you treat a function as a "black box" that performs the same task regardless of the script from which it's called. The following simple practices, which I've used to write the functions in this article, can guide you in writing your own reusable functions:

  • In the function declaration line, make sure that you pass as arguments any variables you need from outside the function, and pass as few variables as possible. By doing so, when you look at the first line of the Function statement, you immediately know everything the function needs from the script.

  • Use the ByVal keyword with all your arguments, which Listing 1 shows, for example. Using the ByVal keyword ensures that if you modify an argument value in the function, you modify a local copy of the data that the value specifies rather than the actual data that the calling script uses. Although the functions in this article don't modify argument values, you might sometimes want your functions to modify such values. Using ByVal to explicitly pass variables from outside a function ensures that your plug-in function doesn't affect the calling script in an unintended way.

  • If you use a constant or variable inside your function, make sure you also explicitly declare it inside the function. Explicitly declaring a constant or variable prevents accidental sharing of variables and their values between the function and calling script and makes your script run slightly faster.

Following these straightforward rules makes reusing code in new scripts quick and painless.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like