coding style 良好的编程风格

来源:互联网 发布:淘宝运营数据分析表格 编辑:程序博客网 时间:2024/05/22 02:12

Coding Style 良好的编程风格

目的:
1. Better readability         提高阅读性
2. Less bugs                   减少bugs的产生
3. Easier maintenance      易于维护
4. More productivity         高效的生产力


怎么来形成良好的编程风格

1. Indentation  使用缩进
   *使用tabs键 tab键为8个字符
   *如果缩进太深 说明代码有待修改

2  Braces  大括号
   *大括号开始于语句的结尾 另起一行结束
   *函数可以将大括号开始和结束单独放一行

3.Naming variables and functions 变量或者函数命名规则
   *描述清楚
   *言简意赅
   *格式别混乱 大小写
   *别以名字编写No encoding the type within the name
   *除非必要 才用全局变量
   *局部变量 短且到点 cmd_group_size

 4.Functions 函数
   *每个函数只处理一个任务
   *尽量短
   *如果超过10个局部变量 需要修改函数

 5. Comments 注释
   *必须有,但是必须正确 每个函数的功能,实现什么
   *在函数开头
   *解释 what 和why

 6.Data Structure requirement 数据结构需求
   *使用引用,/* if another thread can find your data structure,
                     and  you do not have a reference count on it ,
                      you almost certainly have a bug*/

 7.Unwritten rules
    *使用已经存在的代码 字符函数 字节顺序函数  链接表

 8. typedef is evil
    *隐藏真实变量类型
    *使程序员陷入困境  大结构在堆栈中  大的结构作为返回值传递
    *隐藏大结构的定义
    *作为函数指针
    *一般系统类型 u8, u16, u32, u64  dev_t  list_t
                例如:typedef struct {
                                   _u32 link;
                                   _u32 status;
                                   _u32 info;
                                   _u32 buffer;
                       } uhci_td_t, *puhci_td_t;

  9. No #ifdef in .c files 在.c文件中没有#ifdef
     *#ifdef 只在.h文件中
     *让编译器完成者工作

原创粉丝点击