当前位置:网站首页>变量的命名规则十二条
变量的命名规则十二条
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 行政楼
边栏推荐
- leetcode1020. 飞地的数量(中等)
- [some special grammars about C]
- 同事上了个厕所,我帮产品妹子轻松完成BI数据产品顺便得到奶茶奖励
- Visitor tweets about how you can layout the metauniverse
- 升级版手机检测微信工具小程序源码-支持多种流量主模式
- Simple use of MySQL database: add, delete, modify and query
- What is the difference between int (1) and int (10)? Senior developers can't tell!
- 前缀和数组系列
- The first Baidu push plug-in of dream weaving fully automatic collection Optimization SEO collection module
- 《从0到1:CTFer成长之路》书籍配套题目(周更)
猜你喜欢

Every API has its foundation when a building rises from the ground

Basic commands of MySQL
![[server data recovery] case of offline data recovery of two hard disks of IBM server RAID5](/img/c3/7a147151b7338cf38ffbea24e8bafd.jpg)
[server data recovery] case of offline data recovery of two hard disks of IBM server RAID5

Proteus -- Serial Communication parity flag mode

19.段页结合的实际内存管理

Is it difficult for girls to learn software testing? The threshold for entry is low, and learning is relatively simple

Wechat brain competition answer applet_ Support the flow main belt with the latest question bank file

A brief introduction of reverseme in misc in the world of attack and defense

Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL

1189. Maximum number of "balloons"
随机推荐
Fedora/rehl installation semanage
这个高颜值的开源第三方网易云音乐播放器你值得拥有
SEO学习的最好方式:搜索引擎
18. Multi level page table and fast table
Latex文字加颜色的三种办法
Wechat official account infinite callback authorization system source code, launched in the whole network
26岁从财务转行软件测试,4年沉淀我已经是25k的测开工程师...
leetcode6109. 知道秘密的人数(中等,周赛)
win10 64位装三菱PLC软件出现oleaut32.dll拒绝访问
“无聊猿” BAYC 的内忧与外患
【Hot100】739. Daily temperature
中青看点阅读新闻
Cif10 actual combat (resnet18)
BUU的MISC(不定时更新)
Practical guidance for interface automation testing (Part I): what preparations should be made for interface automation
ROS学习_基础
[hot100] 739. Température quotidienne
The difference between get and post request types
软件测试外包到底要不要去?三年真实外包感受告诉你
Pymongo gets a list of data
https://www.bilibili.com/video/BV1XY411J7aG?p=8&vd_source=8e3e569f698756c0f7529e30f2af56ee