当前位置:网站首页>Camel case with Hungarian notation

Camel case with Hungarian notation

2022-07-06 18:56:00 Life needs depth

1. Hump naming rules

Hump nomenclature (camel-case) Once from Perl A mixed case format commonly used in languages , and Larry wall The best seller by et al 《program Perl》(o'Relly published ) The cover picture of is a camel , Hence the name . Hump method at C++ And other high-level languages , The underline method occupies the existing C Language system . The Hungarian method is win It is widely used among the old users of , However, the Hungarian method is less and less used in the current naming system . perhaps , To be exact, with the progress of Technology , This nomenclature gradually combined with the hump method to form a kind of suitable for today C++ And other high-level languages .  

Hump type nomenclature is divided into big hump (big camel-case) And the small hump (little camel-case) Two kinds of . among , The big hump is characterized by capital letters at the beginning and at the back , Often used for class names 、 Function name 、 Properties and namespaces . Comparison , The small hump is characterized by the fact that the first letter of the first word is lowercase and the next letter is capital , Commonly used for local variables .

## Summary General  

If you want to manage a slightly more complex system , A uniform set of foods 、 With hierarchy 、 Clear naming rules are essential and very useful tools .  

Active in Biology 、 chemical 、 army 、 prison 、 underworld 、 A large number of knowledgeable predecessors in various fields such as terrorist organizations have proved the correctness of the above axiom with practical actions for countless times . Except God ( Suppose he could change the order of all things in the world ) outside , No one has the strength to disdain pictograms . In the highly abstract and complex activity of software development , The importance of naming rules is particularly prominent . A well-defined and complete set of 、 The naming conventions used throughout the project will greatly improve the readability of the source code and the maintainability of the software . Before introducing the details , Let's first explain the overall principles of naming conventions :  

standard       explain                                                      
Unity When writing a sub module live derived class , Follow the naming convention style of its base class or overall module , Keep the naming convention style consistent throughout the module
The identification consists of The identifier shall be an English word or a combination thereof , It should be managed and leveled , You know what you mean , Use words appropriately and accurately
The principle of minimizing the length and maximizing the amount of information While keeping the meaning of an identifier clear , Try to shorten the length . The method is to use English accurately , Use word abbreviations at the same time
Avoid being too similar Do not appear similar identifiers that are only case sensitive , for example “i” And “I”,“function” And “Function” wait .
Avoid duplicate names in different levels of scope The program should not have local variables and global variables with identical names , Although the two functions are different, there will be no grammatical errors , But it's easy to misunderstand
Correctly name mutually exclusive identifiers Name mutually exclusive identifiers with correct antonyms , Such as :“nMinValue” and “nMaxValue”,“GetName()” and “SetName()”
Avoid numeric numbers in names Try to include numbers in your name , Such as value1,value2 etc. , Unless there is a logical need for numbering . This is to prevent programmers from being lazy , Refusing to name Dong Daojing leads to meaningless names ( Because number numbering is the easiest ).

## class / structure class  

Except for exceptional cases ( You do not want users to treat this class as a normal class ) Outside ,C++ class / The naming of structures should follow the following guidelines :

type Naming method
C++ class / Naming of structures Class names are in uppercase letters “C" start , Followed by one or more words . For convenience of definition , The first letter of each word should be capitalized
Recommended composition It is recommended to use nouns or adjectives for the naming of classes + The form of a noun , for example :“CAnalyzer”,“CFastVector” wait
Tradition C Structure naming Tradition C The names of structures are all composed of capital letters , Underline between words , for example :SERVICE_STATUS

differ C++ The concept of class , Conventional C A structure is just a way to tie a set of data together . Tradition C Naming rules for structures :

## function Function  

type explain
Function name The name of a function consists of one or more words , For convenience of definition , The first letter of each word should be capitalized
Recommended composition Function names should use verbs or verbs + Noun ( The verb object phrase ) In the form of . for example :“GetName()", "SetValue()","Reserve()"
Protect member functions Protected member functions should begin with an underscore “-” In order to show the difference between , for example :“————SetState()”
Private member functions Similarly , Private member functions should start with two underscores “__”, for example :“__DestoryImp()”
Virtual functions Virtual functions are used to Do start , for example :“DoRefresh()","_DoEncryption()”
Callbacks and event handlers Callbacks and time handlers are used to using words “On” start , for example :“_OnTimer()"

## Variable  

Variables should be the most used identifiers in the program , The naming convention for variables may be a set of C++ The most important part of the standard specification , The specific naming rules are as follows :

Variable name   Variables have a scope prefix + Type prefix + One or more words make up . In order to define , The first letter of each word should be capitalized . For some simple and clear local variables , You can also use simplified methods , for example :i,j,k....

Scope prefix           The scope prefix mainly indicates the visible range of a variable . Scopes can be as follows :

Prefix                 explain

nothing                   local variable

m_                   Member variables of class (member)

sm_                 Static member variable of class (static member)

s_                   Static variables (static)

g_                   External global variables (global)

sg_                 Static global variables (static glboal)

gg_                 Shared data segment global variables shared by processes (global global)

Unless you have to , Otherwise, you should use as few global variables as possible  

Type prefix             The type prefix indicates the type of a variable

Prefix                 explain

n                   Shaping and bit field variables (number)

e                   Enumerative variable (enumeration)

c                   Character variables (char)

b                   Boolean variables (bool)

f                   Floating point variables (float)

p                   Pointer variables and iterators (pointer)

pfn                 Especially for pointer variables of linear functions and function object pointers (pointer of function)

g                   Array (grid)

i                   Class (instance)

                    You can also define some special prefixes for frequently used classes , For example std::vector The intrinsic constraint variables of this container type , have access to v etc.

                    Type prefixes can be combined , for example “gc” Represents an array of characters ,“ppn” A pointer that represents a pointer that only wants to be shaped

char*/wchar_t* -> psz

char[]/wchar_t[] ->sz

sz Express string end of zero

S                   Structure SAddress

Prefix

type

describe

example

c

char

8 Bit character

cGrade

str string   Character strName

ch

TCHAR

If _UNICODE Definition , Then for 16 Bit character

chName

b

BOOL

Boolean value

bEnable

n ,i

int

integer ( Its size depends on the operating system )

nLength

si

short int   Short siSequ

n

UINT

Unsigned value ( Its size depends on the operating system )

nHeight

w

WORD

16 Bit unsigned value

wPos

f

float   floating-point fRadius

d

double   Double precision type    dArea

l

LONG

Long integer

lOffset

ld

long double Long double precision   ldRate

dw

DWORD

32 Bit unsigned integer

dwRange

p

*

The pointer

pDoc

lp

FAR*

Far pointer

lpszName

lpsz

LPSTR

32 Bit string pointer

lpszName

lpsz

LPCSTR

32 Bit constant string pointer

lpszName

lpsz

LPCTSTR

If _UNICODE Definition , Then for 32 Bit constant string pointer

lpszName

h

handle

Windows Object handle

hWnd

lpfn

callback

Point to CALLBACK Far pointer to function

?

if

Input file stream   ifDataFile

of

Output file stream   ofStuFile

sz

With "\0" Ending string szAppName


 

Recommended composition       Variable names should use nouns or adjectives + Noun . for example :“nCode”,“m_nState”,“nMaxWidth”

## Constant

Constants are named with a type prefix + All capital letters , Underline between words , Such as :cMAX_BUFFER. The definition of type prefix is the same as that of variable naming rules .

## enumeration 、 union 、typedef

enumeration 、 Joint and typedef Statement generation is tiring. Your name consists of all capital letters , Underline between words , Such as :FAR_PROC

## macro 、 Enumerated values  

Macro and enumeration values consist of all uppercase letters , Words are defined by underlining , Such as :ERROR_UNKNOWN,OP_STOP...

## Property  

g_                  Global variable/function  

c_                  Const variable

C                   class  

m_                  Member of struct/class  

s_                  Static variable  

S                   Struct  

## Componnet

Button              btn

Label               lbl

TextBox             txt

## varibal  

pointer             p  

char*

2. Underline naming rules

stay Linux The operating system is used C In the code written in language , Continue to use the underline coding specification according to the previous coding style . The difference between this coding standard and hump method lies in the semantic segmentation of words that form meaning between words , Underline use “_” Distinguish . In other variables, it is convenient to define and distinguish rules , There is no difference between the two .

原网站

版权声明
本文为[Life needs depth]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061105515821.html