Variable naming rules------Hungarian naming rules!

Source: Internet
Author: User

A few years ago, Charles Simonyi (who later became Microsoft's famous programmer) devised a prefix-based naming method, which was later called the "Hungarian notation" to remember him. His idea was to give it a suffix based on what each identifier represented. Microsoft later adopted this idea, Give each identifier a prefix to describe its data type. Therefore, the integer variable is prefixed with n, the long integer variable is nl, the character array variable is the CA, and the string (an array of characters ending in an empty type) is prefixed with SZ. These names can be very odd. For example, Lpszfoo says "Foo" is a long pointer to a string that ends with a null character.

The advantage of this approach is that it allows a person to discern the type of a variable by its name, rather than to find its definition. Unfortunately, this approach not only makes the variable name very much more important, but also makes it very difficult to change the variable type. In Windows3.1, The integer variable is 16 wide. If we use an integer variable at the beginning, but after the calculation of the 40 functions in the---, we find that the integer variable width is not enough, then we need not only change the type of the variable, but also change the name of the variable in this 30--40 function.

Because of the unrealistic, no one has used the "Hungarian notation" in addition to some stubborn windows programmers. There is no doubt that in some cases it still exists, but most people have now abandoned it. In general, entering a prefix is a bad idea because it binds the variable tightly to its type.

For functions below 30 lines, the Hungarian method generally has an advantage.

Especially for interface programming, there are advantages.

But the Hungarian approach is a disaster for strong algorithmic requirements, especially for C + + programs with many abstract types.

See where you're going with it.

Now there are very good IDE tools, such as: Vc,sourceinsight and so on.

Checking the variable automatically prompts you to tell you its declaration and definition, so

There is no great need for the Hungarian nomenclature.

It is for the program readability is good.

In fact, a good code writing habit is more important than forcing the use of Hungarian nomenclature.

Systemic. Integrity. Readability. The classification should be clear. To have comments!

The Hungarian nomenclature is a naming convention for various types of symbols, such as variables, functions, objects, prefixes, and macro definitions, which Microsoft promotes. The main idea of the Hungarian nomenclature is to add prefixes to variables and function names to improve understanding of the program. It was launched by a Hungarian inside Microsoft, and it became popular within Microsoft and was extended to Windows developers around the world. The Hungarian nomenclature is described below, and the following example will try to follow it and the code style above. Still, not all readers are required to abide by it, but it is hoped that the reader will abide by it as a modern software developer.

A array of arrays

b bool (int) Boolean (integer)

by Unsigned Char (byte) unsigned character (bytes)

C Char character (bytes)

cb Count of bytes bytes

CR Color reference value color (reference) values

A collection of CX Count of x (short) x (shorter integers)

DW DWORD (unsigned long) double word (unsigned long integer)

F flags (usually multiple bit values) flag (usually with multiple digits)

fn function functions

G_ Global's

H Handle Handle

I integer integers

L Long Integer

LP Long pointer length pointer

M_ data member of a class of a classes

n Short int shorter integer

P Pointer Pointer

S string strings

SZ Zero terminated string ending with 0

TM Text Metric Textual rules

u Unsigned int unsigned integer

UL Unsigned Long (ULONG) unsigned length integer

W WORD (unsigned short) unsigned integer

x, y x, coordinates (short) coordinate value/shorter integer

v void null

The global variables for the project start with G_, class member variables with M_, local variables if the function is larger, you can consider using L_ to show that it is a local variable.

Prefix type example

G_ global variable G_servers

Class C or structural body cdocument,cprintinfo

M_ member Variable M_pdoc,m_ncustomers

VC Common Prefix list:

Prefix Type Description Example

CH Char 8-bit character Chgrade

CH TCHAR 16-bit Unicode type character chname

b BOOL Boolean variable benabled

n int integer (its size is determined by the operating system) nlength

n UINT unsigned integer (its size is determined by the operating system) nlength

W WORD 16-bit unsigned integer wpos

L LONG 32-bit signed integer loffset

DW DWORD 32-bit unsigned integer dwrange

P * Ambient memory model pointer RAM module pointer, pointer variable PDOC

LP far* long Pointer Lpdoc

Lpsz LPSTR 32-bit string pointer lpszName

Lpsz LPCSTR 32-bit constant string pointer lpszName

Lpsz LPCTSTR 32-bit Unicode type constant pointer lpszName

H handle Windows object handle HWND

LPFN (*FN) () callback function pointer Callback far pointer to Callback function lpfnabort

Windows object name abbreviation:

Windows Object Example variables MFC class Example Object

HWND hwnd; cwnd* pWnd;

Hdlg Hdlg; cdialog* Pdlg;

HDC hdc; cdc* PDC;

Hgdiobj Hgdiobj; cgdiobject* Pgdiobj;

Hpen Hpen; cpen* Ppen;

Hbrush Hbrush; cbrush* Pbrush;

Hfont Hfont; cfont* Pfont;

Hbitmap Hbitmap; cbitmap* Pbitmap;

Hpalette Hpalette; Cpalette* Ppalette;

HRGN HRGN; crgn* prgn;

HMENU HMENU; cmenu* Pmenu;

HWND Hctl; cstatic* pstatic;

HWND Hctl; cbutton* pbtn;

HWND Hctl; cedit* PEdit;

HWND Hctl; clistbox* Plistbox;

HWND Hctl; ccombobox* Pcombobox;

VC common macro definition named list:

Prefix symbol type symbol example range

IDR_ identifies the type of multiple resource shares Idr_mainframe 1~0X6FFF

Idd_ dialog Resource (Dialog) Idd_spell_check 1~ 0x6fff

Hidd_ dialog-based context help Hidd_spell_check 0X20001~0X26FF

Idb_ bitmap Resource (Bitmap) Idb_company_logo 1~0x6fff

IDC_ cursor resource (cursor) Idc_pencil 1~0X6FFF

Idi_ icon Resource (icon) Idi_notepad 1~0x6fff

Id_, idm_ toolbar, or menu bar command items id_tools_spelling 0x8000~0xdfff

Hid_ command Context help hid_tools_spelling 0X18000~0X1DFFF

IDP_ message box Prompt text resource Idp_invalid_partno 8~0xdfff

HIDP_ message box context help Hidp_invalid_partno 0X30008~0X3DFFF

Ids_ string Resource (String) Ids_copyright 1~0x7fff

IDC_ Control resources in the dialog box Idc_recalc 8~0xdfff

Microsoft MFC Macro Naming conventions

Name Type

_afxdll unique dynamic Link Library,dll version

_alpha only the DEC ALPHA processor is compiled

_DEBUG includes debug versions of diagnostics

_mbcs Compiling multibyte character sets

_unicode to open UNICODE in an application

AFXAPI functions provided by MFC

CALLBACK functions that pass the pointer callback

Library identifier naming method

Identifier value and meaning

U ANSI (N) or Unicode (U)

D Debug or Release: D = Debug; Ignore identifier for release

Static library version naming specification

Library description

NAFXCWD. LIB Debug Version: MFC Static Connection Library

NAFXCW. LIB release Version: MFC static Connection Library

UAFXCWD. LIB Debug Version: MFC static connection library with Unicode support

UAFXCW. LIB release Version: MFC static connection library with Unicode support

Dynamic Connection Library Naming conventions

Name Type

_afxdll Unique Dynamic Connection library (DLL) version

WINAPI functions provided by Windows

New naming conventions in Windows.h

Type definition Description

WINAPI uses the far Pascal location in the API declaration, if you are writing a DLL with an exported API population point, you can use that type in your own API

CALLBACK uses the location of Far Pascal in application callback programs such as Windows and dialog boxes

LPCSTR is the same as LPSTR, except that LPCSTR is used for read-only string pointers and is similar in definition (const char far*)

A UINT-portable unsigned integer type whose size is determined by the host environment (32-bit for Windows NT and Windows 9x); it is a synonym for unsigned int

LRESULT the type of the return value of the window program

LPARAM declares the type used by LPARAM, LPARAM is the fourth parameter of a window program

WPARAM declares the type used by WPARAM, WPARAM is the third parameter of a window program

LPVOID generic pointer type, same as (void *), can be used instead of LPSTR

A code reference that adheres to the code style and Hungarian nomenclature is given in MSDN as follows:

1#include "sy.h"2 extern int*Rgwdic;3 extern intBsymac;4 structSY *psysz (Charsz[])6 {7 Char*pch;8 intCCH;9 structSY *psy, *psycreate ();Ten int*Pbsy; One intCwsz; Aunsigned whash=0; -Pch=sz; -  while(*pch!=0) theWhash=whash<> One+*pch++; -cch=pch-sz; -pbsy=&rgbsyhash[(whash&077777)%Cwhash]; -  for(; *pbsy!=0; Pbsy = &psy->bsynext) + { - Char*Szsy; +Szsy= (psy= (structsy*) &rgwdic[*pbsy])sz; APch=sz; at  while(*pch==*szsy++) - { - if(*pch++==0) - return(PSY); - } - } incwsz=0; - if(cch>=2) tocwsz=cch-2/sizeof(int)+1; +*pbsy= (int*) (Psy=psycreate (CWSY+CWSZ))-Rgwdic; -Zero (int*) psy,cwsy); theBltbyte (SZ, Psy->sz, cch+1); * return(PSY); $}

Variable naming rules------Hungarian naming rules!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.