Friday, July 22, 2011

Variables and Data Types Basics

In simple terms, a variable is a place in the computer memory where your script holds a piece of information, or data. The data stored in a variable can be pretty much anything. It may be something simple, like a small number, like 5, or even complex, like a floating-point number such as 4.3, or a much bigger number like 971.123432434. Or it might not be a number at all and could be a word or a combination of letters and numbers. In fact, a variable can store pretty much anything you want it to store.

Behind the scenes, the variable is a reserved section of the computer’s memory for storing data. Memory is temporary — things stored there are not stored permanently like they are when you use the hard drive. Because memory is a temporary storage area, and variables are stored in the computer’s memory, they are therefore also temporary. Your script will use variables to store data temporarily that the script needs to keep track of for later use. If your script needs to store that data permanently, it would store it in a file or database on the computer’s hard disk.

To make it easier for the computer to keep track of the millions of bits of data that are stored in memory at any given moment, the memory is broken up into chunks. Each chunk is exactly the same size, and is given a unique address. Don’t worry about what the memory addresses are or how you use them because you won’t need to know any of that to use VBScript, but it is useful to know that a variable is a reserved set of one or more chunks. Also, different types of variables take up different amounts of memory.

In your VBScript program, a variable usually begins its life cycle after it is being declared before use.

It is not required to declare all variables before using. By default, VBScript allows you to use undeclared variables. However, it’s strongly recommended that you should declare the variables before you start using them in your scripts. Declaring variables before use makes code easier to read and to debug later. Just go for it!

Here goes an example for the same:

Dim VariableName

The above code is instructing your computer to reserve some memory space for you and to name that chunk VariableName. From now on, your computer (or, more accurately, the VBScript engine) keeps track of that memory for you, and whenever you use the variable name VariableName, it will know what you’re talking about.

Variables are very important in programming. Without them, there is no way to hold all the data that your script will be handling. Every input to the script, output from the script, and process within the script uses variables. They are the computer’s equivalent of the sticky notes that you leave all over the place with little bits of information on them. All the notes are important (otherwise why would you write them?) but they are also temporary. Some might become permanent (so you take a phone number and write it down in your address book or contact list), while others are thrown away after use (say, after reminding you to do something). This is how it works with variables, too. Some hold data that you might later want to keep, while others are just used for general housekeeping and are disposed of as soon as they’re used.

In VBScript, whenever you have a piece of information that you need to work with, you declare a variable using the exact same syntax you saw a moment ago. At some point in your script, you’ll need to do something with the memory space you’ve allocated yourself (otherwise, what would be the point of declaring it?). And what you do with a variable is place a value in it. This is called initializing the variable. Sometimes you initialize a variable with a default value. Other times, you might ask the user for some information, and initialize the variable with whatever the user enters. Alternatively, you might open a database and use a previously stored value to initialize the variable.

Initializing the variable gives you a starting point. After it has been initialized, you can begin making use of the variable in your script.

Here goes a simple VBScript example:

Dim strName
' Above we dimensioned the variable
str = "
Value stored inside variable"

Using Comments

You already know what the first line of code in the previous block does. It declares a variable for use called strName

The second line in the code is a comment. In VBScript, any text preceded by the single quote character (') is treated as a comment, which means that the VBScript engine completely ignores the text, which begs the question why bother typing it in at all? It doesn’t contribute to the execution of the script, right? This is absolutely correct, but don’t forget one of the most important principles of programming: It is not just computers that may have to read script. It is equally important to write a script with human readers in mind as it is to write with the computer in mind.

Of course, none of this means you should for one moment forget that when you write scripts, you must do so with the computer (or, more specifically, the script engine) in mind. If you don’t type the code correctly (that is, if you don’t use the proper syntax), the script engine won’t be able to execute the script. However, once you’ve written some useful scripts, you’ll probably need to go back to make some changes to a script you wrote six months or a year ago. If you didn’t write that code with human readers, as well as computers, in mind it could be pretty difficult to figure out what you were thinking and how you decided to solve the problems at the time you wrote the script. Things can get worse. What happens when you or one of your coworkers has to make some changes to a script you wrote many months ago? If you did not write that script to be both readable and maintainable, others who use your code will encounter difficulties deciphering it — no matter how well written the actual computer part of the code is.

Adding comments to your code is just one part of making sure code is clear and readable. There are many other things that you can do:

  • Choose clear, meaningful variable names.
  • Indent code for clarity.
  • Make effective use of white space.
  • Organize the code in a logical manner.
All of these aid human-readability and are covered later, but clear, concise comments are by far the most important. However, too much of a good thing is never good and the same is true for comments. Over-burdening code with comments doesn’t help. Remember that if you are scripting for the Web that all the code, including the comments, are downloaded to the browser, so unnecessary comments may adversely affect download times.

You have learnt about some good commenting principles, but for now just be aware of the fact that the comment in line 2 of the script is not really a good comment for everyday use. This is because, to any semi-experienced programmer, it is all too obvious that what you are doing is declaring the strName variable on the code line above. This is because the point of the code is to instruct the reader in how a particular aspect of VBScript programming works, and the best way to do that is to add comments to the code directly. It removes ambiguity and keeps the code and comments together.

Also worth noting is that comments don’t have to be on a separate line. Comments can also follow the code, like so:

Dim strName ' Initialize the variable
strName = InputBox("Hey! What is your name?") ' Ask for the user's name
MsgBox "Hello " & strName & "! Nice to meet you." ' Display a greeting in a popup

------------------------------------------------------------------------------------------------------------------------------------
Have questions on this article? Feel free to ask me in the comments section below.

If you like this post, I would suggest you to subscribe my RSS feed to have future articles delivered to your email address directly.
------------------------------------------------------------------------------------------------------------------------------------

0 comments: