Flylib.com

Books Software

 
 
 

Scripting Languages as Loosely Typed


Scripting Languages as Loosely Typed

Programming languages come in two flavors when it comes to how data types are handled.

A strongly typed language forces the programmer to declare, in advance, the data type of every variable so that the compiler will know exactly what kind of value to expect in that variable. If the programmer declares a variable with a numeric data type, the compiler expects that variable to hold a number, and produces an error if the programmer violates that assumption by trying to, for instance, store a date in that variable.

In a loosely typed language, the programmer does not have to declare in advance the data type of a variable. Usually, in fact, a loosely typed language does not even provide a way to declare the data type. Scripting languages like VBScript are very often loosely typed. They use a generic, 'universal' super data type that can hold any type of data.

The opposite of a scripting language is a compiled language . Code written in a compiled language is processed in advance by a compiler, which produces an optimized binary executable file-like the .EXE files you are no doubt accustomed to seeing on your computer. A scripting language is not compiled in advance, but rather 'on the fly.' The process for a compiled language is:

  1. Write the code in plain text

  2. Compile the code to produce an executable file

  3. Run the compiled executable file

  4. The program runs

Instead of a compiler, most scripting languages, including VBScript, have the concept of a runtime engine , which 'interprets' the code 'at runtime' instead of compiling it in advance. The process for a scripting language goes a bit differently:

  1. Write the code in plain text

  2. Execute the script file

  3. The scripting runtime engine compiles the code 'on the fly'

  4. The program runs

The delayed compilation that comes with a scripting language goes hand-in-hand with the loose typing of the language. This is an oversimplification, but since code is compiled on the fly, the compiler can examine the data being placed into a variable and what kinds of operations are being performed on the variable to arrive at an educated guess for what the data type of that variable should be.

The concepts of loose typing, a universal data type, and educated guessing about data types at runtime lead to some interesting scenarios and behaviors when you execute the VBScript code you have written. Throughout this chapter, we will closely examine these details to ensure that you will not fall into any programming traps related to VBScript's unique way of working with variables and data types.



Why Data Types Are Important

Let's consider for a moment the Visual Basic programmer's perspective on data types. It may seem odd to suddenly change the subject to a different programming language, but VBScript and Visual Basic are actually very closely related and often used together. You can think of Visual Basic as the parent of VBScript. VBScript syntax is derived directly from Visual Basic, and in many cases Visual Basic and VBScript syntax are identical.

However, the reason for bringing up Visual Basic is that, in a discussion of data types, the concepts are simpler to explain and easier to grasp when presented in the context of Visual Basic. What's more, these concepts translate directly when we return in the next section to the discussion of VBScript's Variant data type and its peculiarities .

You will remember from the previous section that Visual Basic is a strongly typed language, which means that a Visual Basic programmer must declare a specific data type for each variable used in his or her program. For example, here is a variable declaration in Visual Basic. What this line of code means is that the programmer is telling the computer to reserve space in memory for a variable called OrderTotal and that the data type that will be stored in that variable is the Currency data type. (The Currency type is used to store numeric values that represent an amount of money.)



Dim OrderTotal As Currency


By declaring the OrderTotal variable with the Currency data type, the programmer is signaling his or her intention to store only numeric amounts of money in this variable. He does not plan to try to store a date or a customer's name in the OrderTotal variable. And if he did, the Visual Basic compiler would produce an error. Take a look at the next two lines of code, which assign two different values to the OrderTotal variable.



OrderTotal = 695.95




OrderTotal = "Bill's Hardware Store"


The first line of code works fine, because a numeric value is being stored in the variable. However, the second line of code will produce an error because the type of data going into the variable does not match the declared data type. A strongly typed language also makes a line of code like the following produce an error.



OrderTotal = 695.95 + "Bill's Hardware Store"


This strongly typed syntax produces several technical benefits in the compilation and performance of a Visual Basic application. However, since this book is about VBScript, we're not going to get into that. What we do want to talk about though are benefits that translate directly to VBScript-namely, the predictability and clarity that strong typing brings to programming.

A programmer always wants to accomplish at least two things: fulfilling the requirements for the program (in other words, building a program that will do what it is supposed to do) and producing a program that is free of bugs and mistakes. Code that is clear, readable, understandable, and predictable will always be easier for human beings to read, understand, and change. Code that is easy to read, understand, and change is always more likely to fulfill the requirements and more likely to be free of bugs than code that is not.

{% if main.adsdop %}{% include 'adsenceinline.tpl' %}{% endif %}

A Visual Basic programmer must declare a variable for a specific purpose, give the variable a specific name, and declare the intention to store only a specific type of data in that variable. If all of the elements of a program are this neatly segmented, given good specific names like OrderTotal , and used in a very consistent manner, the program is likely to do what it's supposed to do without a lot of bugs.

Things are a little different, though, for the VBScript programmer. VBScript does not have any syntax for declaring a variable with the Currency data type, or any other specific data type. All VBScript variables have the same data type, Variant . The following line of code shows what the same variable declaration would look like in VBScript.



Dim OrderTotal


The syntax is almost the same, but VBScript does not support the As keyword for declaring a data type. This means that the VBScript programmer is free to put any kind of data in this variable he or she wants. The following two lines of VBScript code are both equally valid in VBScript. Unlike in Visual Basic, the second line of code will not produce an error.



OrderTotal = 695.95




OrderTotal = "Bill's Hardware Store"


Believe it or not, the second line of code that seems so ridiculous does not produce an error in VBScript. As mentioned a moment ago, in Visual Basic this line definitely produces an error when OrderTotal is declared with the Currency data type. However, in VBScript this line of code results in the value "695.95Bill's Hardware Store" stored in the OrderTotal variable.



OrderTotal = 695.95 + "Bill's Hardware Store"


The reason for these seemingly strange VBScript behaviors will become clear as we dig deeper into the Variant data type and its subtypes . Before we get there, however, there is a lesson to take away from this comparison of Visual Basic and VBScript variables and data types: even though VBScript does not inherently offer the benefits that come with the rigidity of Visual Basic's strong typing and declared data types, VBScript programmers can still realize these benefits. Realizing the benefits takes two things.

First, we must understand how the Variant data type works-in particular, how the Variant subtypes correspond almost exactly to the Visual Basic data types. There are specific ways to control the subtype of a Variant variable so that your programming techniques won't be that much different than if you were programming in Visual Basic. We'll learn these techniques in this chapter.

Second, when we program in VBScript, we must pretend we are programming in Visual Basic. We must pretend that each variable we declare has been declared with a specific data type. Just because the VBScript runtime engine does not care if we store the value "Bob's Hardware Store" in the OrderTotal variable does not mean that we can't be careful to ensure that our code never does that. In fact, when we introduce the 'Hungarian naming convention' later in this chapter you'll see a way that you can declare your intention for each variable to hold a specific data type even though VBScript will not enforce that intention in the way that Visual Basic would.