Naming rules in C # (Hungarian naming method)

Source: Internet
Author: User

Microsoft recommends the Hungary naming method, which is also applicable to most software production. The detailed rules are as follows:

The Hungarian naming convention is a naming convention for programming. The basic principle is: variable name = Attribute + Type + object description. Each object name must have a clear meaning. You can take the full name or part of the object name. Naming should be based on the principle of easy to remember and easy to understand. It is important to ensure name consistency.

For example, if the form name is form, it can be abbreviated as frm in the Hungarian naming method. When the form variable name is switchboard, the full name of the variable should be frmswitchboard. In this way, we can easily see from the variable name that switchboard is a form. Similarly, if this variable type is label, it should be named lblswitchboard. It can be seen that the Hungarian naming method is very easy to remember, and the variable name is very clear and easy to understand, which enhancesCodeEasy to readProgramMembers exchange code with each other.

This naming technique was proposed by Charles Simonyi, a competent Microsoft programmer, who was born in Hungary. The person who works with him at Microsoft is taught to use this convention. This is normal for them. However, people outside the Simonyi-led project team are very strange. They think this is a rigid expression, or even a strange look, because it was written in Hungary. This naming method is called the Hungarian naming method.

It is said that this naming method was invented by a Hungary programmer named Charles Simonyi. Later he stayed at Microsoft for several years, so
This naming method has spread to the world through various products and documents of Microsoft. Currently, most programmers do not need to use
Software development, more or less use this naming method. The starting point of this naming method is to change the volume name to: Attribute + Type
+ Object descriptions are combined in order to allow programmers to intuitively understand the type and other attributes of variables when making variables.
It is the HN variable naming convention, some of which are my personal bias:

Attribute section
Global Variables
G _
Constant
C _
C ++ class member variables
M _
Static variables
S _

Type
Pointer
P
Function
FN
Invalid
V
Handle
H
Long Integer
L
Boolean
B
Float (sometimes a file)
F
Double-Character
DW
String
SZ
Short integer
N
Double Precision Floating Point
D
Count
C (usually CNT)
Character
CH (usually C)
Integer
I (usually N)
Bytes
By
Word
W
Real-type
R
Unsigned
U

Description
Max
Max
Minimum
Min
Initialization
Init
Temporary Variable
T (or temp)
Source object
SRC
Target object
Dest

Here are some examples:
Hwnd: h indicates the type description, indicating the handle, and WND indicates the variable object description, indicating the window, so hwnd indicates the window handle;
Pfneatapple: PFN is a type description, indicating a pointer to a function. eatapple is a variable object description, so it indicates
The function pointer variable pointing to the eatapple function.
G_cch: G _ is the attribute description, indicating global variables. C and ch are the Count type and character type respectively, indicating the variable class together.
Type, the object description is ignored here, so it represents a global variable for counting characters.
The above is the general rule of HN naming.

Summary: Hungarian naming law

Hungarian naming convention

Naming rules for MFC, handles, controls, and structures: Windows sample variables: MFC sample Variables
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 hpaltte; cpalette * ppalette;
Hrgn; crgn * prgn;
Hmenu; cmenu * pmenu;
Hwnd hctl; cstate * pstate;
Hwnd hctl; cbutton * pbutton;
Hwnd hctl; cedit * pedit;
Hwnd hctl; clistbox * plistbox;
Hwnd hctl; ccombobox * pcombobox;
Hwnd hctl; cscrollbar * pscrollbar;
Hsz hszstr; cstring pstr;
Point pt; cpoint pt;
Size size; csize size;
Rect; crect rect;

General prefix naming rules prefix type instances
C Class or structure cdocument, cprintinfo
M _ member variables m_pdoc, m_ncustomers

variable naming convention prefix type description instance
CH char 8-character chgrade
CH tchar if _ Unicode is defined, it is a 16-character chname
B bool Boolean benable
N int INTEGER (the size depends on the operating system) nlength
N uint unsigned value (its size depends on the operating system) nheight
W word 16-bit unsigned value wpos
L Long 32-bit signed integer loffset
dw dword 32-bit unsigned integer dwrange
P * pointer pdoc
LP far * remote pointer lpszname
lpsz lpstr 32-Bit String pointer lpszname
lpsz lpstr 32-bit constant string pointer lpszname
lpsz lpctstr if _ Unicode is defined, it is a 32-bit constant string pointer lpszname
H handle windows object handle hwnd
lpfn callback pointing to the remote pointer of the callback function

Prefix type instance range
Idr_multiple Resource Sharing identities of different types idr_maiinframe 1 ~ 0x6fff
Resource idd_spell_check 1 ~ 0x6fff
Hidd _ dialog box resource help context hidd_spell_check 0x20001 ~ 0x26ff
Idb_bitmap resource idb_company_logo 1 ~ 0x6fff
IDC _ cursor resource idc_penpencil 1 ~ 0x6fff
IDI _ icon resource idi_notepad 1 ~ 0x6fff
Id_tools_spelling 0x8000 ~ 0 xdfff
Hid _ Command help context hid_tools_spelling 0x18000 ~ 0x1dfff
The IDP _ message box prompts idp_invalid_partno 8 ~ 0 xdeef
Hidp _ message box help context hidp_invalid_partno 0x30008 ~ 0x3deff
IDS _ string resource ids_copyright 1 ~ 0x7eef
Idc_recalc 8 ~ 0 xdeef

Microsoft MFC macro naming convention 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 convention 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 convention name Type
_ Afxdll unique dynamic connection library (DLL) version
Functions provided by winapi windows

Description of the new naming convention type definition in windows. h
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 call-back routine, 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

Related Article

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.