Variable naming rules are designed to enhance the readability and ease of maintenance of your code. The following are the variable naming conventions that C + + must follow:
1. variable names can only be letters (A-Z , A-Z ) and Numbers (0-9 ) or underline (_ ) composition.
2. The first letter must start with a letter or an underscore.
3. cannot use C + + keyword to name variables to avoid conflicts.
4. variable names are case-sensitive.
variable naming rules:
First, use the shortest character to represent the most accurate meaning.
Second, use a variable prefix.
1. Prefix of integer type
int nId; int prefix: nshort sId; Short prefix: sunsigned int unId //unsigned int prefix: Unlong lId; Long prefix: l
2. Floating-point prefix
float Fvalue; Float prefix: fdouble dvalue; Double prefix: D
3. Prefix of character type
char Chchar; Char prefix: CH
4. String prefix
char szpath; Char string prefix: szstring strpath; String string prefix: strcstring strpath; MFC CString class prefix: str
5. Boolean prefix
BOOL Bisok; BOOL type prefix: Bbool bisok; MFC BOOL Prefix: b
6. Pointer type prefix
char * Ppath; Pointer prefix: P
7. Array prefix
int arrnnum; Array prefix: arrcstring arrstrname; Array prefix + type prefix + Name
8. Structure Prefix
STUDENT Txiaozhang; struct Prefix: t
9. Enumeration prefix
Enum Emweek; Enumeration prefix: EM
10, byte prefix
BYTE Byip; BYTE prefix: by
11, the prefix of the word
DWORD Dwmsgid; Double word prefix: Dwword wmsgid; Word prefix: w
12. Character pointer prefix
LPCTSTR Ptszname; TCHAR type is ptszlpcstr pszname; Pcszlpstr Pszname; Psz
13. STL Container Prefix
Vector Vecvalue; Vector container prefix: VEC
14. RECT Rectangle structure Prefix
RECT Rcchild; Rccrect rcchild/ //RC
15. Handle Prefix
HWND Hwnddlg; Hhbrush HBr; Hhpen Hpen; Hhbitmap Hbmpback; H
16. Windows Color Prefix
COLORREF Crfont; Cr
17. Windows DC Prefix
CDC dcclient; dc
Third, the member variables of the M_ class are at the beginning, followed by a variable, the variable is also prefixed.
CString M_strname; M_ start + type prefix + Name
Four, defines a variable that, in order to simplify, can use only prefixes without affecting the meaning of the variable.
RECT RC;
Five, The global variables are all g_ at the beginning, followed by a variable, the variable is also prefixed.
int g_id; G
Six, define the structure to ensure C and C + + compatible, with typedef statement, and the struct type is all uppercase to T_ starting with the pointer form to Pt_ start.
typedef struct TAG Tstudent{int nId; CString StrName;} Student,*pstudent; STUDENT Txiaozhang; Complete definition of struct body
Seven, A variable is made up of multiple words, and the first letter of each word is capitalized.
int Nstudentid; CString Strstudentname;
Eight, define a class with a C or T as the class name prefix.
Class Cmylistctrl;class Tmylistctrl;
Nine, MFC Control-bound value category or control class category, you need to m_ begins and prefixes.
CEdit M_edt_strvalue; Edit Bound control category CListBox M_lb_nname; Listboxclistctrl M_lc_name; Listctrl; CComboBox M_cb_name; /combobox
10. Control ID simplify and indicate the type and meaning of the control as much as possible.
Button Idc_bnt_name; Edit Idc_edt_name; ListBox Idc_lb_name; Listctrl Idc_lc_name; ComboBox Idc_cb_name;
Article turned from: http://blog.sina.com.cn/s/blog_8a7012cf01017h9p.html
C + + variable naming conventions