Previous Page TOC Next Page



- B -
Naming and Formatting Conventions for Visual C++ Objects and Variables


This book's Introduction lists the Hungarian Notation prefixes that are most commonly used. This appendix expands on prefixes and covers the formatting of code, which can help you write better applications.

These naming conventions use a modified form of Hungarian Notation that commonly is used in C and C++ programming. The term Hungarian comes from the nationality of the conventions' inventor, Charles Simonyi, who at one time worked on the development of Access 1.0 at Microsoft. The prefix sz, which defines a zero-terminated string variable in C, is a common letter combination in the Hungarian language.

You might wonder why you should follow these rules when naming your variables. The long and short of it is that when we all follow the same set of rules, it's much easier to interact with each other. Of course, no variable name, no matter how well formed, will make poorly written code good. But well written and well formatted code, with variable names that follow standards, is much easier to debug and maintain.



NOTE

It's a good idea to learn and use as many of the established standards as you can. When you're thinking about making a career change, the ability to show well-written code that conforms to standards will generally be a positive influence on prospective employers.



Hungarian Notation for Variables and Objects


There are a number of simple rules that you should follow when naming your variables and objects. The following is a set of rules that will assist you in making your code as readable as possible.



NOTE

Microsoft doesn't always follow these guidelines. The file DBDAOERR.H has #defined variables that are mixed case, and other Windows header files demonstrate the same use of mixed case.




#define NULL    ((void *)0)


NOTE

Remember that NULL (as used in C/C++) is different from null as used by databases. NULL in C/C++ is used to indicate an address that is undefined. The database null indicates a field that has no value, which is different from a field that contains a string of zero characters. A character field of zero characters isn't a null field.


Correct Incorrect
FirstName FIRSTNAME

firstname

Firstname

first_name

Table B.1 lists the more commonly used prefixes of Hungarian Notation. Nothing in the concept of using prefixes prevents you from creating your own prefixes for types that don't already have a prefix defined. However, resist the urge to change a prefix of a data type that already has a prefix defined.

Table B.1. Hungarian Notation prefixes.

Prefix Description
b BOOL (int)
by BYTE (unsigned char)
c char
cx, cy short (used as x or y length; the c stands for "count")
dw DWORD (unsigned long)
fn Function (not used often)
h Handle (generic)
i int
l LONG (long)
n short or int
p Pointer
s String
sz A string terminated by the 0 byte
w UINT (unsigned int) or WORD (unsigned word)
x, y short (used as an x-coordinate or y-coordinate)

Formatting Your C/C++ Source Code


When writing C or C++ code, you should try to make your source as attractive as possible.

Previous Page Page Top TOC Next Page