点击查看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. python 使用模块

    Python本身就内置了很多非常有用的模块,只要安装完毕,这些模块就可以立刻使用. 我们以内建的sys模块为例,编写一个hello的模块: #!/usr/bin/env python # -*- co ...

  2. Qt on_pushButton_clicked()的用法【worldsing笔记】

    在Qt里按钮控件默认对应一个on_pushButton_clicked()成员,如果想用点击信号,在代码中实现on_pushButton_clicked()成员即可. 最近看了一段代码,里面并没有co ...

  3. Windbg分析DMP文件

    1.提取Dump格式文件 有两种方式: 第一种,程序崩溃时,启动任务管理器,选择崩溃的*.exe进程,右键选择创建转储文件,通过 开始—运行—输入 %temp% --确定--在打开Temp窗口中即可找 ...

  4. Oracle Data Guard PING[ARC2]: Heartbeat failed to connect to standby ''. Error is 12514 故障分析

    朋友搭建的一套DG,折腾了很长时间,一直都是报如下错误: ORA-12514: TNS:listener does not currentlyknow of service requested in ...

  5. C/C++中的变量作用域

    #include <iostream> using namespace std; int i = 1;int j = 2; int main(){     int i = 9;  //C/ ...

  6. mysql---where子查询、form子查询、exists子查询

    1.什么是子查询? 当一个查询是另一个查询的条件时,称之为子查询. 2.子查询有什么好处? 子查询可以使用几个简单命令构造功能强大的复合命令. 那么,现在让我们一起来学习子查询. 3.where型的子 ...

  7. CF 319B Psychos in a Line 【单调队列】

    维护一个单调下降的队列. 对于每一个人,只需要找到在他前面且离他最近的可以杀掉他的人即可. #include <cstdio> #include <vector> #inclu ...

  8. Hdu 5444 Elven Postman dfs

    Elven Postman Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...

  9. [React Native] Using the WebView component

    We can access web pages in our React Native application using the WebView component. We will connect ...

  10. Linux 基础 —— RPM

    http://liaoph.com/linux-rpm/ 这篇文章主要讲 RPM 软件包管理器的使用. 软件包的演变史 最早期时,软件包是一些可以运行的程序组成的集合,可能还要加上若干配置文件和动态库 ...