How-to: VBScript Naming Convention

Some recommended prefixes for VBScript variables.

DataType  ShortPrefix Prefix Example

String      s          str   strFirstName
DateTime    t          dtm   dtmStart

Integer     i          int   intQuantity
Double      d          dbl   dblTolerance
Single                 sng   sngAverage
Long        l          lng   lngDistance
Byte                   byt   bytRasterData
Boolean     b          bln   blnFound

Array       a          arr   arrMyArray
Collection  c          col   colMyCollrction
Class                  cls   clsMyObject
Error                  err   errOrderNum
Object      o          obj   objCurrent

This is designed to generate variable names which are both short and descriptive. For example without a naming convention you might have variables called PartNumber, PartDescription, and Delivery_Date following this naming convention they would become intPart, strPart, dtmDelivery.

Some names are reseverd words, any name which exactly matches a datatype (String, Long, Int) e.g. string = "Testing" will produce an error: Illegal Assignment or compilation error. Using a naming convention prefix makes such naming collisions much less likely.

Try to avoid variable names which duplicate the prefix naming, i.e for a File System Object objFS makes more sense than objFSO - we already know that it's an object from the obj prefix.

The prefix can also be abbreviated to a single digit e.g. sFirstName, iQuantity this assumes you don't have any need to distinguish String from Single or DateTime from Double.

All VBScript variables are variants meaning that a variable can contain a string or an integer number or a date etc, This makes it easy to write VBScript code but it can also be easy to lose track of which variable should contain which type of data.

“If I had to live my life again, I'd make the same mistakes, only sooner” ~ Tallulah Bankhead.

Related

The Reddick VBA Naming Conventions (VBA not VBScript).


 
Copyright © 1999-2020 SS64.com
Some rights reserved