点击查看Evernote原文

#@author:       gr
#@date: 2014-08-15
#@email: forgerui@gmail.com

记录一些标准规范。让自己的编码更可读,更可维护。(代码风格尽量统一)

一、Contents

1. tab or spaces

尽量使用spaces取代tabtab键可能在不同编辑器缩进不同,导致混乱。把tab使用4个space代替。

2. 代码换行

如果一行超过80个字符,则需要进行换行。第二行应以第一行同一级的位置开始,如果这样太靠右,可以缩进8个空格。换行的地方可以在,(逗号)后面,或者操作符前面,保证不会因为换行导致歧义。

3. 在for, if, while等后面需要空一格

空一格会保证代码更易读,更优雅。

for(int i; i < 10; ++i){}
for (int i; i < 10; ++i){}
while(i < 10){}
while (i < 10){}

4. 单行注释上方尽量空一行

//快速排序
quick_sort(a);
//归并排序
merge_sort(a);

更好的写法是,下面的写法在代码多的时候更优秀。

//快速排序
quick_sort(a); //归并排序
merge_sort(b);

5. 操作符两边空格

操作符两边都要空格,逗号后面要空格,最基本的编程素养,重要性就不说了。注意要手敲这些空格,强烈不推荐使用代码格式化工具去做这些工作,如果以后在文本编辑器中,没有IDE,那写的代码简直是一坨。

int a=1,b=3,c=4;
int a = 1, b = 3, c = 4;
if(a>b);
if (a > b);

6. 命名

  • 变量命名尽量统一,比如统一使用小写字母加下划线的格式。

      //可以
    int node_flag;
    int nodeFlag;
    //不可以
    int Node_Flag;
  • 类型命名使用大小写交替的形式,类型包括class, struct, enum ...

    class TrackRouter{};

    struct SensorNode{};

    enum SensorStatus{RUN, STOP, DESTROY};

  • 函数使用大小写字母交替表示,第一个单词首字母小写,之后每个单词首字母大写。

      void addTable();
    inline int get_status();
  • 文件命名以小写下划线组合。

  • 宏定义使用大写字母下划线。

  • 命名空间小写下划线。

7. 头文件卫士

在头文件中尽量使用头文件卫士。防止头文件被重复引用。

#ifndef _A_H_
#define _A_H_
...
#endif//_A_H_

8. 循环

循环次数多的放在里面,这样可以减少CPU跨切循环层次数。

for (int a = 0; a < 5; a++){
for (int b = 0; b < 100; b++){
//...
}
}

尽量减少循环里的判断。

for (int i = 0; i < 5; i++){
//尽量移到循环外面
if(){}
else{}
}

###Maintainable C++的更多相关文章

  1. Building Maintainable Software-java篇之Separate Concerns in Modules

    Building Maintainable Software-java篇之Separate Concerns in Modules   In a system that is both complex ...

  2. JavaScript Patterns 2.1 Writing Maintainable Code

    Revisiting the code after some time has passed requires: • Time to relearn and understand the proble ...

  3. Building Maintainable Software-java篇之Couple Architecture Components Loosely

    Building Maintainable Software-java篇之Couple Architecture Components Loosely There are two ways of co ...

  4. Practical Go: Real world advice for writing maintainable Go programs

    转自:https://dave.cheney.net/practical-go/presentations/qcon-china.html?from=timeline   1. Guiding pri ...

  5. [转][C++ 11]override and final - write clean and maintainable C++ code

    原文: http://arne-mertz.de/2015/12/modern-c-features-override-and-final/ Today I write about a pair of ...

  6. Maintainable HashCode and Equals Using Apache Commons

    Java hashCode and equals methods can be tricky to implement correctly. Fortunately, all majors IDEs ...

  7. Maintainable JavaScript(编写可维护的JavaScript) PART I Style Guidelines

    “Programs are meant to be read by humans and only incidentally( 顺便:偶然地:附带地) for computers to execute ...

  8. JavaScript Maintainable

    1. Avoid conflict with Native Variable namespace

  9. REST简介

    一说到REST,我想大家的第一反应就是“啊,就是那种前后台通信方式.”但是在要求详细讲述它所提出的各个约束,以及如何开始搭建REST服务时,却很少有人能够清晰地说出它到底是什么,需要遵守什么样的准则. ...

随机推荐

  1. quartus II 自动生成testbench

    如果自己不想写这些testbench的这些固定格式,可以在quartus里自动生成testbench文件的模板,然后往里面写信号就行了 步骤:processing->start->star ...

  2. [Objective-c 基础 - 2.2] OC弱语法、类方法

    A.OC弱语法 1.在运行的时候才会检查方法的声明和实现 2.没有声明只有实现的方法也能正常运行,只要在调用之前定义即可 3.类的声明必须存在,否则会出现运行时错误   B.类方法 1.是类名调用的方 ...

  3. lazyload 图片延迟加载

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. Android Handler的简单使用

    大家好我们这一节讲的是Android Handler的使用,在讲Handler之前,我们先提个小问题,就是如何让程序5秒钟更新一下Title. 首先我们看一下习惯了Java编程的人,在不知道Handl ...

  5. Oracle 中 union与union all

    如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字. union(或称为联合)的作用是将多个结果合并在一起显示出来. union和uni ...

  6. Codeforces Round #200 (Div. 1) B. Alternating Current 栈

    B. Alternating Current Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/343 ...

  7. php关于日期时间 php日期 php时间

    strtotime 的牛逼用法: $a='-4 days '.date('Y-m-d');$day = date('Y-m-d', strtotime($a));var_dump($day); /** ...

  8. iOS7 设置隐藏状态栏(status bar)

    在info.plist 添加 UIViewControllerBasedStatusBarAppearance(View controller-based status bar appearance) ...

  9. Dashboards (Android)

    his page provides information about the relative number of devices that share a certain characterist ...

  10. 深入研究Block捕获外部变量和__block实现原理

    Blocks是C语言的扩充功能,而Apple 在OS X Snow Leopard 和 iOS 4中引入了这个新功能“Blocks”.从那开始,Block就出现在iOS和Mac系统各个API中,并被大 ...