11.5. Standardized Prefixes

 < Free Open Study > 

Further Reading

For further details on the Hungarian naming convention, see "The Hungarian Revolution" (Simonyi and Heller 1991).


Standardizing prefixes for common meanings provides a terse but consistent and readable approach to naming data. The best known scheme for standardizing prefixes is the Hungarian naming convention, which is a set of detailed guidelines for naming variables and routines (not Hungarians!) that was widely used at one time in Microsoft Windows programming. Although the Hungarian naming convention is no longer in widespread use, the basic idea of standardizing on terse, precise abbreviations continues to have value.

Standardized prefixes are composed of two parts: the user-defined type (UDT) abbreviation and the semantic prefix.

User-Defined Type Abbreviations

The UDT abbreviation identifies the data type of the object or variable being named. UDT abbreviations might refer to entities such as windows, screen regions, and fonts. A UDT abbreviation generally doesn't refer to any of the predefined data types offered by the programming language.

UDTs are described with short codes that you create for a specific program and then standardize on for use in that program. The codes are mnemonics such as wn for windows and scr for screen regions. Table 11-6 offers a sample list of UDTs that you might use in a program for a word processor.

Table 11-6. Sample of UDTs for a Word Processor

UDT Abbreviation

Meaning

ch

Character (a character not in the C++ sense, but in the sense of the data type a word-processing program would use to represent a character in a document)

doc

Document

pa

Paragraph

scr

Screen region

sel

Selection

wn

Window


When you use UDTs, you also define programming-language data types that use the same abbreviations as the UDTs. Thus, if you had the UDTs in Table 11-6, you'd see data declarations like these:

CH    chCursorPosition; SCR   scrUserWorkspace; DOC   docActive PA    firstPaActiveDocument; PA    lastPaActiveDocument; WN    wnMain;

Again, these examples relate to a word processor. For use on your own projects, you'd create UDT abbreviations for the UDTs that are used most commonly within your environment.

Semantic Prefixes

Semantic prefixes go a step beyond the UDT and describe how the variable or object is used. Unlike UDTs, which vary from project to project, semantic prefixes are some-what standard across projects. Table 11-7 shows a list of standard semantic prefixes.

Table 11-7. Semantic Prefixes

Semantic

Prefix Meaning

c

Count (as in the number of records, characters, and so on)

first

The first element that needs to be dealt with in an array. first is similar to min but relative to the current operation rather than to the array itself.

g

Global variable

i

Index into an array

last

The last element that needs to be dealt with in an array. last is the counterpart of first.

lim

The upper limit of elements that need to be dealt with in an array. lim is not a valid index. Like last, lim is used as a counterpart of first. Unlike last, lim represents a noninclusive upper bound on the array; last represents a final, legal element. Generally, lim equals last + 1.

m

Class-level variable

max

The absolute last element in an array or other kind of list. max refers to the array itself rather than to operations on the array.

min

The absolute first element in an array or other kind of list.

p

Pointer


Semantic prefixes are formatted in lowercase or mixed uppercase and lowercase and are combined with the UDTs and with other semantic prefixes as needed. For example, the first paragraph in a document would be named pa to show that it's a paragraph and first to show that it's the first paragraph: firstPa. An index into the set of paragraphs would be named iPa; cPa is the count, or the number of paragraphs; and firstPaActiveDocument and lastPaActiveDocument are the first and last paragraphs in the current active document.

Advantages of Standardized Prefixes

Standardized prefixes give you all the general advantages of having a naming convention as well as several other advantages. Because so many names are standard, you have fewer names to remember in any single program or class.


Standardized prefixes add precision to several areas of naming that tend to be imprecise. The precise distinctions between min, first, last, and max are particularly helpful.

Standardized prefixes make names more compact. For example, you can use cpa for the count of paragraphs rather than totalParagraphs. You can use ipa to identify an index into an array of paragraphs rather than indexParagraphs or paragraphsIndex.

Finally, standardized prefixes allow you to check types accurately when you're using abstract data types that your compiler can't necessarily check: paReformat = docReformat is probably wrong because pa and doc are different UDTs.

The main pitfall with standardized prefixes is a programmer neglecting to give the variable a meaningful name in addition to its prefix. If ipa unambiguously designates an index into an array of paragraphs, it's tempting not to make the name more meaningful like ipaActiveDocument. For readability, close the loop and come up with a descriptive name.

 < Free Open Study > 


Code Complete
Code Complete: A Practical Handbook of Software Construction, Second Edition
ISBN: 0735619670
EAN: 2147483647
Year: 2003
Pages: 334

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net