Compare commits
1 Commits
8e7550bfee
...
abdulla
| Author | SHA1 | Date | |
|---|---|---|---|
| 39fc9c4c7e |
267
Qt码规范.md
267
Qt码规范.md
@ -1,267 +0,0 @@
|
||||
# 开发团队C++编码规范
|
||||
|
||||
- [开发团队C++编码规范](#开发团队c编码规范)
|
||||
- [代码风格](#代码风格)
|
||||
- [缩进](#缩进)
|
||||
- [变量声明](#变量声明)
|
||||
- [空白](#空白)
|
||||
- [大括号](#大括号)
|
||||
- [圆括号](#圆括号)
|
||||
- [Switch 语句](#switch-语句)
|
||||
- [断行](#断行)
|
||||
- [继承与关键字 `virtual`](#继承与关键字-virtual)
|
||||
- [通用例外](#通用例外)
|
||||
- [附录A 参考列表](#附录a-参考列表)
|
||||
|
||||
## 代码风格
|
||||
|
||||
### 缩进
|
||||
|
||||
- 采用4个空格
|
||||
- 空格,不要用TAB!
|
||||
|
||||
### 变量声明
|
||||
|
||||
- 每行一个变量
|
||||
- 尽可能避免短的变量名(比如"a", "rbarr", "nughdeget")
|
||||
- 单字符的变量只在临时变量或循环的计数中使用
|
||||
- 等到真正需要使用时再定义变量
|
||||
|
||||
```cpp
|
||||
// 错误示例
|
||||
int a, b;
|
||||
char *c, *d;
|
||||
|
||||
// 正确
|
||||
int height;
|
||||
int width;
|
||||
char *nameOfThis;
|
||||
char *nameOfThat;
|
||||
```
|
||||
|
||||
- 以小写字符开头,后续单词以大写开头
|
||||
- 避免使用缩写
|
||||
|
||||
```cpp
|
||||
// 错误示例
|
||||
short Cntr;
|
||||
char ITEM_DELIM = '';
|
||||
|
||||
// 正确
|
||||
short counter;
|
||||
char itemDelimiter = '';
|
||||
```
|
||||
|
||||
- 类名总是以大写开头。公有类以Q开头(QRgb),公有函数通常以q开头(qRgb)。
|
||||
|
||||
### 空白
|
||||
|
||||
- 利用空行将语句恰当地分组
|
||||
- 总是使用一个空行(不要空多行)
|
||||
- 总是在每个关键字和大括号前使用一个空格
|
||||
|
||||
```cpp
|
||||
// 错误示例
|
||||
if(foo){
|
||||
}
|
||||
|
||||
// 正确
|
||||
if (foo) {
|
||||
}
|
||||
```
|
||||
|
||||
- 对指针和引用,在类型和*、&之间加一个空格,但在*、&与变量之间不加空格
|
||||
|
||||
```cpp
|
||||
char *x;
|
||||
const QString &myString;
|
||||
const char* const y = "hello";
|
||||
```
|
||||
|
||||
- 二元操作符前后加空白
|
||||
- 类型转换后不加空白
|
||||
- 尽量避免C风格的类型转换
|
||||
|
||||
```cpp
|
||||
// 错误示例
|
||||
char* blockOfMemory = (char* ) malloc(data.size());
|
||||
|
||||
// 正确
|
||||
char *blockOfMemory = reinterpret_cast<char*>(malloc(data.size()));
|
||||
```
|
||||
|
||||
### 大括号
|
||||
|
||||
- 基本原则:左大括号和语句保持在同一行:
|
||||
|
||||
```cpp
|
||||
// 错误示例
|
||||
if (codec)
|
||||
{
|
||||
}
|
||||
|
||||
// 正确
|
||||
if (codec) {
|
||||
}
|
||||
```
|
||||
|
||||
- 例外:函数定义和类定义中,左大括号总是单独占一行:
|
||||
|
||||
```cpp
|
||||
static void foo(int g)
|
||||
{
|
||||
qDebug("foo: %i", g);
|
||||
}
|
||||
class Moo
|
||||
{
|
||||
};
|
||||
```
|
||||
|
||||
- 控制语句的body中只有一行时不使用大括号
|
||||
|
||||
```cpp
|
||||
// 错误示例
|
||||
if (address.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 10; +''i) {
|
||||
qDebug("%i", i);
|
||||
}
|
||||
|
||||
// 正确
|
||||
if (address.isEmpty())
|
||||
return false;
|
||||
|
||||
for (int i = 0; i < 10;i)
|
||||
qDebug("%i", i);
|
||||
```
|
||||
|
||||
- 例外1:如果父语句跨多行,则使用大括号
|
||||
|
||||
```cpp
|
||||
// 正确
|
||||
if (address.isEmpty() || !isValid()
|
||||
|| !codec) {
|
||||
return false;
|
||||
}
|
||||
```
|
||||
|
||||
- 例外2:在if-else结构中,有一处跨多行,则使用大括号
|
||||
|
||||
```cpp
|
||||
// 错误示例
|
||||
if (address.isEmpty())
|
||||
return false;
|
||||
else {
|
||||
qDebug("%s", qPrintable(address));
|
||||
it;
|
||||
}
|
||||
|
||||
// 正确
|
||||
if (address.isEmpty()) {
|
||||
return false;
|
||||
} else {
|
||||
qDebug("%s", qPrintable(address));
|
||||
it;
|
||||
}
|
||||
|
||||
// 错误示例
|
||||
if (a)
|
||||
if (b)
|
||||
…
|
||||
else
|
||||
…
|
||||
|
||||
// 正确
|
||||
if (a) {
|
||||
if (b)
|
||||
…
|
||||
else
|
||||
…
|
||||
}
|
||||
```
|
||||
|
||||
- 如果控制语句的body为空,则使用大括号
|
||||
|
||||
```cpp
|
||||
// 错误示例
|
||||
while (a);
|
||||
|
||||
// 正确
|
||||
while (a) {}
|
||||
```
|
||||
|
||||
### 圆括号
|
||||
|
||||
- 使用圆括号将表达式分组
|
||||
|
||||
```cpp
|
||||
// 错误示例
|
||||
if (a && b || c)
|
||||
|
||||
// 正确
|
||||
if ((a && b) || c)
|
||||
|
||||
// 错误示例
|
||||
a | b & c
|
||||
|
||||
// 正确
|
||||
(a + b) & c
|
||||
```
|
||||
|
||||
### Switch 语句
|
||||
|
||||
- case 和 switch 位于同一列
|
||||
- 每一个case必须有一个break(或renturn)语句,或者用注释说明无需break
|
||||
|
||||
```cpp
|
||||
switch (myEnum) {
|
||||
case Value1:
|
||||
doSomething();
|
||||
break;
|
||||
case Value2:
|
||||
doSomethingElse();
|
||||
// fall through
|
||||
default:
|
||||
defaultHandling();
|
||||
break;
|
||||
}
|
||||
```
|
||||
|
||||
### 断行
|
||||
|
||||
- 保持每行短于100 个字符,需要时进行断行
|
||||
- 逗号放一行的结束,操作符放到一行的开头。如果你的编辑器太窄,一个放在行尾的操作符不容易被看到。
|
||||
|
||||
```cpp
|
||||
// 正确
|
||||
if (longExpression
|
||||
+ otherLongExpression
|
||||
+ otherOtherLongExpression) {
|
||||
}
|
||||
|
||||
// Wrong
|
||||
if (longExpression +
|
||||
otherLongExpression +
|
||||
otherOtherLongExpression) {
|
||||
}
|
||||
```
|
||||
|
||||
### 继承与关键字 `virtual`
|
||||
|
||||
- 重新实现一个虚函数时,头文件中 不 放置 virtual 关键字。
|
||||
|
||||
### 通用例外
|
||||
|
||||
如果它使你的代码看起来不好,你可以打破任何一个规则 。
|
||||
|
||||
## 附录A 参考列表
|
||||
|
||||
1. [Qt编码风格/Qt Coding Style](https://wiki.qt.io/Qt_Coding_Style)
|
||||
2. [LLVM编码标准/LLVM Coding Standards](https://llvm.org/docs/CodingStandards.html)
|
||||
3. [谷歌C++风格指南/Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html)
|
||||
4. [C++核心指南/Cpp Core Guidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines)
|
||||
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
23
代码规范/README.md
Normal file
23
代码规范/README.md
Normal file
@ -0,0 +1,23 @@
|
||||
# [代码规范](./cppStyleGuide/StyleGuide.md)
|
||||
|
||||
引用模板
|
||||
|
||||
* [google cpp guide](https://google.github.io/styleguide/cppguide.html)
|
||||
* [C++ Core Guidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines)
|
||||
* Qt
|
||||
* osg
|
||||
* 源码文件编码(utf-8)
|
||||
|
||||
# [git仓库管理](./gitStore/git.md)
|
||||
* 仓库创建流程
|
||||
* git钩子
|
||||
|
||||
# 代码审查
|
||||
|
||||
* 代码规范检查
|
||||
* C/C++ 静态代码检查工具cppCheck
|
||||
|
||||
# CMake
|
||||
|
||||
* 快速搭建基础项目(持续更新)
|
||||
* 编译器配置(持续更新)
|
||||
61
代码规范/cppStyleGuide/StyleGuide.md
Normal file
61
代码规范/cppStyleGuide/StyleGuide.md
Normal file
@ -0,0 +1,61 @@
|
||||
# c++ 代码风格规范
|
||||
|
||||
## 源代码文件命名
|
||||
|
||||
##### 1. 前要
|
||||
|
||||
**通常情况下每个.cpp文件应该有一个配套的.h文件。当然也有例外的情况(例如:c++模板类的实现)。正确使用头文件会大大改善代码的可读性和执行文件的大小和性能.**
|
||||
|
||||
> 头文件应能独立编译,就是头文件的使用者和构建工具在导入文件时无需任何特殊的前提条件就能完成代码编译。具体来说,头文件要有头文件防护符,并导入它所需的所有其它头文件。
|
||||
> 当编写c++模板时应保证模板的实现声明和实现都在同一个文件里。一般,写模板时源码文件以.hpp命名。
|
||||
> 避免编写依赖某个构建工具的代码。(例如:项目的构建依赖于VSCode的一个插件,使用其他的构建工具,项目无法构建且生成)
|
||||
> 避免前向声明
|
||||
|
||||
##### 2. 文件命名
|
||||
|
||||
* **新建c++类[.h 或 .hpp]**
|
||||
|
||||
> 新建类文件时,尽量保证文件名与导出(外部调用)的类名保持一致(一个头文件中可能包含好几个类)。
|
||||
> Help.h
|
||||
|
||||
```cpp
|
||||
class Help{}
|
||||
```
|
||||
|
||||
Help.hpp
|
||||
|
||||
```cpp
|
||||
template<typename T>
|
||||
class Help{}
|
||||
```
|
||||
|
||||
##### 3. 防护符
|
||||
|
||||
* **要保证头文件带有防护重复引用预处理头**
|
||||
|
||||
```cpp
|
||||
#pragma once
|
||||
```
|
||||
|
||||
或
|
||||
|
||||
```cpp
|
||||
#ifndef HELP_H
|
||||
#define HELP_H
|
||||
|
||||
#endif
|
||||
```
|
||||
|
||||
## 命名规范
|
||||
|
||||
##### 1. 命名空间[namespace]
|
||||
##### 2. 类[class]
|
||||
##### 3. 结构体[struct]
|
||||
##### 4. 模板[template]
|
||||
##### 5. 私有函数或变量[private]
|
||||
##### 6. 共有函数或变量[public]
|
||||
##### 7. 受保护函数或变量[protected]
|
||||
##### 8. 预处理[#define]
|
||||
##### 9. 内联函数[inline]
|
||||
##### 10. 常量[const]
|
||||
##### 11. 静态变量或函数[static]
|
||||
1
代码规范/gitStore/git.md
Normal file
1
代码规范/gitStore/git.md
Normal file
@ -0,0 +1 @@
|
||||
|
||||
281
编码规范.md
281
编码规范.md
@ -1,281 +0,0 @@
|
||||
# 开发团队C++编码规范
|
||||
|
||||
- [开发团队C++编码规范](#开发团队c编码规范)
|
||||
- [代码风格](#代码风格)
|
||||
- [缩进](#缩进)
|
||||
- [变量声明](#变量声明)
|
||||
- [空白](#空白)
|
||||
- [大括号](#大括号)
|
||||
- [圆括号](#圆括号)
|
||||
- [Switch 语句](#switch-语句)
|
||||
- [断行](#断行)
|
||||
- [继承与关键字 `virtual`](#继承与关键字-virtual)
|
||||
- [通用例外](#通用例外)
|
||||
- [附录A 参考列表](#附录a-参考列表)
|
||||
|
||||
## 代码风格
|
||||
|
||||
### 缩进
|
||||
|
||||
- 采用4个空格进行缩进
|
||||
- 使用空格,不要用TAB!
|
||||
|
||||
### 变量声明
|
||||
|
||||
- 每行一个变量
|
||||
- 尽可能避免短的变量名(比如"a", "rbarr", "nughdeget")
|
||||
- 单字符的变量只在临时变量或循环的计数中使用
|
||||
- 等到真正需要使用时再定义变量
|
||||
|
||||
```cpp
|
||||
// 错误示例
|
||||
int a, b;
|
||||
char *c, *d;
|
||||
|
||||
// 正确
|
||||
int height;
|
||||
int width;
|
||||
char *nameOfThis;
|
||||
char *nameOfThat;
|
||||
```
|
||||
|
||||
- 首字母小写,后续单词以大写开头(驼峰式)
|
||||
- 避免使用缩写
|
||||
|
||||
```cpp
|
||||
// 错误示例
|
||||
short Cntr;
|
||||
char ITEM_DELIM = '';
|
||||
|
||||
// 正确
|
||||
short counter;
|
||||
char itemDelimiter = '';
|
||||
```
|
||||
|
||||
- 类名总是以大写开头。
|
||||
|
||||
### 空白
|
||||
|
||||
- 利用空行将语句恰当地分组
|
||||
- 总是使用一个空行(不要空多行)
|
||||
- 总是在每个关键字和大括号前使用一个空格
|
||||
|
||||
```cpp
|
||||
// 错误示例
|
||||
if(foo)
|
||||
{
|
||||
}
|
||||
|
||||
// 正确
|
||||
if (foo)
|
||||
{
|
||||
}
|
||||
```
|
||||
|
||||
- 对指针和引用,在类型和*、&之间加一个空格,但在*、&与变量之间不加空格
|
||||
|
||||
```cpp
|
||||
char *x;
|
||||
const QString &myString;
|
||||
const char* const y = "hello";
|
||||
```
|
||||
|
||||
- 二元操作符前后加空白
|
||||
- 类型转换后不加空白
|
||||
- 尽量避免C风格的类型转换
|
||||
|
||||
```cpp
|
||||
// 错误示例
|
||||
char* blockOfMemory = (char* ) malloc(data.size());
|
||||
|
||||
// 正确
|
||||
char *blockOfMemory = reinterpret_cast<char*>(malloc(data.size()));
|
||||
```
|
||||
|
||||
### 大括号
|
||||
|
||||
- 基本原则:左大括号和语句之间换行,左大括号总是单独占一行:
|
||||
|
||||
```cpp
|
||||
// 错误示例
|
||||
if (codec){
|
||||
}
|
||||
|
||||
// 正确
|
||||
if (codec)
|
||||
{
|
||||
}
|
||||
```
|
||||
|
||||
- 例外1:定义命名空间时,左大括号与命名空间同行:
|
||||
|
||||
```cpp
|
||||
namespace Test {
|
||||
qDebug("foo: %i", g);
|
||||
}
|
||||
```
|
||||
|
||||
- 例外2:控制语句的body为空时,大括号与控制语句同行:
|
||||
|
||||
```cpp
|
||||
while(a) {}
|
||||
```
|
||||
|
||||
- 控制语句的body中只有一行时不使用大括号
|
||||
|
||||
```cpp
|
||||
// 错误示例
|
||||
if (address.isEmpty())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 10; +''i)
|
||||
{
|
||||
qDebug("%i", i);
|
||||
}
|
||||
|
||||
// 正确
|
||||
if (address.isEmpty())
|
||||
return false;
|
||||
|
||||
for (int i = 0; i < 10;i)
|
||||
qDebug("%i", i);
|
||||
```
|
||||
|
||||
- 例外1:如果父语句跨多行,则使用大括号
|
||||
|
||||
```cpp
|
||||
// 正确
|
||||
if (address.isEmpty() || !isValid()
|
||||
|| !codec)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
```
|
||||
|
||||
- 例外2:在if-else结构中,有一处跨多行,则使用大括号
|
||||
|
||||
```cpp
|
||||
// 错误示例
|
||||
if (address.isEmpty())
|
||||
return false;
|
||||
else
|
||||
{
|
||||
qDebug("%s", qPrintable(address));
|
||||
it;
|
||||
}
|
||||
|
||||
// 正确
|
||||
if (address.isEmpty())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
qDebug("%s", qPrintable(address));
|
||||
it;
|
||||
}
|
||||
|
||||
// 错误示例
|
||||
if (a)
|
||||
if (b)
|
||||
…
|
||||
else
|
||||
…
|
||||
|
||||
// 正确
|
||||
if (a)
|
||||
{
|
||||
if (b)
|
||||
…
|
||||
else
|
||||
…
|
||||
}
|
||||
```
|
||||
|
||||
- 如果控制语句的body为空,则使用大括号
|
||||
|
||||
```cpp
|
||||
// 错误示例
|
||||
while (a);
|
||||
|
||||
// 正确
|
||||
while (a) {}
|
||||
```
|
||||
|
||||
### 圆括号
|
||||
|
||||
- 使用圆括号将表达式分组
|
||||
|
||||
```cpp
|
||||
// 错误示例
|
||||
if (a && b || c)
|
||||
|
||||
// 正确
|
||||
if ((a && b) || c)
|
||||
|
||||
// 错误示例
|
||||
a | b & c
|
||||
|
||||
// 正确
|
||||
(a + b) & c
|
||||
```
|
||||
|
||||
### Switch 语句
|
||||
|
||||
- case 和 switch 位于同一列
|
||||
- 每一个case必须有一个break(或renturn)语句,或者用注释说明无需break
|
||||
|
||||
```cpp
|
||||
switch (myEnum)
|
||||
{
|
||||
case Value1:
|
||||
doSomething();
|
||||
break;
|
||||
case Value2:
|
||||
doSomethingElse();
|
||||
// fall through
|
||||
default:
|
||||
defaultHandling();
|
||||
break;
|
||||
}
|
||||
```
|
||||
|
||||
### 断行
|
||||
|
||||
- 保持每行短于100 个字符,需要时进行断行
|
||||
- 逗号放一行的结束,操作符放到一行的开头。如果你的编辑器太窄,一个放在行尾的操作符不容易被看到。
|
||||
|
||||
```cpp
|
||||
// 正确
|
||||
if (longExpression
|
||||
+ otherLongExpression
|
||||
+ otherOtherLongExpression)
|
||||
{
|
||||
...
|
||||
}
|
||||
|
||||
// Wrong
|
||||
if (longExpression +
|
||||
otherLongExpression +
|
||||
otherOtherLongExpression)
|
||||
{
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
### 继承与关键字 `virtual`
|
||||
|
||||
- 重新实现一个虚函数时,头文件中 不 放置 virtual 关键字。
|
||||
|
||||
### 通用例外
|
||||
|
||||
如果它使你的代码看起来不好,你可以打破任何一个规则 。
|
||||
|
||||
## 附录A 参考列表
|
||||
|
||||
1. [Qt编码风格/Qt Coding Style](https://wiki.qt.io/Qt_Coding_Style)
|
||||
2. [LLVM编码标准/LLVM Coding Standards](https://llvm.org/docs/CodingStandards.html)
|
||||
3. [谷歌C++风格指南/Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html)
|
||||
4. [C++核心指南/Cpp Core Guidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines)
|
||||
Reference in New Issue
Block a user