Page

4.1.3- Naming Variables

  by NT Community Manager.
Last Updated  by Joel Bush.  

PublicCategorized as 04. Variables.

Not tagged.
<< 4.1.2- Data Types in VBScript, or One Type For AllChapter44.1.4- Naming Conventions >>

Naming Variables

As we go, we'll look at the different types of variables, how to assign values to them, and how to use them in expressions. We'll also talk about the kinds of names that you should give to your variables. For example, while the variable names above reflect their contents in a relatively self-explanatory way, the meanings of the variables in the following expressions are less obvious:

 

a = 1*x+73

varBoolean = true

 

They're not particularly helpful, are they? It's really up to the programmer to find a suitable name for his variable when he creates it. Ideally, you should find a name that is meaningful to a developer who subsequently reads your code. At the same time, excessively long variable names are unwieldy and easy to mistype, so you should avoid these too. If the variable names are chosen well, then the thinking behind the apparent gobbledygook in expressions like those above will become clearer. It's a good idea to make variable names descriptive even if this means making them longer. Here's some quick tips:

 

  • DateStart and DateEnd are better then StartDate and EndDate, as these two related functions will then come next to each other in a search
  • Variables like Date, Price, Name and Number are confusing because there are usually more than one of these, like NameFirst, NameLast, NameSpouse, PriceBuy, or PriceSell
  • Avoid confusing abbreviations
  • Never use the same variable name for two different variables, no matter how sure you are that there will not be a conflict
  • Within a procedure or function you may want to start names with a code letter like p_ or f_
  • Create name with multiple words, the first letter of each word in uppercase

In most languages, the name of a variable can be almost anything you choose, but there are usually a few restrictions:

 

  • There's usually a practical limit to the length of your variable names. In VBScript, the limit is 255 characters – this should be more than ample for most people!
  • There are usually restrictions on which characters you can use in your variable names. In VBScript there are restrictions, such as all variable names must begin with a letter, and variable names must not contain an embedded period/full-stop. In fact you're better off avoiding symbols altogether, other than dashes and underscores, to keep your code readable and to guarantee it will work as intended.
  • Case-sensitivity is another important issue. VBScript is case-insensitive, which means that you can use upper- and lower-case characters to refer to exactly the same variable. For example, VBScript will interpret counter and COUNTER as one and the same. On the other hand, JScript/JavaScript is case sensitive and would interpret counter and COUNTER as two entirely different entities.
<< 4.1.2- Data Types in VBScript, or One Type For AllChapter44.1.4- Naming Conventions >>

Copyright © 2003 by Wiley Publishing, Inc.

Powered by Near-TimeTerms of Services | Privacy Policy | Security Policy |