代码规范

代码规范为了写出更好看的代码

1. 空格

#include <stdio.h>
if (x >= 1) {
    return true;
}

2. 缩进

建议使用四个空格去代替 Tab键

因为不同的编辑器的tab设置的值可能不同,比如Vim的Tab为8个
这个感觉还是看个人习惯因为现在的主流就是tab缩进为4


3. 大括号

推荐这种代码风格的大括号写法,比较赏心悦目和节省时间
#include <stdio.h>
if (x >= 1) {
    return true;
}

这种在IDE设置虚线对齐时最能显示出代码的结构
#include <stdio.h>
if (x >= 1) 
{
.   if (x <= 2)
.   { 
.   .   
.   .
.   .
.   .     return true;
.   }
.    
.
}

4. 变量名、函数名

常见变量命名:

array 数组 -> arr;
string 字符串 -> str/s;
    

temporary -> temp 暂时的、临时的 -> tmp;
flag 标志 -> flg;
statistic 统计 -> stat;
count 计数 -> cnt;
increment 自增数 -> inc;
decrement 自减 -> dec;
message 消息 -> msg;
value 值 -> val;
position 位置 -> pos;

加减乘除:
加 addition -> add;
减 subtraction -> sub;
乘 multiplication -> mul;
除 divide -> mod;
取余数 modulues -> mod;

最大值 -> max;
最小值 -> min;

函数命名:

camle case驼峰样式
UNIX 系统规定:find_index()//找出下标函数

注释:

/**
 * 查找最大值的位置.
 * @param arr 一个数组的长度
 * @param n   数组的长度
 * @return    最大值的下标
 */
int find_max_pos(int arr[],int n) {
    
}
posted @ 2020-03-30 17:27  DengSchoo  阅读(116)  评论(0编辑  收藏  举报