DBA Scripts Archive for Windows |
|
Variable Naming Conventions
by Jeff Hunter, Sr. Database Administrator
Documentation is an important step in any coding task. Programmers are always encouraged to provide as much detail as possible in the form of comments when writing code. One form of documentation, however, that is often overlooked by programmers is the naming of program variables. It is always good practice to use suggestive variable names that will assist in the documenting of a program. A good convention for variable names can often assist someone in better understanding the operation of a program module or script.
With Windows scripting languages like VBScript and JScript, proper naming of variables becomes even more important. Remember that these scripting languages are not strongly typed. A variable name in VBScript or JScript can contain data of any type. It is considered good practice when working with these non-strongly types languages to restrict the type of data in a variable to a specific type. For example, when defining a variable to store a numeric type, don't attempt to reuse the same variable to store a String value later on in the script. Something that is useful when using scripting languages like this is to come up with a solid naming convention for variable names. A good naming convention allows the programmer to encode the intended data type in the variable name.
This article provides a brief overview of a convention I typically use when creating variable names in either VBScript or JScript. Although there are many different conventions that can be used, I find this one to be easy for programmers to catch on to within a very short period time after using it. It is modeled after the popular Hungarian notation used by C programmers. The Hungarian notation uses a three character, lower case prefix to each variable name that indicates the data type of the variable. For example, the prefix dtm would represent a Date (or Date and Time) data type, so the variable dtmTodaysDate would indicate a variable used to store a Date data type. The following is a list of prefixes used for VBScript and JScript modeled using Hungarian notation:
| Prefix | Subtype | Description | Sample |
|---|---|---|---|
| arr | Array |
Contains an array of variables. Since arrays are designed to store multiple objects, array names should always be plural. Many users follow another convention and use two prefixes with array names. The first prefix will be the constant arr and the second prefix will be the data type of the objects stored in the array. The example displays both conventions. |
arrEmployeeNames arrstrEmployeeNames |
| bln | Boolean |
Can contain either True or False |
blnIsToday |
| byt | Byte |
Integer value in the range of 0 to 255. |
bytDepartmentNumber |
| col | Collection |
Technically, a collection is not a variable subtype. It is mentioned in this table because it is good practice to use the col prefix to indicate a collection. Collections are used extensively in system administration scripts. |
colSystemEnvVars |
| (None) | Constant |
Constant values contain no prefix and use UPPER case letters with underscores. Constants cannot be altered like normal variables. |
Const STATE_SALES_TAX |
| cur | Currency |
Range of -922,337,203,685,477.5808 to 922,337,203,685,477.5807 |
curAnnualRevenue |
| dbl | Double |
Contains a double-precision floating-point number in the range -1.79769313486232E308 to -4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values. |
dblRateMultiplier |
| dtm | Date (Time) |
Contains a date value between January 1, 100 and December 31, 9999. (either a Date, Time, or Date and Time) |
dtmTodaysDate |
| err | Error |
Contains an error number value. |
errOpenFile |
| int | Integer |
Contains integer value in the range of -32,768 to 32,767. |
intNumberOfComponents |
| crs |
|
||
| lng | Long |
Contains an integer value in the range -2,147,483,648 to 2,147,483,647. |
lngBufferGets |
| obj | Object |
Contains a reference to an Object. An object variable represents an Automation object. |
objFSO |
| sng | Single |
Contains a single-precision floating-point number in the range -3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values. |
sngHourlyPayRate |
| str | String |
A variable length string of textual data. May contain any alphanumeric characters. |
strLastName |
| var | Variant |
A variable that can store different data types at different times. |
varMiscValue |
| g_ | Globals |
Variables with global scope. All global variables should have a g_ prefix. |
g_strScriptName |
|
Jeffrey Hunter is an Oracle Certified Professional, Java Development Certified Professional, Author, and an Oracle ACE. Jeff currently works as a Senior Database Administrator for The DBA Zone, Inc. located in Pittsburgh, Pennsylvania. His work includes advanced performance tuning, Java and PL/SQL programming, developing high availability solutions, capacity planning, database security, and physical / logical database design in a UNIX, Linux, and Windows server environment. Jeff's other interests include mathematical encryption theory, programming language processors (compilers and interpreters) in Java and C, LDAP, writing web-based database administration tools, and of course Linux. He has been a Sr. Database Administrator and Software Engineer for over 17 years and maintains his own website site at: http://www.iDevelopment.info. Jeff graduated from Stanislaus State University in Turlock, California, with a Bachelor's degree in Computer Science.
Copyright (c) 1998-2012 Jeffrey M. Hunter. All rights reserved.
All articles, scripts and material located at the Internet address of http://www.idevelopment.info is the copyright of Jeffrey M. Hunter and is protected under copyright laws of the United States. This document may not be hosted on any other site without my express, prior, written permission. Application to host any of the material elsewhere can be made by contacting me at jhunter@idevelopment.info.
I have made every effort and taken great care in making sure that the material included on my web site is technically accurate, but I disclaim any and all responsibility for any loss, damage or destruction of data or any other property which may arise from relying on it. I will in no case be liable for any monetary damages arising from such loss, damage or destruction.
Last modified on
Wednesday, 28-Dec-2011 14:16:06 EST
Page Count: 9136