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. It is said that this naming method was invented by a Hungary programmer named Charles Simonyi. Later, he stayed at Microsoft for several years, as a result, this naming method has spread to the world through various products and documents of Microsoft. Nowadays, most programmers use this naming method more or less no matter what software they use for development.
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 enhances the readability of the code and facilitates the exchange of code between programmers.
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 (its 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 lpcstr 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: Remote pointer to 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
--------------------------------------------------------------------------------
Attack the Hungarian naming law
Hunya naming method is a naming convention for programming. Naming Conventions are the most important and the most controversial part of the procedural writing norms. They have been a place for military personnel since ancient times. What is the use of naming conventions? Four words: proper name. Using the binary method, naming rules can be divided into good naming rules and bad naming rules, that is, naming rules that are justified and not justified. A good dance shoe is a dance shoe that makes a dancer feel invisible. A bad dance shoe is used to make the dancer dance with handcuffs. A bad naming convention is more destructive than a good naming convention.
This article will prove that the Hungarian naming convention is a bad naming rule. The scope of this article is static strong type programming language. The analysis model in this article is C and C ++. The Hungary method is short for the Hungarian naming law below.
Cost of a Hungarian naming law
The expression of hunfa is to append the type name prefix to the variable name, for example, nfoo, szfoo, pfoo, and cpfoo respectively indicate the integer and string variables, pointer variables and constant pointer variables. It can be seen that hunfa copies the type information of a variable from a single location (where the variable is declared) to multiple locations (where the variable is used), which is a redundancy method. One of the costs of redundancy is to maintain copy consistency. This cost is required to change the variable type during code writing and maintenance. The second advantage of the redundancy method is that it occupies additional space. A good writer will consciously follow a rule: the minimum length of the Organization Unit of the Code should be 30 rows or less, and should be reorganized if there are more than 50 lines. The writing space of a variable adds unnecessary difficulty to this rule.
Benefits of the Hungarian naming law
Here we need to prove that the benefits of the Hungarian naming law are vague and unpredictable.
TEMPLATE 1: strcpy (pstrfoo, pcstrfoo2) vs strcpy (Foo, foo2)
What are the benefits of hunfa? I cannot see it. No programmer will admit that he does not know the parameter type of the strcpy function.
Template 2: unknown_function (nfoo) vs unknown_function (FOO)
What are the benefits of hunfa? I cannot see it. For a function that does not know the type, the programmer should check the document of the function, which is a cost. The only benefit of using hunfa is that the Code person knows that this function requires an integer parameter. What is the use of this function? A function is an interface. The parameter type is only a small part of the interface. Important information such as function functions, exit information, thread security, exception security, and parameter validity must still be reviewed.
TEMPLATE 3: nfoo = nbar vs Foo = bar
What are the benefits of hunfa? I cannot see it. The only benefit of using hunfa is that people who read the code know that an integer variable is copied. It sounds okay, so you can feel at ease. If he sees nfoo = szbar, he may be awakened from his dream. Slow, Will things really be like this? The first thing I should wake up with is the compiler. On the other hand, nfoo = nbar is only syntactically legal. people looking at code really care about the legitimacy of semantics, which hunfa does not help. On the other hand, a good writer will consciously follow the rule that one or two temporary variables in the smallest organization unit of the Code should be appropriate. If there are more than three temporary variables, they should be re-organized. Combined with the first rule above, we can draw the conclusion that the easy-to-understand code itself should be easy to understand, which is the built-in quality of the Code. The benefits of building a good naming convention for internal high quality are quite limited, and the damage of building a bad naming convention for internal high quality is greater than people think.
3. Implementation of the Hungarian naming law
It should be proved that the Hungarian naming method is difficult to implement in C language and cannot be implemented in C ++ language. Logically speaking, after making a negative conclusion on the profits of Hungary law, it is necessary to demonstrate the feasibility of Hungary law. However, considering that Jack Ma once revived an enemy that has been shot, it is wonderful that I continue to step on.
As mentioned above, hunfa is the redundancy of the type system, so the key to implementing hunfa is whether we can precisely replicate the type system. This depends on the complexity of the type system.
Let's take a look at the C language:
1. built-in type: int, Char, float, and double are copied to N, CH, F, D? It seems that there is no problem. But who will tell me how to express void?
2. Combination Type: array, union, Enum, struct copy to A, U, E, S? It seems awkward.
The difficulty here is not to name the primary type, but to name the secondary type. An indicates an integer array? Sfoo, sbar indicates the structure Foo, the structure bar? Ausfoo indicates the foo array of the union structure? Tired.
3. Special Type: pointer. Pointer should be a combination type in theory, but it can be considered as a built-in type in C language, because the C language does not strictly distinguish between different pointer types. The following shows the result: pausfoo indicates the foo array pointer of the union structure? Is PPP a pointer to a pointer?
The nightmare is not over yet. Let's take a look at the rich C ++ language of the type system:
1. Class: If the struct in the C language can still be blocked by stru, do not use Cls to block the class in C ++. Strictly speaking, class is not a type at all, but a tool for creating types. In C ++, the number of built-in types in the language is negligible compared with the number of custom types created by the class. Stdvectorfoo indicates the standard library vector type variable Foo? Crazy thoughts.
2. namespace: boostfilesystemiteratorfoo, which indicates that the boost space filesystem sub-space traverses the directory type variable Foo? The programmer is about to crash.
3. template: Do you remember the exact name of the STD: Map <STD: String, STD: String> type? I can't remember it. It seems like it's more than 255 characters. Let's spare it.
4. template parameters: Template <class T, class binarypredicate> const T & MAX (const T & A, const T & B, binarypredicate comp) smart you, please use hunfa to name T. God is laughing.
5. Type modifier: static, extern, mutable, register, volatile, const, short, long, unsigned what is the modifier for the nightmare? It's still a nightmare.