- Variables and Assignment
- Some Suggested Naming Standards
- Simple Calculations
- Writing Your Own Routines
- Sample Application: Calculating a Future Value
- Summary
- Q&A
Some Suggested Naming Standards
With all the different types of variables, and so many programmers using them, there are many possible names you can use when declaring variables. This can lead to a problem when you see the variable later. Unless you see the declaration, you might have difficulty knowing the variable's type. Similarly, if you inherit some code another developer has written, you might need to spend some time before you understand how they have named their variables. Naming conventions, preferably shared naming conventions, will reduce both of these difficulties by identifying the type of the variable.
A commonly used naming convention is to add a lowercase prefix to your variable names. The prefix identifies the type of variable. Table 3.5 shows one suggested set of prefixes.
Table 3.5 Suggested Naming Conventions
Variable Type |
Prefix |
Example |
Byte |
byt |
bytAge |
Short |
sht |
shtCount |
Integer |
i or int |
iSheep |
Long |
l or lng |
lPopulation |
Single |
sng |
sngThrust |
Double |
d or dbl |
dblInterest |
Char |
c |
cMiddleInitial |
String |
s or str |
sName |
Boolean |
b |
bIsOpen |
Date |
dt |
dtHireDate |
User-defined types |
Two or three important characters from the structure name |
Variables created, based on Point and Rectangle structures could be called ptLocation and recSize. |
Constants |
No prefix. Name is all uppercase, with each word separated with underscores (_). |
PI, TAX_RATE |
Enumerations |
Two or three significant characters |
dowWeekDay, colBackColor |
NOTE
Why this mishmash of single, double, and triple characters? I have to admit that the prefixes I use have changed over the years. My basic philosophy was originally to use a single character, to limit the amount of distraction caused by the prefixes. However, some prefixes would only cause more confusion. For example, what would be the data type of sValue? It could be a Short, Single, or String. For data types that start with the same letter, I have extended the prefix to multiple characters. I must admit, however, I still tend to use s for strings because it has been used so commonly. (You have to make some sacrifices to avoid carpal tunnel syndrome).
When you first start using these prefixes, you might find they are a little distracting. However, the prefixes quickly become natural, and the information they provide will be invaluable.