Variable naming rules-Hungary naming rules!

Source: Internet
Author: User

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

A few years ago, Charles Simonyi (who later became a well-known programmer at Microsoft) designed a post-fix-based naming method known as "Hungary notation" to remember him. his idea is to give each identifier a prefix based on its meaning. microsoft later adopted this idea to give each identifier a prefix to describe its data type. therefore, the prefix of the integer variable is N, the long integer variable is NL, the numeric array variable is ca, and the string (character array ending with an empty type) is prefixed with Sz. these names may be odd. for example, lpszfoo indicates that "foo" is a long integer pointer to a string ending with an empty character.

 

The advantage of this method is that you can identify the type of a variable by its name, instead of looking for its definition. unfortunately, this method not only bypasses the variable name, but also makes it very difficult to change the variable type. in windows3.1, the integer variable is 16 in width. if we use an integer variable at the beginning, but after using the 30---40 function, we find that the width of the integer variable is not enough. In this case, we should not only change the type of this variable, in addition, you need to change the variable name in the 30--40 functions.

 

Because it is impractical, no one except some stubborn windows programmers can use "Hungary notation. there is no doubt that it still exists on some occasions, but most people have abandoned it now. generally, the input prefix is a bad idea because it binds the variable together with its type.

The Hungary method is generally advantageous for functions with less than 30 rows.

Especially for interface programming, it has advantages.

However, with strong algorithm requirements, especially many abstract C ++ programs, the Hungarian method is a disaster.

See where you are using it.

Now we have good ide tools, such as Vc and sourceinsight.

When you select a variable, the system will automatically prompt you for its declaration and definition.

There is no need for the Hungarian naming law.

This is nothing more than for better program readability.

In fact, good code writing habits are more important than forcing the Hungarian naming method.

Systematic. Integrity. Readability. The classification should be clear. Annotations are required!

 

The Hungarian naming convention is a naming convention promoted by Microsoft for various types of symbols such as variables, functions, objects, prefixes, and macro definitions. The main idea of the Hungarian naming method is to add prefixes to variables and function names to enhance people's understanding of programs. It was launched by a Hungary inside Microsoft. As a result, it gradually became popular within Microsoft and spread to Windows developers around the world. The following describes the Hungarian naming method, which will be followed by the above Code style as much as possible in the subsequent examples. In other words, not all readers are required to abide by the rules, but readers are expected to follow the rules as a modern software developer.

 

 

A Array

B bool (INT) Boolean (integer)

By unsigned char (byte) unsigned characters (bytes)

C char character (byte)

CB count of bytes

Cr color reference value color (reference) Value

CX count of X (short) x set (short integer)

Dw dword (unsigned long) Dual-character (unsigned long integer)

F flags (usually multiple bit values) Sign (usually a multi-digit value)

FN Function

G _ global Global

H handle

I integer

L long integer

LP long pointer

M _ data member of a class

N short int short integer

P pointer

S string

SZ zero terminated string ending with 0

TM text metric text rules

U unsigned int unsigned integer

Ul unsigned long (ulong) unsigned long integer

W word (unsigned short) unsigned short integer

X, y x, Y coordinates (short) coordinate value/short integer

V void is empty.

 

 

 

The global variables of the project start with G _, and the class member variables start with M _. If the local variables are large, you can use L _ to show that they are local variables.

 

Prefix type example

G _ global variable g_servers

C Class or struct cdocument, cprintinfo

M _ member variables m_pdoc, m_ncustomers

 

List of common VC prefixes:

 

Prefix type Description Example

Ch char 8-character chgrade

Ch tchar 16-bit Unicode Character chname

B bool Boolean variable benabled

N int INTEGER (the size is determined by the operating system) nlength

N uint unsigned integer (the size is determined by the operating system) nlength

WWord 16-bit unsigned integer wpos

L long 32-bit signed integer loffset

Dw dword 32-bit unsigned integer dwrange

P * ambient Memory Model pointer memory 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

 

Abbreviation of Windows Object Name:

 

Windows Object example variable MFC class example object

Hwnd; cwnd * pwnd;

Hdlg; cdialog * pdlg;

HDC; CDC * PDC;

Hgdiobj; cgdiobject * pgdiobj;

Hpen; cpen * ppen;

Hbrush; cbrush * pbrush;

Hfont; cfont * pfont;

Hbitmap; cbitmap * pbitmap;

Hpalette; cpalette * ppalette;

Hrgn; crgn * prgn;

Hmenu; cmenu * pmenu;

Hwnd hctl; cstatic * pstatic;

Hwnd hctl; cbutton * pbtn;

Hwnd hctl; cedit * pedit;

Hwnd hctl; clistbox * plistbox;

Hwnd hctl; ccombobox * pcombobox;

 

List of commonly used macro definitions in VC:

 

Example range of prefix symbols

Idr_identifies the types of resource sharing idr_mainframe 1 ~ 0x6fff

Resource (DIALOG) idd_spell_check 1 ~ 0x6fff

Hidd _ helps hidd_spell_check 0x20001 ~ Based on the context of the dialog box ~ 0x26ff

Idb_bitmap Resource (Bitmap) idb_company_logo 1 ~ 0x6fff

IDC _ cursor idc_penpencil 1 ~ 0x6fff

IDI _ icon Resource (icon) idi_notepad 1 ~ 0x6fff

Id_tools_spelling 0x8000 ~ 0 xdfff

Hid _ command context help hid_tools_spelling 0x18000 ~ 0x1dfff

The IDP _ message box prompts the text resource idp_invalid_partno 8 ~ 0 xdfff

Hidp _ message box context help hidp_invalid_partno 0x30008 ~ 0x3dfff

IDS _ string resource (string) ids_copyright 1 ~ 0x7fff

Idc_recalc 8 ~ 0 xdfff

 

Microsoft MFC macro naming rules

 

Name type

_ Afxdll's unique dynamic connection library (DLL) version

_ ALPHA: only compiles the DEC Alpha processor.

_ Debug includes debugging versions for Diagnosis

_ MBCS compile multi-byte character sets

_ Unicode open Unicode in an application

Functions provided by afxapi MFC

Callback functions that Use Pointer callback

 

Library identifier name Method

 

Identifier value and meaning

U ansi (n) or Unicode (u)

D debugging or release: D = debugging; ignore the identifier as release

 

Static library version naming rules

 

Library description

Debugging version of nafxcwd. Lib: MFC static Connection Library

Nafxcw. Lib release: MFC static Connection Library

Uafxcwd. Lib debugging version: the static Connection Library of MFC with Unicode support

Uafxcw. Lib release version: static Connection Library of MFC with Unicode support

 

Dynamic Connection Library naming rules

 

Name type

_ Afxdll unique dynamic connection library (DLL) version

Functions provided by winapi windows

 

New naming rules in windows. h

 

Type Definition description

Winapi uses the far Pascal position in the API declaration. If you are writing a DLL with an export API population point, you can use this type in your own API.

Callback uses the position of far Pascal in the application callback program, such as the window and dialog box.

The same as lpstr, but the same as the definition (const char far *)

The portable unsigned integer type of uint, which is determined by the host environment (32-bit for Windows NT and Windows 9x). It is a synonym for unsigned Int.

Type of the return value of the lresult window Program

Lparam declares the type used by lparam. lparam is the fourth parameter of the window program.

Wparam declares the type used by wparam. wparam is the third parameter of the window program.

Lpvoid is generally a pointer type, which is the same as (void *) and can be used to replace lpstr

 

Msdn provides a code reference for following the code style and the Hungarian naming method:

 

1 # include "Sy. h"

2 extern int * rgwdic;

3 extern int bsymac;

4 struct Sy * psysz (char SZ [])

6 {

7 char * PCH;

8 int CCH;

9 struct Sy * Psy, * psycreate ();

10 int * pbsy;

11 int cwsz;

12 unsigned whash = 0;

13 PCH = SZ;

14 While (* PCH! = 0)

15 whash = whash <> 11 + * PCH ++;

16 CCH = PCH-SZ;

17 pbsy = & rgbsyhash [(whash & 077777) % cwhash];

18 For (; * pbsy! = 0; pbsy = & psy-> bsynext)

19 {

20 char * szsy;

21 szsy = (Psy = (struct Sy *) & rgwdic [* pbsy])-> SZ;

22 PCH = SZ;

23 while (* PCH = * szsy ++)

24 {

25 if (* PCH ++ = 0)

26 return (Psy );

27}

28}

29 cwsz = 0;

30 if (CCH> = 2)

31 cwsz= cch-2/sizeof (INT) + 1;

32 * pbsy = (int *) (Psy = psycreate (cwsy + cwsz)-rgwdic;

33 zero (int *) Psy, cwsy );

34 bltbyte (SZ, Psy-> SZ, CCH + 1 );

35 return (Psy );

36}

 

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.