Naming Your Variables

There are a few rules to follow when naming a variable:

□ Use only letters, numbers, and the underscore symbol (_). No other symbols are allowed.

□ Variable names must start with a letter.

□ Do not use a reserved word for your variable name.

□ Variable names must be less than 255 characters.

Special characters and reserved words are discussed in Appendix K.

In addition to the rules you must follow when naming your variables, it's customary to follow some sort of naming convention when creating your variables. While you may choose any convention, the most popular is the Reddick naming convention. Appendix L provides detailed information about this naming convention, and guidelines for creating your own naming conventions. Although developers have the latitude to implement a recognized convention, to modify a convention to fit their style, or even to ignore conventions, it's strongly recommended that you at least create meaningful variable names. Meaningful names not only make it easier to read through the code, but they minimize conflicts and facilitate debugging. If you create variables with names such as varl, var2, and var3, you'll have a hard time keeping track of which variable you need to use for which statement.

This book sticks pretty closely to Reddick's naming conventions. So variables usually contain a prefix that determines their data type. A string variable, for example, will ususally have the str prefix, such as strSQL and strMsg, whereas a Boolean variable will have a bln prefix, such as blnUnderBudget and blnCurrentMeeting.

In addition to Reddick's naming conventions, some developers like to use an additional convention to make their code easier to interpret: adding a prefix to variable names to denote whether the variable is a global, private, or local variable. The following table describes the prefixes used to denote variable scope and lifetime.

Prefix

Variable Scope

Examples

Usage

g

Global variable

gobj; gcurPrice

Variables declared with the Public keyword

m

Private (module-level) variables

mSalesTotal

Variables declared with the Private keyword

s

Static variables

sintWhileCount

Local variables declared with the Static keyword

Was this article helpful?

0 0

Post a comment