当前位置:网站首页>变量的命名规则十二条
变量的命名规则十二条
2022-07-06 07:04:00 【Ana_ing】
一般规则
规则一
直观可以拼读,望文得知意,便于记忆,采用英文单词或组合,不建议使用拼音,英文单词也不要太复杂,建议使用简写
规则二
变量长度符合“min_length && max_information”,比如Max_value替换maxvalueuntiloverflow,较长的单词可以去掉元音形成缩写
规则三
当标识符号由多个词组成时,每个词的第一个字母要大写,其余全部小写
例如 int CurrentVal; //看起来清楚易懂
规则四
尽量避免名字中出现数字编号,例如我之前命名很喜欢用value1,value2,value3等等,除非逻辑上的确需要,像驱动开发和引脚之类的,加编号更易使用
规则五
在对多个文件之间共同使用的全局变量或函数一定要加范围限定符号,(建议使用模块缩写作为范围限定符)
规则六
符号命名有两部分,规范标识符前缀(后缀)+含义标识,全局变量以g或g+模块缩写(大写)作前缀,后面跟第一个字母大写的变量名。C中全局变量是不稳定因素,要保持警惕,用前缀显式标识变量为全局,能起到提示作用。名称前加上模块的缩写,可以在多模块集成链接时,避免模块间全局变量重名。
局部变量中,循环计数/数组下标可用i,j,k,m依次表示。除此以外,其它变量加前缀表示变量类型,前缀没有统一规范,p代表指针,pf指向函数的指针,f单精度浮点,db双精度浮点,u,s分别代表无符号与有符号数,即unsigned与signed。这样可以从变量名看出其类型,并避免一些bug
模板:模块名缩写_作用域前缀|数据类型前缀|【指针前缀】|含义标识|数组/结构后缀
规则七
作用域前缀命名规则
标识符类型 | 作用域前缀 |
全局变量 | g |
文件局部变量 | n |
函数局部变量 | f |
局部作用域变量 | a |
全局函数 | g |
静态函数 | n |
规则八
数据类型前缀命名规则
前缀 | 后缀 | 数据类型 | 备注 |
bt | bit | ||
boolean | boolean | ||
char | char | ||
i | int | ||
s | shrot[int] | ||
l | long[int] | ||
u | unsigned[int] | ||
d | duoble | ||
f | float | ||
p | pointer | 指针前缀 | |
v | void | ||
st | enum | ||
st | struct | ||
st | union | ||
fp | function point | ||
_a | array of | ||
_st | typedef enum/struct/union |
规则九
变量含义标识符构成:目的词+动词(过去分词)+状语+目标
例如:DataDeletedFromSD
函数含义标识符构成:
动词(一般现在时)+目标词+状语+目标
对了,同时安利一波现在真正看的语法视频
规则十
所有宏定义,枚举常数,只读变量全部用大写字母命名,用下划线分割单词。
例如
const int MAX_LENGTH = 100;
#define FILE_PATH “/usr/tmp”
规则十一
定义变量时不要忘记了初始化。定义变量时编译器并不一定清空这块内存,它的值可能是无效的数据。
规则十二
考虑到习惯问题,局部变量中可采用通用的命名方式,仅限于n,i,j等作为循环变量使用。
2022年7月5日21:26:07 行政楼
边栏推荐
- 指尖上的 NFT|在 G2 上评价 Ambire,有机会获得限量版收藏品
- Simple use of JWT
- Arduino tutorial - Simon games
- Call, apply, bind rewrite, easy to understand with comments
- 【软件测试进阶第1步】自动化测试基础知识
- C language_ Double create, pre insert, post insert, traverse, delete
- pymongo获取一列数据
- 18. Multi level page table and fast table
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- Introduction to ros2 installation and basic knowledge
猜你喜欢
指尖上的 NFT|在 G2 上评价 Ambire,有机会获得限量版收藏品
Practical guidance for interface automation testing (Part I): what preparations should be made for interface automation
Kubernetes cluster builds ZABBIX monitoring platform
Configure raspberry pie access network
leetcode704. 二分查找(查找某个元素,简单,不同写法)
因高额网络费用,Arbitrum 奥德赛活动暂停,Nitro 发行迫在眉睫
Visitor tweets about how you can layout the metauniverse
Brief introduction to the curriculum differences of colleges and universities at different levels of machine human major -ros1/ros2-
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
A brief introduction of reverseme in misc in the world of attack and defense
随机推荐
Reflex WMS medium level series 3: display shipped replaceable groups
Raspberry pie serial port login and SSH login methods
UNIPRO Gantt chart "first experience": multi scene exploration behind attention to details
At the age of 26, I changed my career from finance to software testing. After four years of precipitation, I have been a 25K Test Development Engineer
GET 和 POST 请求类型的区别
RichView TRVStyle 模板样式的设置与使用
Interface automation test framework: pytest+allure+excel
1189. Maximum number of "balloons"
Embed UE4 program into QT interface display
漏了监控:Zabbix对Eureka instance状态监控
When my colleague went to the bathroom, I helped my product sister easily complete the BI data product and got a milk tea reward
What is the difference between int (1) and int (10)? Senior developers can't tell!
Briefly describe the differences between indexes, primary keys, unique indexes, and joint indexes in mysql, and how they affect the performance of the database (in terms of reading and writing)
Introduction to ros2 installation and basic knowledge
leetcode704. 二分查找(查找某个元素,简单,不同写法)
Cif10 actual combat (resnet18)
CDN acceleration and cracking anti-theft chain function
OpenGL ES 学习初识(1)
26岁从财务转行软件测试,4年沉淀我已经是25k的测开工程师...
Short video, more and more boring?