Common Programming naming and programming specifications
1. common naming rules
The famous naming rules are first introduced in the Hungarian naming method,
This naming method was proposed by Microsoft programmer Charles Simonyi.
The main idea is "adding prefixes to variables and function names to improve people's understanding of programs ".
The key to the Hungarian naming method is that the identifier name starts with one or more lower-case letters as the prefix;
A prefix is a combination of uppercase words or multiple words. The word must specify the purpose of the variable.
For example, lpszStr indicates a long pointer (lp) variable pointing to a string (sz) ending with '\ 0.
Camel naming has become increasingly popular in recent years,
Many new function libraries and Java platforms are used properly.
The camel naming method, as its name indicates, refers to the name of an identifier composed of uppercase and lowercase letters.
The first and remaining letters are in upper case.
For example, in printEmployeePaychecks (), each logical breakpoint in the function name is marked with an uppercase letter.
The Pascal naming method is similar to the camel naming method.
However, the trigger name is the first letter in lowercase, while the Pascal naming convention is the first letter in upper case.
For example, DisplayInfo () and UserName both use the Pascal naming method.
In C #, Pascal naming and camel naming are mostly used.
In fact, many programmers use the camel naming method and Pascal in actual naming,
For example, the variable name uses the camel naming method, and the function uses the Pascal naming method.
Another popular naming rule is the underline naming method.
The underline method is popular with the emergence of C language. It is widely used in UNIX/LIUNX environments and GNU code.
The naming rules described in this chapter are mainly developed based on the underline naming method.
2. Function Name
Function names are named using underscores to separate lower-case letters:
Device name_operation name ()
Generally, the operation name is a predicate (in this case, the device name is used as the object or indicates the module to which the operation belongs) or
Predicate + object/table language (in this case, the device name is used as the subject or indicates the module to which the operation belongs), for example:
Tic_init ()
Adc_is_busy ()
Uart_tx_char ()
The name of the interrupt function is directly named by the device name_isr (), for example:
Timer2_isr ()
3 variable name
The name of a variable is also named using underscores (_) to separate lowercase letters.
The name must be accurate, unambiguous, and moderately long. For example:
Int length;
Uint32 test_offset;
Single-character names are also commonly used, such as I, j, and k. They can usually be used as local variables in functions.
Tmp is commonly used as a temporary variable name.
For local static variables, the s _ crown (static) should be added, for example:
Static int s_lastw;
For global variables (especially global variables for external access), g _ should be added to indicate global, such:
Void (* g_capture_hook) (void );
4. Constant and macro naming
Names are separated by underscores (_) and names are generally prefixed with the device name,
Prevents duplicate names between modules. For example:
# Define TIMER0_MODE_RELOAD 2
# Define TIMER2_COUNT_RETRIEVE (val) (uint16) (65536-(val )))
Of course, macros treated as interfaces can be named according to the function naming method, for example:
# Define timer2_clear () (TF2 = 0)
# Define timer0_is_expired () (TF0)
5 common acronyms
Abbreviations
Addition add
Answer ans
Array arr
Average avg
Buffer buf or buff
Capture cap or capt
Check chk
Count cnt
Column col
Control ctrl
Decode dec
Define def
Delete del
Destination dst or dest
Display disp
Division div
Encode enc
Environment env
Error err
Float flt
Frequency freq
Header hdr
Index idx
Image img
Increment inc
Initalize init
Iteration itr
Length len
Memory mem
Middle mid
Make mk
Message msg
Multiplication mul
Number num
Operand opnd
Optimization opt
Operator optr
Packet pkt
Positon pos
Previous pre or prev
Payload type pt
Pointer ptr
Return code rc
Record rcd
Receive recv
Result res
Return ret
Source src
Stack stk
String str
Subtraction sub
Table tab
Temporary tmp or temp
Total tot
Time stamp ts
Value val
6 conclusion
There is no naming rule that can be endorsed by all programmers. These naming rules have their own advantages and disadvantages.
There is no need to spend too much energy trying to invent the best naming rules,
Instead, a naming rule should be developed and implemented to satisfy most project members.
The consistency of identifier naming naturally reflects the elegance of the Code.
Of course, if your program uses third-party code, and these modules are verified to be correct.
Therefore, it is not necessary to modify the names of functions and variables in these finalized modules.