###Maintainable C++
点击查看Evernote原文。
#@author: gr
#@date: 2014-08-15
#@email: forgerui@gmail.com
记录一些标准规范。让自己的编码更可读,更可维护。(代码风格尽量统一)
一、Contents
1. tab or spaces
尽量使用spaces取代tab,tab键可能在不同编辑器缩进不同,导致混乱。把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++的更多相关文章
- Building Maintainable Software-java篇之Separate Concerns in Modules
Building Maintainable Software-java篇之Separate Concerns in Modules In a system that is both complex ...
- JavaScript Patterns 2.1 Writing Maintainable Code
Revisiting the code after some time has passed requires: • Time to relearn and understand the proble ...
- Building Maintainable Software-java篇之Couple Architecture Components Loosely
Building Maintainable Software-java篇之Couple Architecture Components Loosely There are two ways of co ...
- 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 ...
- [转][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 ...
- Maintainable HashCode and Equals Using Apache Commons
Java hashCode and equals methods can be tricky to implement correctly. Fortunately, all majors IDEs ...
- Maintainable JavaScript(编写可维护的JavaScript) PART I Style Guidelines
“Programs are meant to be read by humans and only incidentally( 顺便:偶然地:附带地) for computers to execute ...
- JavaScript Maintainable
1. Avoid conflict with Native Variable namespace
- REST简介
一说到REST,我想大家的第一反应就是“啊,就是那种前后台通信方式.”但是在要求详细讲述它所提出的各个约束,以及如何开始搭建REST服务时,却很少有人能够清晰地说出它到底是什么,需要遵守什么样的准则. ...
随机推荐
- A Tour of Go Map literals continued
If the top-level type is just a type name, you can omit it from the elements of the literal. package ...
- soliworks三维机柜布局(四)进入solidworks中三维布线
首先需要在solidworks electrical中创建solidworks装配体文件. 菜单栏:处理--solidworks机柜布局--勾选要创建的装配体--点击确定 在文件列表下右键装配体文件- ...
- hdoj 2122 Ice_cream’s world III
并查集+最小生成树 Ice_cream’s world III Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- fastica matlab 转载
FastICA工具箱1 http://chunqiu.blog.ustc.edu.cn/?p=68#comment-3512 FastICA代码网址如下:http://research.ics.aal ...
- cocos2d-x 动画特效集合
转自:http://www.cnblogs.com/linux-ios/archive/2013/04/09/3009292.html 备用 bool HelloWorld::init() { /// ...
- dotnetfx35.exe
http://download.microsoft.com/download/2/0/e/20e90413-712f-438c-988e-fdaa79a8ac3d/dotnetfx35.exe
- nape.geom.MarchingSquares
Nape中的MarchingSquares类很简单,只有一个静态函数run,不过这对绘制那些简单的形状来说,已经足够了(当然MarchingSquares能做的不只这些).下面是这个run方法的结构: ...
- iOS开发笔记系列-基础2(类)
面向对象编程总是离不开类和对象的,Objective-C也不例外,不过Objective-C中的类还有一些自己的独特点. 类的声明和定义 在iOS开发中,类的声明与定义通常都是分开的,类得声明通常存放 ...
- XMLHTTP使用具体解释
XMLHTTP对象是Microsoft的MSXML开发包中带的一个用HTTP,XML协议訪问web资源的对象. 从MSXML3.0開始出现. 它在AJAX技术中主要用来从其它网络资源获取信息,然后由j ...
- CustomViewWith_Image_Text_Video
CustomViewOfTextVideoImage.rar https://github.com/Grishu/CustomViewWith_Image_Text_Video