Several common program naming rules

Naming functions and variables correctly and vividly can not only increase the readability of the program, but also reflect the programmer's programming style. A good naming convention can effectively improve the maintainability of the program. The following introduces several commonly used variable naming rules.

Hungarian nomenclature

It is widely used in environments such as Microsoft Windows.

This naming technique was proposed by a capable Microsoft programmer Charles Simonyi (Charles Simonyi). Hungarian nomenclature identifies the scope, type, etc. of the variable by prefixing the variable name with the corresponding symbol in lowercase letters. These symbols can be used at the same time, the order is m_ (member variable), then pointer, then simple data type, and then others. For example: m_lpszStr, means a long pointer member variable that points to a character string ending with 0 characters.

The key to the Hungarian nomenclature is: the name of the identifier starts with one or more lowercase letters as a prefix; after the prefix is ​​a word or a combination of words with the first letter capitalized, and the word must indicate the purpose of the variable.

For example: bEnable, nLength, hWnd.

The prefixes of lowercase letters commonly used in Hungarian nomenclature:

前缀	类型	描述
a	Array	数组
b	BOOL	布尔
by	BYTE	无符号字符
c	char	字符
cb	Count of bytes	字节数
cr	Color reference value	颜色值
cx,cy	Count of x,y(short)	长度
dw	DWORD	双字(无符号长整形)
f	Flags	标志
fn	Function	函数
g_	Global	全局的
h	HANDLE	句柄
i	Integer(int)	整数
l	Long(long)	长整数
lp	Long point	长指针
m_	Data member of a class	类的数据成员
n	Short(short)	短整型
np	Near point	短指针
p	Point	指针
s	String	字符串
sz	Zero terminated string	以0结尾的字符串
tm	Text metric	文本规则
u	Unsigned int	无符号整数
ul	Unsigned long(ULONG)	无符号长整数
w	WORD	无符号短整数
x,y	x,y coordinates(short)	坐标
v	Void	空

The global variables related to the project start with g_, and the class member variables use m_.

前缀	类型	例子
C	类	CDocument, CPrintInfo
m_	成员变量	m_pDoc, m_nCustomers
g_	全局变量	g_Servers

Hump ​​nomenclature

It has become more and more popular in recent years.

CamelCase nomenclature, as its name implies, refers to a name that uses a mixture of upper and lower case letters to form an identifier. The first letter of the first word is lowercase, and the first letter of the remaining words is uppercase.

E.g:

printEmployeePaychecks();

Each logical breakpoint in the function name is marked with a capital letter.

Pascal nomenclature

Similar to the hump nomenclature.

It’s just that the CamelCase nomenclature is lowercase the first letter of the first word, while Pascal’s nomenclature is the first letter of the first word. Therefore, this nomenclature is also called the " big hump nomenclature ".

E.g:
DisplayInfo();
UserName

Both use the Pascal nomenclature. In C#, Pascal nomenclature and camel nomenclature are mostly used. In fact, many programmers use CamelCase nomenclature in combination with Pascal when actually naming them. For example, variable names use CamelCase nomenclature, while functions use Pascal nomenclature.

Underscore nomenclature

The underscore method became popular with the emergence of the C language, and it is very common in environments such as UNIX/LIUNX and in GNU code.

Function naming

Function names are named using underscores to separate lowercase letters: device name_operation name();

The operation name generally adopts: predicate (the device name is used as the object or the module to which the operation belongs) or predicate + object/predicative (the device name is used as the subject or the module to which the operation belongs), etc., such as:
tic_init();
adc_is_busy();
uart_tx_char();

The interrupt function is named directly in the form of device name _isr(), such as:
timer2_isr();

Variable naming

Variables are also named using underscores to separate lowercase letters. The naming should be accurate, not cause ambiguity, and be of moderate length. Such as:
int length;
uint32 test_offset;

Single-character names are also commonly used, such as i, j, k, etc. They are usually used as local variables in functions. tmp is often used as a temporary variable name.
Local static variables should be added with s_ prefix (for static), such as:
static int s_lastw;

Global variables (especially global variables for external access) should be added with the g_ prefix (for global), such as:
void (* g_capture_hook)(void);

Constants and macro naming

Use the underscore to separate capital letters for naming. Generally, the device name should be used as a prefix to
prevent duplication of naming between modules. Such as:
#define TIMER0_MODE_RELOAD 2
#define TIMER2_COUNT_RETRIEVE(val) ((uint16)(65536 - (val)))

Of course, macros regarded as interfaces can be named according to the naming method of functions, for example:
#define timer2_clear() (TF2 = 0)
#define timer0_is_expired() (TF0)

to sum up

According to investigations, there is no naming rule that can be approved by all programmers, and programming textbooks generally do not specify naming rules. Naming rules are not a matter of success or failure for software products. We should not devote too much energy to trying to invent the best naming rules in the world. Instead, we should develop a naming rule that satisfies most project members. Implementation in the project.

Guess you like

Origin blog.csdn.net/xp178171640/article/details/106662069