Hungarian nomenclature is the naming standard in programming. The basic principle is: variable name = attribute+type+object description, in which the name of each object requires a clear meaning, and you can take the full name or partial name of the object. Naming should be based on the principle of easy memory and understanding. It is very important to ensure the consistency of names.
For example, the name of a form can be abbreviated as frm in Hungarian nomenclature. When the form variable is called Switchboard, the full name of the variable should be frmSwitchboard. So it is easy to see from the variable name that the switchboard is a form. Similarly, if the variable type is a label, it should be named lblSwitchboard. It can be seen that Hungarian nomenclature is very easy to remember, and makes variable names very clear and easy to understand, which enhances the readability of the code and facilitates the communication between programmers.
It is said that this term was invented by a Hungarian programmer named Charles Simonyi. Later, he stayed in Microsoft for several years, so this nomenclature spread all over the world through various products and documents of Microsoft. Nowadays, most programmers use this nomenclature to some extent, no matter what software they use for development. The starting point of this naming method is to combine variable names in the order of: attribute+type+object description, so that programmers can have an intuitive understanding of the type and other attributes of variables when making variables. The following are HN variable naming conventions, some of which are my personal preferences:
Attribute part
global variable
g_
constant
c_
C++ class member variable
m_
static variable
s_
Type part
needle
p
function
[Mathematics] Function
be invalid
v
deal with
h
Long integer
l
Boole
b
Floating-point types (sometimes called files)
f
Double word
Width of development (abbreviation of Developed Width)
character string
Size (short for size)
Short integer
n
Double precision floating point
d
count
C (usually cnt)
Character; Role; letter
Ch (usually c)
integer
I (usually n)
byte
pass by
word
w
Real type
r
No symbol
u
Descriptive part
highest
maximum
minimum
Fujian dialect
initialization
initialization
Temporary variable
temperature
Source object
Scientific research Committee
destination object
Software platform for building environment and HVAC system simulation
Here are a few examples by the way:
Hwnd: h is the type description, indicating the handle, wnd is the variable object description, indicating the window, so Hwnd indicates the window handle;
PFNeatapple: PFN is a type description, which means a pointer to a function, and eatapple is a variable object description, so it means.
Function pointer variable pointing to EatApple function.
G _ Cch: G _ is an attribute description, representing a global variable, and C and ch are counting type and character type respectively, representing a variable class together.
Type, where the object description is ignored, so it represents a global variable that counts the number of characters.
The above are the general rules of HN nomenclature.
Summary: Hungarian nomenclature
Hungarian nomenclature
MFC, handle, control and structure naming specification window type example variable MFC class example variable
HWND hWndCWnd * pWnd
HDLG hDlg; CDialog * pDlg
HDC hDCCDC * pDC
HGDIOBJ hGdiObjCGdiObject * pGdiObj
HPEN hPen; CPen * pPen
HBRUSH hBrushCBrush * pBrush
HFONT hFontCFont * pFont
HBITMAP hBitmapCBitmap * pBitmap
HPALETTE hPaltteCPalette * pPalette
HRGN hRgnCRgn * pRgn
HMENU hMenuCMenu * pMenu
HWND hCtlCState * pState
HWND hCtlCButton * pButton
HWND hCtlCEdit * pEdit
HWND hCtlCListBox * pListBox
HWND hCtlCComboBox * pComboBox
HWND hCtlCScrollBar * pScrollBar
HSZ hszStr; CString pStr
Point pt; CPoint angle;
Size; CSize size;
RECT rectangle; CRect rect
Example of common prefix naming specification prefix types
Class c or structure CDocument, CPrintInfo
M_ member variable m_pDoc, m _ n customer
Variable Naming Specification Prefix Type Description Instance
Ch char 8-bit character chGrade
Ch TCHAR If _UNICODE is defined, it is 16-bit chName.
boolean type
N int integer (its size depends on the operating system) nLength
Cancels unsigned values (whose size depends on the operating system) nHeight.
W word 16-bit unsigned value wPos
L offset of long 32-bit signed integer
32-bit unsigned integer
P * pointer pDoc
Lp far * far pointer lpszName
32-bit string pointer
32-bit constant string pointer
If defined by _UNICODE, Lpsz LPCTSTR is a 32-bit constant string pointer lpszName.
Window object handle
Lpfn CALLBACK is a far pointer to the callback function.
Prefix symbol type instance range
IDR_ Multiple different types of resources * * * Enjoy IDDR _ Mai in frame 1 ~ 0x6fff.
IDD_ dialog resource IDD _ spell _ check 1 ~ 0x6fff
Help context for HIDD_ dialog resource hidd _ spell _ check 0x20001~ 0x26ff
IDB_ bitmap resource IDB _ company _ logo1~ 0x6fff
IDC_ cursor resource IDC _ pencil 1 ~ 0x6fff
IDI_ icon resource idi _ notepad 1 ~ 0x6fff
ID_ command id comes from menu item or toolbar _ toolsid _ tools _ spelling 0x8000 ~ 0xdfff.
HID_ command help context hid _ tools _ spelling 0x18000 ~ 0x1dfff
IDP_ message box prompts IDP _ IDP _ invalid _ partNo. 8 ~ 0xdeef.
HIDP_ message box help context hidp _ invalid _ partno0x30008 ~ 0x3deff
IDS_ string resource ids _ copyright 1 ~ 0x7eef
Controls IDC_ recalc8 ~ 0xdeef in IDC _ dialog box
Microsoft MFC Macro Naming Specification Name Type
A unique version of the dynamic link library (DLL) of _AFXDLL.
_ALPHA compiles only DEC Alpha processors.
_DEBUG includes the debug version of the diagnosis.
_MBCS compiles multibyte character sets
_UNICODE opens UNICODE in the application.
Functions provided by AFXAPI MFC
Callback A function that is called back by a pointer.
Library identifier Naming identifier value and meaning
U ANSI(N) or Unicode(U)
D debugging or release: D = debugging; Ignore published identifiers.
Static Library Version Naming Specification Library Description
Debugging version of NAFXCWD. LIB: MFC static connection library
NAFXCW。 LIB distribution: MFC static connection library
Debugging version of UAFXCWD. LIB: MFC static connection library supporting Unicode
UAFXCW。 LIB version: MFC static connection library supporting Unicode.
Dynamic Link Library Naming Specification Name Type
Unique version of _AFXDLL dynamic link library (DLL)
Winapi function provided by windows
New Naming Specification Type Definition Description in Windows.h
WINAPI uses the FAR PASCAL location in API declarations. If you are writing a DLL using the exported API padding points, you can use this type in your own API.
CALLBACK uses the location of FAR PASCAL in application callback routines such as windows and dialogs.
LPCSTR is the same as LPSTR, except that LPCSTR is used for read-only string pointers, and its definition is similar to (const char FAR*).
UINT can transplant unsigned integer type, and its size is determined by the host environment (32-bit for Windows NT and Windows 9x); It is synonymous with unsigned int.
The return value type of LRESULT windower.
LParam declares the type used by LPARAM, which is the fourth parameter of the window program.
WParam declares the type used by WPARAM, which is the third parameter of the window program.
LPVOID is a general pointer type, which is the same as (void *) and can be used instead of LPSTR.
-
Attack Hungarian nomenclature
Hungarian nomenclature is the naming standard in programming. Naming specification is the most important and controversial place in programming specification, and it has been a battleground for military strategists since ancient times. What's the use of naming conventions? Four words: well-founded. Through dichotomy, naming conventions can be divided into good naming conventions and bad naming conventions, that is, well-founded naming conventions and well-founded naming conventions. Good shoes are shoes that make dancers feel invisible, while bad shoes make dancers dance in chains. A bad naming convention is more destructive than a good one.
What this paper wants to prove is that Hungarian nomenclature is a bad naming standard. The scope of this paper is static strongly typed programming language. The analysis templates in this paper are C language and C++ language. Hungarian law is the abbreviation of Hungarian nomenclature below.
The cost of Hungarian nomenclature
The form of Hungarian law is to add the type name before the variable name, such as nfoo, szfoo, pfoo and cpfoo, which respectively represent integer variables, string variables, pointer variables and const pointer variables. It can be seen that Hungarian law copies the type information of variables from a single place (where variables are declared) to multiple places (where variables are used), which is a redundant method. One of the costs of redundant methods is to maintain the consistency of copies. When you need to change the type of variables in the process of writing and maintaining code, you have to pay this price. The second cost of redundancy method is to occupy extra space. A good writer will consciously abide by a rule: the minimum organizational unit length of code should be less than 30 natural lines, and if it exceeds 50 lines, it will be reorganized. The writing space of variables will add unnecessary difficulty to this rule.
Second, the benefits of Hungarian nomenclature
This proves that the benefits of Hungarian nomenclature are vague and unpredictable.
Template 1: strcpy (pstrfoo, pcstrfoo2) and strcpy (foo, foo2)
What are the benefits of Hungary and France here? I do not see any at all. No programmer will admit that he doesn't know the parameter type of strcpy function.
Template 2: Unknown Function (nfoo) and Unknown Function (foo)
What are the benefits of Hungary and France here? I do not see any at all. For a function that doesn't know the definite type, the programmer has to look up the document of the function, which is a cost. The only advantage of using the Hungarian method is that anyone who reads the code knows that this function needs an integer parameter. What's the use? A function is an interface, and the types of parameters are only a small part of the interface. Function, exit information, thread safety, exception safety, parameter legality and other important information. Still need to consult.
Model 3: nfoo = nbar vs foo = bar
What are the benefits of Hungary and France here? I do not see any at all. The only advantage of using the Hungarian method is that people who read the code know that there is a copy of an integer variable here, which sounds fine and they can sleep in peace. If he sees nFoo=szBar, he may wake up from a beautiful dream. Wait a minute. Is that true? I think we should wake up the compiler first. On the other hand, nFoo=nBar is only grammatically legal. What people who read the code really care about is the legitimacy of semantics, which doesn't help Hungary and France. On the other hand, a good writer will consciously follow a rule: one or two temporary variables are appropriate in the smallest organizational unit of the code, and more than three will be reorganized. Combined with the first rule mentioned above, we can draw the conclusion that the code that is easy to understand should be easy to understand itself, which is the built-in high quality of the code. Good naming convention is of limited help to the high quality of built-in buildings, while bad naming convention is more harmful to the high quality of built-in buildings than people think.
Third, the implementation of Hungarian nomenclature.
It needs to be proved that Hungarian nomenclature is difficult to realize in C language, and it is also impossible to realize in C++ language. Logically speaking, it is unnecessary to demonstrate the feasibility of Hungarian law after making a negative conclusion on the benefits of Hungarian law. However, since Mark once revived the enemy who was shot, I'd better set foot on it again.
As mentioned above, Hungarian law is the redundancy of the type system, so the key to realize Hungarian law is whether we can accurately copy the type system. It depends on the complexity of the type system.
Let's look at the C language:
1. Built-in types: int, char, float, double are copied as n, ch, f, d? There seems to be nothing wrong. But who can tell me what vanity should say?
2. Combination type: array, union, enumeration, structure copy as A, U, E, S? It seems awkward.
The difficulty here is not naming the main type, but naming the subtype. An stands for integer array? Sfoo, sbar stands for structure foo, structure bar? Ausfoo stands for joint structure foo array? Are you 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 C language does not distinguish different pointer types very strictly. Let's start the show: pausfoo stands for the joint structure foo array pointer? Ppp stands for pointer pointer. Pointer?
The nightmare is not over yet. Let's take a look at the C++ language with a richer type system:
1.class: If the struct in C language can be used to prevaricate the past, don't dream of using cls to prevaricate the class in C++. Strictly speaking, a class is not a type at all, but a tool for creating types. In C++, compared with the number of user-defined types created by class, the number of language built-in types is completely negligible. StdvectorFoo stands for standard library vector type variable Foo? Crazy idea.
2. Namespace: boostfilesystemiteratorFoo, which means that the file system subspace of boost space traverses the directory type variable Foo? The programmer is going to crash.
3. Template: Remember STD:: Map?
4. Template parameters: template
5. Type modification: static, extern, mutable, register, volatile, const, short, long, unsigned nightmare plus modification? Still a nightmare.
WM is a Windows message, a Windows message.