The Reddick VBA Naming Conventions - Introduction - Variable and Procedure names

An introduction to Hungarian notation

The RVBA conventions are based on the Hungarian style, named for the native country of Charles Simonyi,the inventor of this style of naming objects. The objective of Hungarian is to convey information about the object concisely and efficiently. Hungarian takes some getting used to, but once adopted, it quickly becomes second nature. The format of a Hungarian object name is as follows:

[prefixes]tag[BaseName[Suffixes]]

The square brackets indicate optional parts of the object name. These components have the following meanings:

·Prefixes—Modify the tag to indicate additional information. Prefixes are in all lowercase letters. They are usually picked from a standardized list of prefixes, given later in this article.

.Tag—Short set of characters, usually mnemonic, that indicates the type of the object. The tag is in all lowercase letters. It’s usually selected from a standardized list of tags, given later in this article.

.BaseName—One or more words that indicate what the object represents. The first letter of each word in the base name is capitalized.

.Suffixes—Additional information about the meaning of the BaseName. The first letter of each word in the Suffix is capitalized. They are usually picked from a standardized list of suffixes, given later in this article

Notice that the only required part of the object name is the tag. This may seem counterintuitive; you may feel that the base name is the most important part of the object name. However, consider a generic procedure that operates on any form. The fact that the routine operates on a form is the important thing, not what that form represents. Because the routine may operate on forms of many different types, you don’t necessarily need a base name. However, if you have more than one object of a type referenced in the routine, you must have a base name on all but one of the object names to differentiate them. Also, unless the routine is generic, the base name conveys information about the variable. In most cases a variable should include a base name.

Tags

You use tags to indicate the datatype of an object, and you construct them using the techniques described in the following sections.

Variable tags

Use the tags listed in Table 1 for VBA datatypes. You can also use a specific tag instead of “obj” for any datatype defined by the host application or one of its objects. (See the section “Host Application and Component Extensions to the Conventions” later in this article.)

Table 1. Tags for VBA variables.

Tag

Object Type

byt

Byte

f

Boolean

int

Integer

lng

Long

sng

Single

dbl

Double

cur

Currency

dat

Date

obj

Object

str

String

stf

String (fixed length)

var

Variant

Here are several examples:

lngCount

intValue

strInput

You should explicitly declare all variables, each on a line by itself. Don’t use the old type declaration characters, such as %, &, and $. They are extraneous if you use the naming conventions, and there’s no character for some of the datatypes, such as Boolean. You should explicitly declare all variables of type Variant, the default, as type Variant. For example:

Dim intTotal As Integer

Dim varField As Variant

Dim strName As String

Constant tags

You should indicate generic constants by using the tag “con.” If you need to differentiate one class of constants from another, you can invent a class name, such as glr (for Getz, Litwin, and Reddick), and append the letter “c” to the class—for example, glrcPi. You may want to do this if you have some specific component that has global constants and you want to ensure they don’t conflict with other constants. For example:

conPi

glrcError205

Tags for user-defined types and classes

User-defined types and user-created class objects are treated the same because user-defined types are really a kind of simple user-defined class. These objects have two components: the class name that defines the structure of the class and a tag that is used for instances of that class. Choose an appropriate name for the class. For example, if you had a user-defined class that described a glyph bitmap created at runtime on a form, the class name would be glyph. The tag would be an abbreviation of glyph—perhaps gph. If you had another class that was a collection of these objects, it would use glyphs and gphs, respectively. Some host applications, such as Access, don’t support class modules yet; however, you can also treat a form as a user-defined class with a user interface. For example:

gphGlyph

nclName

Collection tags

You treat a collection object with a special tag. You construct the tag using the datatype of the collection followed by the letter “s.” For example, if you had a collection of Longs, the tag would be lngs. If it were a collection of user-defined types with the tag gph, the collection would be gphs. Although, in theory, a collection can hold objects of different data types, in practice, each of the data types in the collection is the same. If you do want to use different data types in a collection, use the tag objs. For example:

intsEntries

erhsHandler

bscsBaseClass

Constructing procedures

VBA procedures require you to name various objects: procedure names, labels, and parameters. These objects are described in the following sections.

Constructing procedure names

VBA names event procedures, and you can’t change them. You should use the capitalization defined by the system. For user-defined procedure names, capitalize the first letter of each word in the name. For example:

cmdOK_Click

GetTitleBarString

PerformInitialization

Procedures should always have a scope keyword, Public or Private, when they are declared. For example:

Public Function GetTitleBarString() As String

Private Sub PerformInitialization

Naming parameters

You should prefix all parameters in a procedure call with ByVal or ByRef, even though ByRef is optional and redundant. Procedure arguments are named the same as simple variables of the same type, except that arguments passed by reference use the prefix “r”. For example:

Sub TestValue(ByVal intInput As Integer, ByRef rlngOutput As Long)

Function GetReturnValue(ByVal strKey As String, ByRef rgph As Glyph) As Boolean

Copyright © 1995 Greg Reddick. You may freely distribute this document.

Related

  1. Introduction - Variable and Procedure names
  2. Prefixes
  3. Suffixes
  4. Object variables and DAO - variables
  5. Database Explorer objects - Table, Query, Form etc
  6. Object Names


© Copyright SS64.com 1999-2014
Some rights reserved