add coding style doc
Signed-off-by: szdytom <szdytom@qq.com>
This commit is contained in:
parent
836908765f
commit
f6be04300f
56
docs/coding-style.md
Normal file
56
docs/coding-style.md
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
## 适用于 C++ 几条代码风格指引
|
||||||
|
|
||||||
|
1. 不要在空行留下缩进字符
|
||||||
|
|
||||||
|
2. 不要在嵌套的结构外层省略大括号:
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
// 反例
|
||||||
|
for (int i = 0; i < n; ++i)
|
||||||
|
for (int j = 0; j < m; ++j)
|
||||||
|
printf("%d", a[i][j]);
|
||||||
|
|
||||||
|
// 正确做法
|
||||||
|
for (int i = 0; i < n; ++i) {
|
||||||
|
for (int j = 0; j < m; ++j)
|
||||||
|
printf("%d", a[i][j]);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
3. 不要只在 `if-else` 的一边省略大括号:
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
// 反例
|
||||||
|
if (cond) {
|
||||||
|
i += 1;
|
||||||
|
j += 1;
|
||||||
|
} else
|
||||||
|
i -= 1;
|
||||||
|
|
||||||
|
// 正确
|
||||||
|
if (cond) {
|
||||||
|
i += 1;
|
||||||
|
j += 1;
|
||||||
|
} else {
|
||||||
|
i -= 1;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
4. 不要无理由的使用逗号表达式:
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
// 反例
|
||||||
|
w = 5, h = 5;
|
||||||
|
|
||||||
|
// 正确做法
|
||||||
|
w = 5;
|
||||||
|
h = 5;
|
||||||
|
```
|
||||||
|
|
||||||
|
5. 可以考虑在提交代码前使用 `clang-format` 格式化一下,使用命令:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
clang-format -i xxx.cpp
|
||||||
|
```
|
||||||
|
|
||||||
|
请不要格式化不是你编写的代码!
|
Loading…
x
Reference in New Issue
Block a user