@chengzhao-dev/dsh-cpp-workflow 0.7.3 → 0.7.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +7 -7
- package/README.md +7 -7
- package/lib/client.js +1 -1
- package/lib/comment-policy.js +1 -1
- package/lib/index.js +3 -3
- package/package.json +9 -3
- package/skills/building-cpp/SKILL.md +15 -15
- package/skills/building-cpp/examples/environment.md +4 -3
- package/skills/building-cpp/references/{build-script.md → 01-build-script.md} +4 -3
- package/skills/building-cpp/references/{environment.md → 02-environment.md} +3 -2
- package/skills/building-cpp/steps/01-build.md +2 -2
- package/skills/building-cpp/steps/02-tooling.md +12 -12
- package/skills/building-cpp/steps/03-check.md +5 -5
- package/skills/building-cpp/{project/vscode/02-settings.md → steps/04-vscode.md} +24 -6
- package/skills/developing-cpp/SKILL.md +13 -13
- package/skills/developing-cpp/references/comments/{comment-rules.md → 01-comment-rules.md} +52 -47
- package/skills/developing-cpp/references/comments/{comment-style.md → 02-style.md} +11 -7
- package/skills/developing-cpp/references/comments/examples/{declaration-class.md → 01-declaration-class.md} +2 -1
- package/skills/developing-cpp/references/comments/examples/{declaration-constructor.md → 02-declaration-constructor.md} +2 -1
- package/skills/developing-cpp/references/comments/examples/{declaration-enum.md → 03-declaration-enum.md} +3 -3
- package/skills/developing-cpp/references/comments/examples/{declaration-file.md → 04-declaration-file.md} +4 -3
- package/skills/developing-cpp/references/comments/examples/{declaration-function.md → 05-declaration-function.md} +10 -29
- package/skills/developing-cpp/references/comments/examples/{declaration-member.md → 06-declaration-member.md} +1 -0
- package/skills/developing-cpp/references/comments/examples/07-declaration-predicate.md +15 -0
- package/skills/developing-cpp/references/comments/examples/{declaration-struct.md → 08-declaration-struct.md} +1 -0
- package/skills/developing-cpp/references/comments/examples/09-implementation-body.md +114 -0
- package/skills/developing-cpp/references/comments/examples/10-implementation-todo.md +17 -0
- package/skills/developing-cpp/references/comments/glossary/{comment-terms.md → 01-comment-terms.md} +8 -7
- package/skills/developing-cpp/references/design/{project-design.md → 01-project-design.md} +8 -7
- package/skills/developing-cpp/references/design/examples/{behavior-sentence.md → 01-behavior-sentence.md} +1 -0
- package/skills/developing-cpp/references/design/examples/{coordinate-table.md → 02-coordinate-table.md} +1 -0
- package/skills/developing-cpp/references/design/examples/{header-place.md → 03-header-place.md} +1 -0
- package/skills/developing-cpp/references/design/examples/{library-boundary.md → 04-library-boundary.md} +3 -2
- package/skills/developing-cpp/references/design/examples/{library-split.md → 05-library-split.md} +1 -0
- package/skills/developing-cpp/references/design/examples/{move-result.md → 06-move-result.md} +4 -10
- package/skills/developing-cpp/references/design/examples/{preview-read.md → 07-preview-read.md} +1 -0
- package/skills/developing-cpp/references/design/examples/08-screen-enter-exit.md +40 -0
- package/skills/developing-cpp/references/design/session-layer.md +91 -0
- package/skills/developing-cpp/references/exceptions/{throw-rules.md → 01-exception-rules.md} +10 -18
- package/skills/developing-cpp/references/exceptions/examples/{throw-boundary.md → 01-throw-boundary.md} +1 -0
- package/skills/developing-cpp/references/exceptions/examples/{throw-build-mode.md → 02-throw-build-mode.md} +3 -2
- package/skills/developing-cpp/references/exceptions/examples/{throw-catch.md → 03-throw-catch.md} +1 -0
- package/skills/developing-cpp/references/exceptions/examples/{throw-destructor.md → 04-throw-destructor.md} +1 -0
- package/skills/developing-cpp/references/exceptions/examples/{throw-format.md → 05-throw-format.md} +1 -0
- package/skills/developing-cpp/references/exceptions/examples/{throw-message.md → 06-throw-message.md} +2 -1
- package/skills/developing-cpp/references/exceptions/examples/{throw-when.md → 07-throw-when.md} +1 -0
- package/skills/developing-cpp/references/index.md +134 -0
- package/skills/developing-cpp/references/language/glossary/{language-terms.md → 01-language-terms.md} +3 -2
- package/skills/developing-cpp/references/language/language-basics/01-objects.md +1 -0
- package/skills/developing-cpp/references/language/language-basics/02-initialization.md +15 -7
- package/skills/developing-cpp/references/language/language-basics/03-deduction.md +1 -0
- package/skills/developing-cpp/references/language/language-basics/04-const.md +1 -0
- package/skills/developing-cpp/references/language/language-basics/05-expressions.md +7 -3
- package/skills/developing-cpp/references/language/language-basics/06-functions.md +6 -3
- package/skills/developing-cpp/references/language/language-basics/07-classes.md +1 -0
- package/skills/developing-cpp/references/language/language-basics/08-raii.md +1 -0
- package/skills/developing-cpp/references/language/language-basics/09-templates.md +1 -0
- package/skills/developing-cpp/references/language/language-basics/{10-checklist.md → 10-language-checklist.md} +2 -0
- package/skills/developing-cpp/references/language/performance/01-measure.md +1 -0
- package/skills/developing-cpp/references/language/performance/02-complexity.md +1 -0
- package/skills/developing-cpp/references/language/performance/03-memory.md +1 -0
- package/skills/developing-cpp/references/language/performance/04-undefined-behavior.md +1 -0
- package/skills/developing-cpp/references/language/performance/05-pitfalls.md +1 -0
- package/skills/developing-cpp/references/language/performance/06-false-optimizations.md +1 -0
- package/skills/developing-cpp/references/language/performance/07-checklist.md +1 -0
- package/skills/developing-cpp/references/language/standard-library/01-containers.md +5 -3
- package/skills/developing-cpp/references/language/standard-library/02-access.md +1 -0
- package/skills/developing-cpp/references/language/standard-library/03-iteration.md +1 -0
- package/skills/developing-cpp/references/language/standard-library/04-algorithms.md +1 -0
- package/skills/developing-cpp/references/language/standard-library/05-string.md +1 -0
- package/skills/developing-cpp/references/language/standard-library/06-format.md +5 -2
- package/skills/developing-cpp/references/language/standard-library/07-time.md +1 -0
- package/skills/developing-cpp/references/language/standard-library/08-random.md +2 -0
- package/skills/developing-cpp/references/language/standard-library/09-files.md +1 -0
- package/skills/developing-cpp/references/language/standard-library/{10-checklist.md → 10-library-checklist.md} +2 -1
- package/skills/developing-cpp/references/language/value-types/01-width.md +1 -0
- package/skills/developing-cpp/references/language/value-types/02-signed.md +1 -0
- package/skills/developing-cpp/references/language/value-types/03-integer-roles.md +1 -0
- package/skills/developing-cpp/references/language/value-types/04-enum.md +13 -1
- package/skills/developing-cpp/references/language/value-types/05-strong-type.md +1 -0
- package/skills/developing-cpp/references/language/value-types/06-const-method.md +1 -0
- package/skills/developing-cpp/references/language/value-types/07-nodiscard.md +1 -0
- package/skills/developing-cpp/references/language/value-types/08-comparison.md +1 -0
- package/skills/developing-cpp/references/language/value-types/09-subscript.md +2 -1
- package/skills/developing-cpp/references/language/value-types/10-float-bool.md +1 -0
- package/skills/developing-cpp/references/language/value-types/11-checklist.md +2 -0
- package/skills/developing-cpp/references/layout/01-source-layout.md +3 -2
- package/skills/developing-cpp/references/layout/02-file-template.md +4 -3
- package/skills/developing-cpp/references/layout/03-declaration-order.md +15 -4
- package/skills/developing-cpp/references/naming/{identifier-names.md → 01-identifier-names.md} +31 -9
- package/skills/developing-cpp/references/naming/examples/{naming-accessor.md → 01-naming-accessor.md} +1 -0
- package/skills/developing-cpp/references/naming/examples/02-naming-basic.md +4 -0
- package/skills/developing-cpp/references/naming/examples/{naming-current-prefix.md → 03-naming-current-prefix.md} +1 -0
- package/skills/developing-cpp/references/naming/examples/{naming-local.md → 04-naming-local.md} +2 -1
- package/skills/developing-cpp/references/naming/examples/{naming-lookup.md → 05-naming-lookup.md} +1 -0
- package/skills/developing-cpp/references/naming/examples/{naming-lower-layer.md → 06-naming-lower-layer.md} +2 -2
- package/skills/developing-cpp/references/naming/examples/{naming-multiple-return.md → 07-naming-multiple-return.md} +1 -0
- package/skills/developing-cpp/references/naming/examples/{naming-player-value.md → 08-naming-player-value.md} +1 -0
- package/skills/developing-cpp/references/naming/examples/{naming-predicate.md → 09-naming-predicate.md} +1 -0
- package/skills/developing-cpp/references/naming/examples/{naming-rename.md → 10-naming-rename.md} +1 -0
- package/skills/developing-cpp/references/naming/examples/{naming-responsibility.md → 11-naming-responsibility.md} +1 -0
- package/skills/developing-cpp/references/naming/examples/{naming-role.md → 12-naming-role.md} +1 -0
- package/skills/developing-cpp/references/naming/examples/{naming-state-change.md → 13-naming-state-change.md} +4 -4
- package/skills/developing-cpp/references/naming/glossary/01-naming-terms.md +28 -0
- package/skills/developing-cpp/references/naming/glossary/{position.md → 02-position.md} +2 -1
- package/skills/developing-cpp/references/naming/glossary/03-selection.md +23 -0
- package/skills/developing-cpp/references/naming/glossary/{square.md → 04-square.md} +3 -2
- package/skills/developing-cpp/references/naming/glossary/05-scoring.md +27 -0
- package/skills/developing-cpp/references/naming/glossary/06-difficulty.md +26 -0
- package/skills/developing-cpp/references/naming/glossary/07-stream.md +21 -0
- package/skills/developing-cpp/references/style/{google-cpp-style.md → 01-google-cpp-style.md} +16 -9
- package/skills/developing-cpp/scripts/checking-comments.mjs +107 -8
- package/skills/developing-cpp/steps/01-behavior.md +5 -5
- package/skills/developing-cpp/steps/02-boundary.md +6 -6
- package/skills/developing-cpp/steps/03-declaration.md +8 -8
- package/skills/developing-cpp/steps/04-implementation.md +17 -17
- package/skills/engineering/SKILL.md +14 -14
- package/skills/engineering/references/cmake/linking/01-libraries.md +7 -4
- package/skills/engineering/references/cmake/linking/02-rpath.md +1 -0
- package/skills/engineering/references/cmake/linking/03-undefined-reference.md +2 -1
- package/skills/engineering/references/cmake/linking/04-inspect.md +1 -1
- package/skills/engineering/references/cmake/modules/{functions.md → 01-functions.md} +3 -2
- package/skills/engineering/references/cmake/standards/{cxx-standard.md → 01-cxx-standard.md} +3 -2
- package/skills/engineering/references/cmake/standards/examples/{standard.md → 01-standard.md} +2 -1
- package/skills/engineering/references/cmake/standards/{README.md → index.md} +16 -15
- package/skills/engineering/references/cmake/standards/versions.md +44 -0
- package/skills/engineering/references/cmake/targets/{rules.md → 01-target-rules.md} +37 -5
- package/skills/engineering/references/cmake/targets/examples/{rpath.md → 01-rpath.md} +1 -0
- package/skills/engineering/references/cmake/testing/{ctest.md → 01-ctest.md} +3 -2
- package/skills/engineering/references/formatting/01-encoding.md +2 -1
- package/skills/engineering/references/formatting/02-clang-format.md +4 -2
- package/skills/engineering/references/formatting/03-clang-tidy.md +3 -2
- package/skills/engineering/references/formatting/04-clangd.md +2 -0
- package/skills/engineering/references/formatting/examples/{clang-format.md → 01-clang-format.md} +2 -1
- package/skills/engineering/references/formatting/examples/{clang-tidy.md → 02-clang-tidy.md} +1 -0
- package/skills/engineering/references/formatting/examples/{clangd.md → 03-clangd.md} +1 -0
- package/skills/engineering/references/index.md +32 -0
- package/skills/engineering/steps/01-targets.md +3 -3
- package/skills/engineering/steps/02-modules.md +3 -3
- package/skills/engineering/steps/03-check.md +1 -1
- package/skills/shared/budget.md +20 -0
- package/skills/{developing-cpp/references/editing/01-edit-operations.md → shared/operations.md} +7 -3
- package/skills/{developing-cpp/references/editing/02-rule-priority.md → shared/rule-priority.md} +8 -1
- package/skills/building-cpp/project/vscode/01-extensions.md +0 -28
- package/skills/developing-cpp/references/comments/examples/declaration-predicate.md +0 -14
- package/skills/developing-cpp/references/comments/examples/implementation-body.md +0 -81
- package/skills/developing-cpp/references/comments/examples/implementation-todo.md +0 -16
- package/skills/developing-cpp/references/naming/examples/naming-basic.md +0 -3
- package/skills/developing-cpp/references/naming/glossary/naming-terms.md +0 -23
- package/skills/engineering/references/cmake/standards/c++11/README.md +0 -50
- package/skills/engineering/references/cmake/standards/c++14/README.md +0 -48
- package/skills/engineering/references/cmake/standards/c++17/README.md +0 -50
- package/skills/engineering/references/cmake/standards/c++20/README.md +0 -50
- package/skills/engineering/references/cmake/standards/c++23/README.md +0 -50
- package/skills/engineering/references/cmake/targets/examples/glob.md +0 -22
- package/skills/engineering/references/cmake/targets/examples/target.md +0 -156
- /package/skills/engineering/{references/formatting/scripts → scripts}/checking-encoding.mjs +0 -0
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
<!-- 读取时机:查语言细节时;读完能回答:检查清单;预算:一次只开这一份(9 行),不自动串联。 -->
|
|
1
2
|
# 检查清单
|
|
2
3
|
|
|
3
4
|
- [ ] 参与协议、布局或跨平台边界的整数有明确宽度。
|
|
4
5
|
- [ ] 业务编号与容器下标没有混用有符号和无符号。
|
|
5
6
|
- [ ] 封闭状态集合用 `enum class`,没有裸整数状态码。
|
|
7
|
+
- [ ] 没有第二种原因的缺失用 `std::optional`,不用哨兵枚举项。
|
|
6
8
|
- [ ] 查询成员标了尾部 `const`;`[[nodiscard]]` 只出现在结果型 API,结果都有分支处理。
|
|
7
9
|
- [ ] 下标访问前有边界证明,或使用 `at`;乘加得到下标前先拓宽各操作数。
|
|
8
10
|
- [ ] 浮点比较有容差,金额没有用 `double` 结算。
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
<!-- 读取时机:排声明顺序时;读完能回答:源码布局;预算:一次只开这一份(78 行),不自动串联。 -->
|
|
1
2
|
# 源码布局
|
|
2
3
|
|
|
3
4
|
布局服务于边界:库是纯的,程序是薄的,测试从公共接口进入。这里只定目录和文件职责;CMake target 由 `engineering` 的 `steps/01-targets.md` 定。
|
|
@@ -28,7 +29,7 @@ app/
|
|
|
28
29
|
cli.h 输入输出与参数解析的私有接口
|
|
29
30
|
src/
|
|
30
31
|
cli.cpp 终端交互
|
|
31
|
-
main.cpp 只做装配:构造依赖、调用
|
|
32
|
+
main.cpp 只做装配:构造依赖、调用 RunGame、翻译退出码
|
|
32
33
|
```
|
|
33
34
|
|
|
34
35
|
- `main.cpp` 不写业务规则;它只解析参数、打开流、调用库、捕获边界异常。
|
|
@@ -53,7 +54,7 @@ app/
|
|
|
53
54
|
|
|
54
55
|
## 头文件契约
|
|
55
56
|
|
|
56
|
-
自包含、共享常量、`using namespace`、模板实现与包含顺序的规则见 `references/style/google-cpp-style.md`「头文件」;扩展名与槽位见 `references/layout/02-file-template.md`。库不包含 `<iostream>` 的边界见本文「程序」节。本文只强调一条:公共头只暴露稳定契约,测试只包含公共头。
|
|
57
|
+
自包含、共享常量、`using namespace`、模板实现与包含顺序的规则见 `references/style/01-google-cpp-style.md`「头文件」;扩展名与槽位见 `references/layout/02-file-template.md`。库不包含 `<iostream>` 的边界见本文「程序」节。本文只强调一条:公共头只暴露稳定契约,测试只包含公共头。
|
|
57
58
|
|
|
58
59
|
## 测试
|
|
59
60
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
<!-- 读取时机:排声明顺序时;读完能回答:文件模板;预算:一次只开这一份(137 行),不自动串联。 -->
|
|
1
2
|
# 文件模板
|
|
2
3
|
|
|
3
|
-
本文件是头文件与实现文件槽位的唯一权威。`references/style/google-cpp-style.md` 的头文件节保留文件级规则(守卫选择、自包含、包含顺序),槽位细节以本文件为准;`references/layout/01-source-layout.md` 只定文件放在哪;`steps/03-declaration.md` 指向这里。类内声明顺序仍只写在 `references/layout/03-declaration-order.md`,本文件不复制。
|
|
4
|
+
本文件是头文件与实现文件槽位的唯一权威。`references/style/01-google-cpp-style.md` 的头文件节保留文件级规则(守卫选择、自包含、包含顺序),槽位细节以本文件为准;`references/layout/01-source-layout.md` 只定文件放在哪;`steps/03-declaration.md` 指向这里。类内声明顺序仍只写在 `references/layout/03-declaration-order.md`,本文件不复制。
|
|
4
5
|
|
|
5
6
|
## 选择扩展名
|
|
6
7
|
|
|
@@ -29,7 +30,7 @@
|
|
|
29
30
|
| `my_project/include/board.h` | `MY_PROJECT_INCLUDE_BOARD_H_` |
|
|
30
31
|
| `my_project/include/game.h` | `MY_PROJECT_INCLUDE_GAME_H_` |
|
|
31
32
|
| `my_project/include/computer.h` | `MY_PROJECT_INCLUDE_COMPUTER_H_` |
|
|
32
|
-
| `
|
|
33
|
+
| `my_project/include/game_session.h` | `MY_PROJECT_INCLUDE_GAME_SESSION_H_` |
|
|
33
34
|
| `my_project/tests/computer_test_support.h` | `MY_PROJECT_TESTS_COMPUTER_TEST_SUPPORT_H_` |
|
|
34
35
|
|
|
35
36
|
- 通用示例:`taskflow/include/task_table.h` → `TASKFLOW_INCLUDE_TASK_TABLE_H_`。
|
|
@@ -39,7 +40,7 @@
|
|
|
39
40
|
|
|
40
41
|
槽位顺序固定:
|
|
41
42
|
|
|
42
|
-
1. 文件头注释(格式见 `references/comments/comment-rules.md`,留在守卫外)
|
|
43
|
+
1. 文件头注释(格式见 `references/comments/01-comment-rules.md`,留在守卫外)
|
|
43
44
|
2. `#ifndef` / `#define`
|
|
44
45
|
3. 分组包含(组间空行,组内字母序)
|
|
45
46
|
4. 命名空间内:常量 → 枚举 → 结构体 → 自由函数 → 类
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
<!-- 读取时机:排声明顺序时;读完能回答:声明顺序;预算:一次只开这一份(151 行),不自动串联。 -->
|
|
1
2
|
# 声明顺序
|
|
2
3
|
|
|
3
4
|
顺序的作用是让读者先看类型和契约,再看数据。`clang-format` 不管顺序,靠人工和 review 维持。
|
|
@@ -23,7 +24,7 @@ class TaskTable {
|
|
|
23
24
|
~TaskTable();
|
|
24
25
|
|
|
25
26
|
// 5. 其他成员函数
|
|
26
|
-
[[nodiscard]] bool
|
|
27
|
+
[[nodiscard]] bool HasTask(int id) const;
|
|
27
28
|
[[nodiscard]] TaskState GetTaskState(int id) const;
|
|
28
29
|
void SetTaskState(int id, TaskState state);
|
|
29
30
|
|
|
@@ -55,7 +56,8 @@ class TaskTable {
|
|
|
55
56
|
Google、LLVM、OpenCV 都没有规定这一组内部的次序;本工作流补上:
|
|
56
57
|
|
|
57
58
|
- 有比较运算符时,开头是 `operator==`:成员默认比较和 `friend` 都放这里,然后才是谓词 `Is` / `Has` / `Can`、`Get`、普通函数按使用先后。`friend` 不再放到这一组末尾。
|
|
58
|
-
- `
|
|
59
|
+
- 不属于 `Is` / `Get` / `Set` 的流程函数按调用层次排:入口操作在前,它调用的显示 helper 紧随其后;`DisplayOptions`、`Note` 等共享 helper 放在调用它们的流程之后;终端进入、退出与清屏可集中在组首,按进入、退出、清屏排。
|
|
60
|
+
- `CheckInRange` 是诊断函数,紧挨 `IsInRange` 写,排在 `Get` 之前;它是本条谓词顺序的例外,见 `references/exceptions/01-exception-rules.md`。
|
|
59
61
|
- 多个直接返回成员的 `Get` 按对应数据成员的声明顺序排列,不按调用频率或字母序:成员依次是 `current_player_`、`board_`、`history_`,getter 就依次是 `GetCurrentPlayer()`、`GetBoard()`、`GetHistory()`;成员顺序调整时 getter 一起改,一个类里只留一套顺序。
|
|
60
62
|
- 直接返回成员的 `Get` 按成员顺序;没有独立成员的派生查询排在直接 `Get` 之后,按「先局部元素、后整体状态」阅读,不插进直接 `Get` 之间。例:`Game` 的 `GetResult()` 扫描棋盘后才得到结果,排在直接返回成员的 `GetCurrentPlayer()`、`GetBoard()`、`GetHistory()` 之后。
|
|
61
63
|
- 成对的局部/整体操作沿用同一方向:先 `ClearSquare(pos)` 清一格,再 `Clear()` 清整盘;局部操作在前,整体操作在后,不按调用频率互换。
|
|
@@ -87,7 +89,7 @@ struct TaskRecord {
|
|
|
87
89
|
TaskState state{TaskState::kPending};
|
|
88
90
|
};
|
|
89
91
|
|
|
90
|
-
[[nodiscard]] bool
|
|
92
|
+
[[nodiscard]] bool HasTask(int id);
|
|
91
93
|
[[nodiscard]] std::string TaskStateToString(TaskState state);
|
|
92
94
|
|
|
93
95
|
class TaskTable {
|
|
@@ -107,7 +109,7 @@ class TaskTable {
|
|
|
107
109
|
## `.cpp` 的定义顺序
|
|
108
110
|
|
|
109
111
|
- 本工作流约定:与头文件声明同序,先类型和常量,再自由函数,再成员函数;成员函数按类内声明顺序。Google、LLVM 与 C++ Core Guidelines 都没有规定 `.cpp` 成员定义的次序;这里选同序,是为了让读者能在头文件与实现之间逐项对照。
|
|
110
|
-
- 文件头注释之后先放本文件的 `namespace taskflow {`,再放公共定义;常量仍排在具名命名空间内、成员函数之前;内部链接的写法按 `references/style/google-cpp-style.md`,本文不复述。
|
|
112
|
+
- 文件头注释之后先放本文件的 `namespace taskflow {`,再放公共定义;常量仍排在具名命名空间内、成员函数之前;内部链接的写法按 `references/style/01-google-cpp-style.md`,本文不复述。
|
|
111
113
|
- 构造函数、析构函数、拷贝/移动定义通常放在成员函数组最前;如果头文件把某个成员声明在构造函数之前,仍按头文件声明顺序,不把构造函数硬提到前面。`= default` 的定义放 `.cpp` 时紧跟声明顺序。
|
|
112
114
|
- 不要为了「先编译」重排定义;C++ 不要求定义顺序,读者顺序优先。
|
|
113
115
|
|
|
@@ -130,8 +132,17 @@ class TaskTable {
|
|
|
130
132
|
- 取回一个结果并按结果分支的语句连成一组(声明、`if`、`throw` / `return`),组内不空行;这一组与收尾(记录这一步、轮到对方、返回成功)之间空 1 行。
|
|
131
133
|
- 同一组语句不拆:三个校验之后的赋值与 `return` 之间不空行。
|
|
132
134
|
- 只有次序依赖的一对 `if`(`GetWinner`、`JudgeAfterPlace`)之间空 1 行。
|
|
135
|
+
- 互斥策略组与组之间空 1 行;`std::unreachable()` 紧跟 `switch` 的右花括号,中间不空行。位置理由不在这里重复。
|
|
136
|
+
- 循环结束后还有 `return` 时,循环与 `return` 之间空 1 行。
|
|
133
137
|
- 最多 1 行,不连续空两行。
|
|
134
138
|
|
|
139
|
+
### 头文件里的空行
|
|
140
|
+
|
|
141
|
+
- 类内六个组之间、同组函数之间、命名空间内顶层实体之间各空 1 行。
|
|
142
|
+
- 数据成员之间不空行;`private:` 之前空 1 行。
|
|
143
|
+
- 注释贴住它说明的声明,注释与声明之间不空行。
|
|
144
|
+
- Doxygen 列表项之间不插空注释行。
|
|
145
|
+
|
|
135
146
|
## 依据
|
|
136
147
|
|
|
137
148
|
- [Google C++ Style Guide: Declaration Order](https://google.github.io/styleguide/cppguide.html#Declaration_Order):类内先 public、再 protected、最后 private;同节内类型、常量、函数、数据成员的先后。
|
package/skills/developing-cpp/references/naming/{identifier-names.md → 01-identifier-names.md}
RENAMED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
<!-- 读取时机:命名或统一术语时;读完能回答:命名;预算:一次只开这一份(130 行),不自动串联。 -->
|
|
1
2
|
# 命名
|
|
2
3
|
|
|
3
4
|
命名先回答「它是什么角色」,再选词。规则表是底线,语义角色表决定用哪个动词。
|
|
@@ -24,10 +25,20 @@
|
|
|
24
25
|
- 布尔变量用 `is_` / `has_` / `can_` 开头:`is_valid`、`has_stock`;不要用 `flag`、`status`。
|
|
25
26
|
- 容器用复数:`tasks`、`orders`;单个元素用单数。
|
|
26
27
|
- 缩写按单词处理:`url`、`id`、`http`,不要写 `URL` 混进 snake_case;类型名里可以 `HttpClient`。
|
|
27
|
-
- 不用单字母名:坐标参数和局部变量用 `pos`,行、列用 `row`、`col`;公开数据成员仍用完整名词(`Move::position`),避免 API 缩写;迭代器式的位置参数才写完整 `position`。取舍依据与更多词见 [naming-terms.md](glossary/naming-terms.md)。`i` / `j` 只用于没有领域名字的计数(如遍历固定长度的下标)。也不要用 `data`、`info`、`tmp` 这类不说明内容的名字。
|
|
28
|
+
- 不用单字母名:坐标参数和局部变量用 `pos`,行、列用 `row`、`col`;公开数据成员仍用完整名词(`Move::position`),避免 API 缩写;迭代器式的位置参数才写完整 `position`。取舍依据与更多词见 [01-naming-terms.md](glossary/01-naming-terms.md)。`i` / `j` 只用于没有领域名字的计数(如遍历固定长度的下标)。也不要用 `data`、`info`、`tmp` 这类不说明内容的名字。
|
|
28
29
|
- 参数或局部变量的类型已在签名里写明时,用主流缩写而不是重复类型名:`Position pos`,不写 `Position position`;缩写只在含义无歧义时使用。
|
|
30
|
+
- 流的参数与成员是上一条的例外:即使类型已在签名里,也写完整词 `input`、`output`、`diagnostics`,不写 `in`、`out`、`err`;取舍见 [glossary/07-stream.md](glossary/07-stream.md)。
|
|
29
31
|
- 局部变量与类成员用同一套词根:成员叫 `current_player_`,局部就叫 `current_player`,不写 `currentPlayer` 这类 camelCase。涉及多个对象时把角色写进名字,如 `target_square`、`target_square_value`,不要用 `target` 这种离开上下文读不出对象的词。规则表里的 snake_case 同样约束局部变量,`.clang-tidy` 的 `readability-identifier-naming` 会检查。
|
|
30
32
|
|
|
33
|
+
## 用词
|
|
34
|
+
|
|
35
|
+
- 函数名用「动词 + 对象」:`ChooseRandomPosition`、`PlaceAt`、`GetCurrentPlayer`;不要只写动词(`Choose`)或只写对象(`RandomPosition`)。算法、数学或协议的既有专名保留原词,不拆成动词 + 对象,例如 `Minimax`。
|
|
36
|
+
- 容器用复数,并把元素写进名字:`empty_positions`、`moves`;不要写 `empties` 这类省略元素的缩写。
|
|
37
|
+
- 计数带对象:`empty_count`、`move_count`;不要只写 `count`。
|
|
38
|
+
- 不删字母缩写:`distribution` 不写 `dist`;`position` 在需要完整名词时写全,`pos` 只作局部坐标缩写。
|
|
39
|
+
- 可能选不中时返回 `std::optional`:`ChoosePosition`、`ChooseRandomPosition`、`ChooseBestPosition` 没有合法选择时返回 `std::nullopt`,不返回哨兵位置。
|
|
40
|
+
- 共享资源访问器用名词短语(`RandomEngine`),不写 `GetRandomEngine`;`Get` 留给类里直接返回成员的查询。
|
|
41
|
+
|
|
31
42
|
## 字符集与保留标识符
|
|
32
43
|
|
|
33
44
|
- 代码标识符只用 ASCII:字母、数字、下划线。标准允许 Unicode XID 标识符,但工具链、字体与跨平台编辑不一致,Google 同样要求 ASCII。
|
|
@@ -51,6 +62,8 @@
|
|
|
51
62
|
| 读写 | `Read` / `Write` | `ReadEnvironment()`、`WriteLedger()` | 涉及 I/O 时用;不要在库核心用 |
|
|
52
63
|
| 访问器 | `Get` + PascalCase,或 snake_case | `GetHistory()`、`history()` | Google 允许两种;同一 API 只选一种,不用成员名直接当函数名 |
|
|
53
64
|
|
|
65
|
+
- 界面提示用 `Prompt`:显示菜单或提示并等待用户选择时写 `PromptMatchMode()`;`Read` / `Write` 留给 I/O 读写与记录处理。提示负责显示与重试,实际读行由 `GetLine()` 完成;`std::optional` 表示输入结束,这是 `Get` 的界面例外,不承诺前置条件已满足。
|
|
66
|
+
- 带编号选项的开局菜单用 `Display<Object>Options`:写 `DisplayMatchModeOptions()`、`DisplayDifficultyOptions()`;不写 `Show<Object>Menu`。`Prompt<Object>` 负责显示并读入,`Display*Options` 只负责显示。
|
|
54
67
|
- 返回 `bool` 的 lambda 也用 `is_` / `has_` / `can_` 命名:`auto is_empty = [](const Order& order) { ... };`。
|
|
55
68
|
- `Find` 不把“没找到”当异常;`Get` 在前置条件不满足时抛标准异常:坐标越界用 `std::out_of_range`,调用时不变量已破坏用 `std::logic_error`;只有调用方必须把这些类型区分开时,才新增派生自它们的类型。两种构建模式的抛出条件相同。两者不要互换。
|
|
56
69
|
- 查询返回多个值时用 `struct`,不要用三个 `GetXxx()` 加输出参数。
|
|
@@ -67,7 +80,7 @@
|
|
|
67
80
|
- `Get` 只用于取值,`Set` 只用于设值;不要用 `CheckLine()` 当布尔 getter。
|
|
68
81
|
- `Check` / `Validate` 留给会报告错误、产生诊断或可能失败的操作,例如 `CheckEnvironment()` 返回问题列表;返回结果类型的校验器也可以用这两个词,只要名字说明它返回结果。它们不做返回纯 `bool` 的查询,bool 查询写在 `Is` / `Has` / `Can` 里。
|
|
69
82
|
- 动作可能因为多个前置条件失败、而调用方需要区分原因时,返回 `enum class`(`PlaceResult`、`GameResult`、`WinPreviewResult`),不要用 `bool` 加输出参数,也不要拆成一串 `IsXxx` 判断再重复检查。
|
|
70
|
-
- 结果枚举的取值写结果类别,两个词以内:成功用 `kPlaced
|
|
83
|
+
- 结果枚举的取值写结果类别,两个词以内:成功用 `kPlaced`;失败按发生原因分别取名(如 `kInvalidPlayer`、`kOccupiedSquare`),不合并成一个 `kInvalid`。行尾注释以类别开头,写成「成功:…」「失败:…」,冒号后写这次调用的结果与原因,各行按「对局状态 → 落子玩家 → 目标方格」的顺序写。完整案例见 `references/naming/examples/13-naming-state-change.md`。
|
|
71
84
|
- 结果枚举的类型用「操作或对象 + Result」,读作「什么的 Result」:`Game::Place` 的结果是 `PlaceResult`,`GetResult` 的结果是 `GameResult`。结果跟随产生它的层:`Board` 只写入,没有结果枚举,也不把 `GameResult` 塞进 `Square` 或 `Board`。
|
|
72
85
|
- 同名操作在不同层返回不同结果时,按各自处理的对象取名:`Board::SetSquare` 只写入,没有返回值;`Game::Place` 处理的是一次落子,返回 `PlaceResult`;整局结果由 `Game::GetResult` 另外回答。不把两个答案压进同一个名字,读者才分得清拿到的是「这一手写成了没有」还是「这一局结束了没有」。
|
|
73
86
|
- 枚举的取值写成短句,与类型名对齐:`GameResult` 用 `kInProgress`、`kXWin`、`kOWin`、`kDraw`;`SquareValue` 用 `kEmpty`、`kX`、`kO`,行尾写「空」「X」「O」;不写 `kXWins` 这类主谓句,也不掺动作名。
|
|
@@ -80,7 +93,7 @@
|
|
|
80
93
|
1. 纯数据、无 invariant、可以直接聚合初始化 → `struct`,名字是名词:`Order`, `TaskRecord`。
|
|
81
94
|
2. 有 invariant、需要构造函数维持 → `class`,名字是名词:`TaskTable`, `Warehouse`。
|
|
82
95
|
3. 封闭的状态集合 → `enum class`,名字是单数名词:`TaskState`, `OrderResult`;枚举子用 `k` 前缀。
|
|
83
|
-
- 类型名用拥有它的那一层的词汇:`PlaceResult` 与 `GameResult` 都在 `Game` 层;`Board` 只做写入,不出现 `Game` 前缀,也不承担终局。反例见 `references/naming/examples/naming-lower-layer.md`。
|
|
96
|
+
- 类型名用拥有它的那一层的词汇:`PlaceResult` 与 `GameResult` 都在 `Game` 层;`Board` 只做写入,不出现 `Game` 前缀,也不承担终局。反例见 `references/naming/examples/06-naming-lower-layer.md`。
|
|
84
97
|
4. 只有行为、没有状态,且调用方需要替换实现 → 抽象基类;只有一个实现时先用普通类。
|
|
85
98
|
5. 一组自由函数 → 命名空间 + 动词;不要为了「像 Java」造一个 `XxxUtil` 类。
|
|
86
99
|
|
|
@@ -98,12 +111,21 @@
|
|
|
98
111
|
|
|
99
112
|
## 依据
|
|
100
113
|
|
|
101
|
-
-
|
|
102
|
-
-
|
|
103
|
-
-
|
|
104
|
-
- C++ Core Guidelines
|
|
105
|
-
-
|
|
114
|
+
- [Google C++ Style Guide: Naming](https://google.github.io/styleguide/cppguide.html#Naming):类型与函数 PascalCase、变量 snake_case、常量与枚举子 `k` 前缀。
|
|
115
|
+
- [Google Python Style: Naming](https://google.github.io/styleguide/pyguide.html#316-naming):模块、类、函数、常量与私有名的用词与大小写。
|
|
116
|
+
- [PEP 8: Naming Conventions](https://peps.python.org/pep-0008/#naming-conventions):小写、下划线、类名与异常名的惯例。
|
|
117
|
+
- [C++ Core Guidelines: NL](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#s-naming):NL.5 名字不编码类型、NL.7 长度与作用域成比例、NL.8 风格一致、NL.9 全大写只给宏、NL.19 避免易误读。
|
|
118
|
+
- [LLVM: Naming](https://llvm.org/docs/CodingStandards.html#name-types-functions-variables-and-enumerators-properly):类型、函数、变量与枚举的命名与缩写规则。
|
|
119
|
+
- [Rust API Guidelines: Naming](https://rust-lang.github.io/api-guidelines/naming.html):类型、函数、变量与 feature 名的可读性。
|
|
120
|
+
- [Rust RFC 430](https://rust-lang.github.io/rfcs/0430-finalizing-naming-conventions.html):Rust 命名约定与大小写。
|
|
121
|
+
- [clang-tidy: readability-identifier-naming](https://clang.llvm.org/extra/clang-tidy/checks/readability/identifier-naming.html):按类别配置命名风格与大小写。
|
|
122
|
+
- [clang-tidy: readability-identifier-length](https://clang.llvm.org/extra/clang-tidy/checks/readability/identifier-length.html):过短或过长标识符的阈值。
|
|
123
|
+
- [Ruff N801](https://docs.astral.sh/ruff/rules/invalid-class-name/):类名不符合 CapWords 时的诊断。
|
|
124
|
+
- [Pylint C0103](https://pylint.readthedocs.io/en/stable/user_guide/messages/convention/invalid-name.html):不符合命名风格的诊断与命名组。
|
|
125
|
+
- [Microsoft .NET identifier names](https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/identifier-names):类型、成员、参数与局部变量的用词与大小写。
|
|
126
|
+
|
|
127
|
+
`square` / `SquareValue` 的取舍与词义来源见 [04-square.md](glossary/04-square.md)。
|
|
106
128
|
|
|
107
129
|
## 术语
|
|
108
130
|
|
|
109
|
-
中文术语与英文标识符的对应见 `references/language/glossary/language-terms.md`。代码标识符用英文;注释可以用中文,但同一个概念只用一种译法。
|
|
131
|
+
中文术语与英文标识符的对应见 `references/language/glossary/01-language-terms.md`。代码标识符用英文;注释可以用中文,但同一个概念只用一种译法。
|
package/skills/developing-cpp/references/naming/examples/{naming-local.md → 04-naming-local.md}
RENAMED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
<!-- 读取时机:命名示例时;读完能回答:正反例:局部名字;预算:一次只开这一份(45 行),不自动串联。 -->
|
|
1
2
|
# 正反例:局部名字
|
|
2
3
|
|
|
3
4
|
**差**:
|
|
@@ -42,4 +43,4 @@ Game search_game = game;
|
|
|
42
43
|
| `LineResult` | `ReadResult` | 结果名跟读入动作,`Line` 已表示成线 |
|
|
43
44
|
| `OutcomeText` | `ResultText` | 跟 `GameResult` 用同一个词 |
|
|
44
45
|
| `human_moves` | `human_move_count` | 计数用 `_count`,不用复数 |
|
|
45
|
-
| `against_cpu` | `
|
|
46
|
+
| `against_cpu` | `is_against_computer` | 布尔用 `is_`,对象写完整 `computer` |
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
<!-- 读取时机:命名示例时;读完能回答:正反例:下层类型不要借用上层词汇;预算:一次只开这一份(45 行),不自动串联。 -->
|
|
1
2
|
# 正反例:下层类型不要借用上层词汇
|
|
2
3
|
|
|
3
4
|
**差**:`Board` 定义并返回 `GameResult`。
|
|
@@ -33,8 +34,7 @@ enum class PlaceResult : std::uint8_t {
|
|
|
33
34
|
kPlaced = 0,
|
|
34
35
|
kInvalidPlayer = 1,
|
|
35
36
|
kFinishedGame = 2,
|
|
36
|
-
|
|
37
|
-
kOccupiedSquare = 4,
|
|
37
|
+
kOccupiedSquare = 3,
|
|
38
38
|
};
|
|
39
39
|
|
|
40
40
|
class Game {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
<!-- 读取时机:命名示例时;读完能回答:正反例:状态变更:动作与结果枚举;预算:一次只开这一份(69 行),不自动串联。 -->
|
|
1
2
|
# 正反例:状态变更:动作与结果枚举
|
|
2
3
|
|
|
3
4
|
**差**:
|
|
@@ -5,7 +6,7 @@
|
|
|
5
6
|
```cpp
|
|
6
7
|
enum class PlaceResult : std::uint8_t {
|
|
7
8
|
kPlaced = 0,
|
|
8
|
-
|
|
9
|
+
kInvalidValue = 1,
|
|
9
10
|
kOccupiedSquare = 2,
|
|
10
11
|
};
|
|
11
12
|
|
|
@@ -41,8 +42,7 @@ enum class PlaceResult : std::uint8_t {
|
|
|
41
42
|
kPlaced = 0,
|
|
42
43
|
kInvalidPlayer = 1,
|
|
43
44
|
kFinishedGame = 2,
|
|
44
|
-
|
|
45
|
-
kOccupiedSquare = 4,
|
|
45
|
+
kOccupiedSquare = 3,
|
|
46
46
|
};
|
|
47
47
|
|
|
48
48
|
class Game {
|
|
@@ -60,7 +60,7 @@ class Game {
|
|
|
60
60
|
| `Square::Place` | `Square::SetValue` | 单格只做赋值,不承担落子规则 |
|
|
61
61
|
| `Square::PlaceResult` | `PlaceResult`(`Game`) | 结果跟随产生它的对象,不留在值对象里 |
|
|
62
62
|
| `Square::Clear` | `Board::ClearSquare` | 清空需要棋盘位置,单格只改值 |
|
|
63
|
-
|
|
63
|
+
|
|
64
64
|
|
|
65
65
|
`SetValue` 允许写入任意 `SquareValue`,后写覆盖前值。注释也要跟着改名,但不要把「不为空」当状态:
|
|
66
66
|
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
<!-- 读取时机:统一命名术语时;读完能回答:命名术语与用词;预算:一次只开这一份(27 行),不自动串联。 -->
|
|
2
|
+
# 命名术语与用词
|
|
3
|
+
|
|
4
|
+
本文件是 `references/naming/01-identifier-names.md` 的附录,只在判不准某个词该用完整写法还是缩写时打开;它不随该文件一起加载,也不重复命名规则表。
|
|
5
|
+
|
|
6
|
+
一词一文件只用于命名主题的词汇;注释与语言主题仍用单份术语文件,不按这个结构拆。
|
|
7
|
+
|
|
8
|
+
## 词表
|
|
9
|
+
|
|
10
|
+
| 词 | 结论 | 条目 |
|
|
11
|
+
| --- | --- | --- |
|
|
12
|
+
| `position` / `pos` | 坐标值的参数与局部变量用 `pos`;公开数据成员用 `position`;迭代器式位置对象用 `position` | [02-position.md](02-position.md) |
|
|
13
|
+
| `square` / `SquareValue` | 方格;方格的值;不用 `Mark`、`Cell`、`Token` | [04-square.md](04-square.md) |
|
|
14
|
+
| `selection` | 从多个候选里选一个用 `Choose`,找不到合法候选返回 `optional`;查找已有元素用 `Find`;可能失败的动作与校验用 `Try` | [03-selection.md](03-selection.md) |
|
|
15
|
+
| `score` | 终局评分用 `ScoreFinishedGame`,搜索分是 `int`;不把分数塞进 `GameResult` | [05-scoring.md](05-scoring.md) |
|
|
16
|
+
| `difficulty` | 电脑难度用 `Difficulty` 两档 `kNormal`/`kHard`;与 `MatchMode` 分层 | [06-difficulty.md](06-difficulty.md) |
|
|
17
|
+
| `input` / `output` / `diagnostics` | 流的参数与成员写完整词,不用 `in` / `out` / `err` | [07-stream.md](07-stream.md) |
|
|
18
|
+
|
|
19
|
+
## 加词流程
|
|
20
|
+
|
|
21
|
+
1. 查 ISO C++ 草案对应条款,记下标准自己的写法。
|
|
22
|
+
2. 查一个标准库实现(libstdc++ 或 libc++)与一个同类库(Qt、SFML、Godot)。
|
|
23
|
+
3. 结论写进 `glossary/<NN>-<词>.md`,词表加一行链接;只有一词多义或社区争议大时,才在词条里展开。
|
|
24
|
+
4. 同一事实只写一遍:规则在 `01-identifier-names.md`,依据与取舍在词条里。
|
|
25
|
+
|
|
26
|
+
## 词条格式
|
|
27
|
+
|
|
28
|
+
每个词条按 [02-position.md](02-position.md) 的结构写:一句话结论、语义对照表(语义 / 写法 / 依据)、取舍说明、依据链接。
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
<!-- 读取时机:统一命名术语时;读完能回答:position 与 pos;预算:一次只开这一份(23 行),不自动串联。 -->
|
|
1
2
|
# position 与 pos
|
|
2
3
|
|
|
3
|
-
本文件是 `references/naming/identifier-names.md` 的附录,只讨论 `position` 这个词的写法;词表与加词流程见 [naming-terms.md](naming-terms.md)。
|
|
4
|
+
本文件是 `references/naming/01-identifier-names.md` 的附录,只讨论 `position` 这个词的写法;词表与加词流程见 [01-naming-terms.md](01-naming-terms.md)。
|
|
4
5
|
|
|
5
6
|
同一个词按语义分三种写法,不按长短取舍。
|
|
6
7
|
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
<!-- 读取时机:判不准选择类函数用 Choose、Find 还是 Try 时;读完能回答:选择与查找的命名;预算:一次只开这一份(22 行),不自动串联。 -->
|
|
2
|
+
# selection:选择、查找与尝试
|
|
3
|
+
|
|
4
|
+
从多个候选里选一个,用 `Choose`;名字写清选的对象,如 `ChooseRandomPosition`、`ChooseBestPosition`;对外只暴露 `ChoosePosition`。没有合法候选时返回 `std::optional` 的空值,不返回哨兵位置,也不抛异常。
|
|
5
|
+
|
|
6
|
+
查找已经存在的元素,用 `Find`;找不到是正常结果,返回 `std::optional`、指针或迭代器。`Get` 只用于前置条件保证存在的查询;`GetLine` 这类界面读入不在此列,输入结束用 `std::optional` 表示。
|
|
7
|
+
|
|
8
|
+
可能失败、且调用方需要区分原因的动作,用 `Try` 或领域动词;返回 `enum class` 结果。只有一个失败原因时返回 `bool`。
|
|
9
|
+
|
|
10
|
+
## 语义对照
|
|
11
|
+
|
|
12
|
+
| 语义 | 写法 | 例子 |
|
|
13
|
+
| --- | --- | --- |
|
|
14
|
+
| 从候选里选一个 | `Choose` + 对象 | `ChooseRandomPosition`、`ChooseBestPosition` |
|
|
15
|
+
| 查找已有元素 | `Find` + 对象 | `FindMove`、`FindSquare` |
|
|
16
|
+
| 可能失败的动作 | `Try` / 领域动词 | `TryParse`、`Place` |
|
|
17
|
+
| 前置条件保证存在 | `Get` + 对象 | `GetCurrentPlayer` |
|
|
18
|
+
|
|
19
|
+
## 取舍
|
|
20
|
+
|
|
21
|
+
`Choose` 不暗示候选一定存在,所以返回 `optional`;`Find` 的失败是正常结果,不抛;`Try` 的失败原因需要调用方分支,所以返回结果类型。`Get` 不用于可能找不到的查询。
|
|
22
|
+
|
|
23
|
+
依据见 [01-identifier-names.md](../01-identifier-names.md) 的「语义角色」与末尾 12 条来源。
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
<!-- 读取时机:统一命名术语时;读完能回答:square 与 SquareValue;预算:一次只开这一份(47 行),不自动串联。 -->
|
|
1
2
|
# square 与 SquareValue
|
|
2
3
|
|
|
3
|
-
本文件是 `references/naming/identifier-names.md` 的附录,只讨论 `square` / `SquareValue` 这个词的写法;词表与加词流程见 [naming-terms.md](naming-terms.md)。
|
|
4
|
+
本文件是 `references/naming/01-identifier-names.md` 的附录,只讨论 `square` / `SquareValue` 这个词的写法;词表与加词流程见 [01-naming-terms.md](01-naming-terms.md)。
|
|
4
5
|
|
|
5
6
|
`Square` 是棋盘上的一格,中文「方格」;格中的内容写 `SquareValue`,中文「方格的值」。不用 `Mark`、`Cell`、`Token`。
|
|
6
7
|
|
|
@@ -8,7 +9,7 @@
|
|
|
8
9
|
| --- | --- | --- |
|
|
9
10
|
| 棋盘上的一格 | `Square`;参数、局部变量 `square`;查询 `GetSquare` / `SetSquare` | CPW `Squares`、Stockfish `enum Square`、chess.hpp `Square`、python-chess `chess.Square`;有道 `square`「(棋盘上的)方格」;汉典、海词「方格=方形的格子」 |
|
|
10
11
|
| 格中的内容 | `SquareValue`;查询/写入 `GetValue` / `SetValue`;取值 `kEmpty` / `kX` / `kO` | LucHermitte `enum class SquareValue` 与 `Square::value()`;tictacrustle `SquareValue` / `set_value`;React 井字棋 `value`;海词、必应、有道、汉典 `value=值/数值` |
|
|
11
|
-
| 玩家→格中内容 | `PlayerToSquareValue` | 本项目 `move.h`;`identifier-names.md` 的「来源To结果」 |
|
|
12
|
+
| 玩家→格中内容 | `PlayerToSquareValue` | 本项目 `move.h`;`01-identifier-names.md` 的「来源To结果」 |
|
|
12
13
|
| 一条线的三个值 | `line_values` | 本项目 `game.cpp`;容器用复数 |
|
|
13
14
|
| 目标方格的值 | `target_square_value` | 本项目 `game.cpp`;角色写进名字 |
|
|
14
15
|
| 避免 | `Mark`、`Cell`、`Token` | 井字棋英文常用 `mark`(Exploratorium「marks in empty squares」),但 `Value` 在 Stockfish 是搜索分值、中文项目 `Value[3][3]` 是点位评估分;本项目按裁决统一 `SquareValue`。`cell` 是「单元格」「格网单元」;`token` 是词法单元/占用物 |
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
<!-- 读取时机:统一命名术语时;读完能回答:终局评分与搜索分的命名;预算:一次只开这一份(26 行),不自动串联。 -->
|
|
2
|
+
# scoring:终局评分与搜索分
|
|
3
|
+
|
|
4
|
+
终局评分是规则查询:`ScoreFinishedGame(const Game&)` 读 `GameResult` 与 `move_count`,返回 `int`;X 胜为正、O 胜为负、进行中或平局为 0。分数只在搜索内部用于比较,不写进 `GameResult`,也不交给会话层决定胜负。
|
|
5
|
+
|
|
6
|
+
## 语义对照
|
|
7
|
+
|
|
8
|
+
| 语义 | 写法 | 例子 |
|
|
9
|
+
| --- | --- | --- |
|
|
10
|
+
| 已结束对局的分数 | `Score` + 终局对象 | `ScoreFinishedGame` |
|
|
11
|
+
| 叶节点估值 | `Score` + 状态 | `ScorePosition` |
|
|
12
|
+
| 搜索主函数 | 算法专名 | `Minimax` |
|
|
13
|
+
| 胜负本身 | 结果枚举 | `GameResult` |
|
|
14
|
+
|
|
15
|
+
## 取舍
|
|
16
|
+
|
|
17
|
+
- 名字以 `Score` 开头,说明返回分数而不是胜负;对象写 `FinishedGame`,与 `PlaceResult::kFinishedGame` 同词。
|
|
18
|
+
- 分数是搜索量,不保存进对局;同一局重复查询必须得到同一结果,函数不修改游戏。
|
|
19
|
+
- 不用把 `GameSession` 与 `Score` 拼起来的名字,也不用 `Evaluate`、`Heuristic`:前者把会话层词带进规则层,后两者不说明评的是终局还是中间状态。
|
|
20
|
+
- 对外接口只暴露结果与着法;评分函数可以是私有静态成员,测试通过公开着法间接验证。
|
|
21
|
+
- 搜索需要更大分数时,X 为正、O 为负;调用方按当前玩家取最大或最小,不另建两套评分。
|
|
22
|
+
|
|
23
|
+
## 依据
|
|
24
|
+
|
|
25
|
+
- 本项目 `games/tictactoe/tictactoe/include/computer.h` 与 `src/computer.cpp` 的 `ScoreFinishedGame`、`Minimax`。
|
|
26
|
+
- 领域结果与查询的命名规则见 [01-identifier-names.md](../01-identifier-names.md)。
|
|
27
|
+
- 终局结果类型与落子结果的约定见 [06-move-result.md](../../design/examples/06-move-result.md)。
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
<!-- 读取时机:统一命名术语时;读完能回答:电脑难度与对局模式的命名;预算:一次只开这一份(25 行),不自动串联。 -->
|
|
2
|
+
# difficulty:电脑难度与对局模式
|
|
3
|
+
|
|
4
|
+
`Difficulty` 是电脑策略的封闭档位:`kNormal` 在空位里均匀随机选一个,`kHard` 按终局分支评分做 minimax。它与「双人还是人机」的 `MatchMode` 分层:模式决定谁走,难度只决定电脑怎样走。
|
|
5
|
+
|
|
6
|
+
## 语义对照
|
|
7
|
+
|
|
8
|
+
| 语义 | 写法 | 例子 |
|
|
9
|
+
| --- | --- | --- |
|
|
10
|
+
| 电脑策略档位 | `Difficulty` + 档位枚举子 | `Difficulty::kNormal`、`Difficulty::kHard` |
|
|
11
|
+
| 对局模式 | `MatchMode` + 模式枚举子 | `MatchMode::kTwoPlayer`、`MatchMode::kAgainstComputer` |
|
|
12
|
+
| 切换已有电脑的难度 | `Set` + 属性 | `SetDifficulty` |
|
|
13
|
+
| 按难度选着 | `Choose` + 对象 | `ChooseRandomPosition`、`ChooseBestPosition` |
|
|
14
|
+
|
|
15
|
+
## 取舍
|
|
16
|
+
|
|
17
|
+
- 两档就写两个枚举子,不用 `bool`、整数或字符串;新增一档时补一个枚举子,并让 `switch` 覆盖全部值,不写 `default`。
|
|
18
|
+
- 不用把 `Computer` 与 `Strength` 拼起来的名字,也不用 `Level`、`Mode`:`Strength` 暗示连续强度,`Level` 暗示关卡,`Mode` 与 `MatchMode` 撞词。
|
|
19
|
+
- 难度在电脑对象里,不写进 `Game`;规则层不知道电脑怎样选,换难度只改 `difficulty_`,不重新构造对象。
|
|
20
|
+
- 模式与难度分成两个枚举,不合并成 `Mode`;一个回答「谁走」,一个回答「电脑怎样走」,合并后调用点要猜。
|
|
21
|
+
|
|
22
|
+
## 依据
|
|
23
|
+
|
|
24
|
+
- 本项目 `games/tictactoe/tictactoe/include/computer.h` 的 `Difficulty` 与 `SetDifficulty`;`games/tictactoe/tictactoe/include/console.h` 命名空间层的 `MatchMode`。
|
|
25
|
+
- 选择类函数用 `Choose` 的规则见 [03-selection.md](03-selection.md)。
|
|
26
|
+
- 会话、选着与规则的边界见 [session-layer.md](../../design/session-layer.md)。
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
<!-- 读取时机:统一命名术语时;读完能回答:流参数与成员写 input、output、diagnostics 还是 in、out、err;预算:一次只开这一份(20 行),不自动串联。 -->
|
|
2
|
+
# 流:input、output 与 diagnostics
|
|
3
|
+
|
|
4
|
+
本文件是 `references/naming/01-identifier-names.md` 的附录,只讨论输入、输出与诊断三个流的写法;词表与加词流程见 [01-naming-terms.md](01-naming-terms.md)。
|
|
5
|
+
|
|
6
|
+
流的参数与成员写完整词,不用 `in`、`out`、`err` 这类短名。
|
|
7
|
+
|
|
8
|
+
| 语义 | 写法 | 依据 |
|
|
9
|
+
| --- | --- | --- |
|
|
10
|
+
| 输入流 | `input`、`input_` | 与 `output` 成对,方向一眼可读;`in` 还常作介词,离开类型后容易误读 |
|
|
11
|
+
| 输出流 | `output`、`output_` | 与 `input` 对称,参数与成员使用同一个词根 |
|
|
12
|
+
| 诊断流 | `diagnostics`、`diagnostics_` | 项目把该流叫「诊断」;`error` 已指异常对象,`err` 会与它混读 |
|
|
13
|
+
|
|
14
|
+
- 这是 [01-identifier-names.md](../01-identifier-names.md) 中「类型已在签名里就用主流缩写」的例外:流的方向属于接口契约,不能因为类型已写出就省略词根。
|
|
15
|
+
- `[in]`、`[out]`、`[in,out]` 是 Doxygen 的方向标签,不是标识符,保持原样。
|
|
16
|
+
- 标准流仍写 `std::cin`、`std::cout`、`std::cerr`;`std::out_of_range`、`out of range` 等类型名或英文短语不适用本规则。
|
|
17
|
+
|
|
18
|
+
## 依据
|
|
19
|
+
|
|
20
|
+
- Google C++ Style Guide「General Naming Rules」:https://google.github.io/styleguide/cppguide.html#General_Naming_Rules
|
|
21
|
+
- 本项目 [design/session-layer.md](../../design/session-layer.md) 的会话流与 [comments/02-style.md](../../comments/02-style.md) 的「诊断」用词。
|
package/skills/developing-cpp/references/style/{google-cpp-style.md → 01-google-cpp-style.md}
RENAMED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
<!-- 读取时机:查格式与写法时;读完能回答:Google C++ 风格:本技能采用的版本;预算:一次只开这一份(93 行),不自动串联。 -->
|
|
1
2
|
# Google C++ 风格:本技能采用的版本
|
|
2
3
|
|
|
3
|
-
以 [Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html) 为基线。除「异常政策」「新工程默认 C++23
|
|
4
|
+
以 [Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html) 为基线。除「异常政策」「新工程默认 C++23」「枚举底层类型 `std::uint8_t`」和「注释格式」四处明确偏离外,排版、命名、头文件、包含顺序和类型都按 Google 原文执行;普通对象默认花括号属于 Google 允许的本地选择,不单列为偏离;本地习惯不能悄悄覆盖它。
|
|
4
5
|
|
|
5
6
|
## 语言版本
|
|
6
7
|
|
|
@@ -24,17 +25,19 @@
|
|
|
24
25
|
|
|
25
26
|
## 命名
|
|
26
27
|
|
|
27
|
-
大小写、前缀、成员尾随 `_`、常量与枚举子的 `k` 前缀、命名空间与文件名的完整规则表,以及访问器、谓词、`Try`、映射函数、`current_` 前缀的语义角色,见 `references/naming/identifier-names.md`。本文只保留两处 Google 原文允许选择、而本项目需要统一的点:
|
|
28
|
+
大小写、前缀、成员尾随 `_`、常量与枚举子的 `k` 前缀、命名空间与文件名的完整规则表,以及访问器、谓词、`Try`、映射函数、`current_` 前缀的语义角色,见 `references/naming/01-identifier-names.md`。本文只保留两处 Google 原文允许选择、而本项目需要统一的点:
|
|
28
29
|
|
|
29
30
|
- 访问器、修改器 Google 允许 PascalCase 或 snake_case;本项目统一 PascalCase,同一 API 不混用两套。
|
|
30
31
|
- 测试名用 PascalCase 描述行为,例如 `RejectsOutOfRangeTask`;测试文件用 snake_case。
|
|
32
|
+
- 测试套件用被测类型加 `Test`,例如 `OrderBookTest`;suite 与用例两段都必须是合法 C++ 标识符,不能含下划线。
|
|
33
|
+
- 一个 `TEST` 只锁一个可观察行为;词数不设硬上限,以读得懂为准。
|
|
31
34
|
|
|
32
35
|
## 类型与声明
|
|
33
36
|
|
|
34
37
|
- 纯数据聚合用 `struct`;有不变量的类型用 `class`,不变量在构造函数和公共成员函数里维持。
|
|
35
38
|
- 单参数构造函数写 `explicit`;零参数构造函数不写。
|
|
36
39
|
- 默认成员初始化写在声明处(NSDMI),不要在构造函数初始化列表里重复。
|
|
37
|
-
-
|
|
40
|
+
- 普通对象默认用花括号初始化:`int count{};`、`std::vector<int> values{1, 2, 3};`。成员声明处也默认写 `{}`;拷贝初始化或 `auto` 推导用 `=`;`name = {{` 只用于多行聚合常量表,单个对象和空初始化仍写 `count{}`;容量构造等 `()` 例外见 `references/language/language-basics/02-initialization.md`。
|
|
38
41
|
- 类声明顺序:public → protected → private;类型与别名 → 静态常量 → 工厂 → 构造/赋值/析构 → 其他函数 → 数据成员。
|
|
39
42
|
- 用户声明的拷贝/移动/析构函数在头文件声明、在 `.cpp` 定义;只有零参数且默认实现足够时才写 `= default`。
|
|
40
43
|
- 查询成员函数标尾部 `const`;返回结果对象的 API 加 `[[nodiscard]]`,不要给所有非 `void` 函数机械添加。标注位置与 `friend` 的例外见 `references/language/value-types/07-nodiscard.md`。
|
|
@@ -42,6 +45,9 @@
|
|
|
42
45
|
- 禁止 C 风格强制转换;用 `static_cast`、`const_cast`、`reinterpret_cast`,C++20 起可用 `std::bit_cast`,低标准用 `std::memcpy`;并让 `clang-tidy` 的 `google-readability-casting` 兜底。
|
|
43
46
|
- `.cpp` 里不需要外部引用的 `constexpr` 直接放具名命名空间;仅一个函数使用时才用函数内 `static constexpr`;函数和类型仍放匿名命名空间。
|
|
44
47
|
- 一个 `.cpp` 只写一个匿名命名空间,公开函数的定义放在它后面。
|
|
48
|
+
- 只给本翻译单元用的函数和类型放唯一的匿名命名空间,不用文件作用域 `static` 隐藏函数:LLVM 与 Mozilla 的规范偏好 `static`,Chromium、C++ Core Guidelines SF.22、MISRA C++:2023 6.5.2 与 Abseil Tip 186 用匿名命名空间,本工作流跟后面这一组。
|
|
49
|
+
- 类的 `static` 成员函数没有内部链接,不能用来隐藏实现;函数内 `static` 对象只表示静态存储期,不表示内部链接。
|
|
50
|
+
- 另一个 `.cpp` 要调用的函数放在匿名命名空间外,并在配对头文件声明;不要为了两个函数再套 `detail` 或 `testing` 命名空间。
|
|
45
51
|
- 常量要出 `.cpp` 时按四步升:1) 其他翻译单元要的是算出来的答案,给类加查询、表留 `.cpp`;2) 要同一份坐标但仍不是公开契约,放组件 `src/` 的私有头;3) 对方必须写 `Board::kLines` 且这是类型的稳定事实,才用类内 `public static constexpr`;不依赖某个对象的公开常量则用命名空间 `inline constexpr`;4) 第二使用方复用同一套坐标时再抽共享组件。
|
|
46
52
|
|
|
47
53
|
## 函数
|
|
@@ -52,6 +58,7 @@
|
|
|
52
58
|
- 头文件里只把大约 10 行以内、确实适合内联的短函数就地定义;更长或需要完整定义的函数放 `.cpp`,避免头文件膨胀和重复编译。
|
|
53
59
|
- 虚函数不用默认参数;默认参数只用于非虚的便利接口。
|
|
54
60
|
- 尾置返回类型只在普通语法不可行或明显更难读时使用,例如返回类型依赖参数;不要为了风格统一全面改写。
|
|
61
|
+
- `if` / `for` / `while` / `do` 总是写花括号;`case` 的取舍见 `references/language/language-basics/05-expressions.md`。
|
|
55
62
|
- 只对自己的类型重载运算符;不重载 `&&`、`||`、`,`、一元 `&`;不定义用户字面量。
|
|
56
63
|
|
|
57
64
|
## 表达式与整数
|
|
@@ -61,20 +68,20 @@
|
|
|
61
68
|
- 混合优先级算术加显式括号,消除 `readability-math-missing-parentheses`。
|
|
62
69
|
- 内建整数默认用 `int`;需要确定宽度时用 `<cstdint>` 的 `std::int32_t`、`std::int64_t` 等(可省略 `std::` 前缀),可能到 2^31 的值用 64 位;`std::size_t` / `std::ptrdiff_t` 只用于大小和指针差。不要用无符号类型表达「这个值不会为负」。
|
|
63
70
|
- 封闭状态码用 `enum class`;本项目固定写 `: std::uint8_t` 并逐个写出枚举值(见 `references/language/value-types/04-enum.md`,有意偏离 Google 的默认 `int`),普通枚举用默认底层类型。枚举值总是带作用域:`OrderResult::kSuccess`。
|
|
64
|
-
- 枚举的尾随注释要么全写要么全不写;两种句式与禁止项见 `references/comments/comment-rules.md`「枚举项与 `struct` 短字段:两种句式」。
|
|
71
|
+
- 枚举的尾随注释要么全写要么全不写;两种句式与禁止项见 `references/comments/01-comment-rules.md`「枚举项与 `struct` 短字段:两种句式」。
|
|
65
72
|
- 浮点不用 `==` 比较计算结果;按量纲给容差,金额用最小货币单位的整数。
|
|
66
73
|
- 数值宽度、符号、整数角色、枚举、强类型、`const` 成员和下标访问的完整规则见 `references/language/value-types/01-width.md`、`02-signed.md`、`03-integer-roles.md`、`04-enum.md`、`05-strong-type.md`、`06-const-method.md`、`09-subscript.md`;初始化、RAII、移动和模板见 `references/language/language-basics/`。
|
|
67
74
|
|
|
68
75
|
## 异常
|
|
69
76
|
|
|
70
|
-
Google 原文不使用 C++ 异常;本工作流只在开发阶段的源码指导中按 `references/exceptions/
|
|
77
|
+
Google 原文不使用 C++ 异常;本工作流只在开发阶段的源码指导中按 `references/exceptions/01-exception-rules.md` 采用 C++ Core Guidelines 的 Error handling,插件运行时不编译、不执行这套策略。其余条目仍以 Google 原文为准。
|
|
71
78
|
|
|
72
|
-
异常消息的组织方式见 `references/exceptions/
|
|
79
|
+
异常消息的组织方式见 `references/exceptions/01-exception-rules.md` 的「消息」节。
|
|
73
80
|
|
|
74
81
|
## 输出与格式化
|
|
75
82
|
|
|
76
83
|
- 组合消息以 `std::format` 为主:`std::format("任务 {} 进入状态 {}", task, state)`。
|
|
77
|
-
- `std::
|
|
84
|
+
- 写到已有的 `std::ostream` 用 `<<`;没有流对象时,默认用 `std::print` 与 `std::println`,它们只在 C++23 且标准库支持时可用;见 `references/language/standard-library/06-format.md`。
|
|
78
85
|
- 简单字面量输出可以直接用 `std::cout << "..."`;需要拼接变量、对齐、数值格式时用 `std::format`。
|
|
79
86
|
- 列宽跟工程根 `.clang-format` 的 `ColumnLimit` 走;项目已声明 100 时按 100,模板资产不写 `ColumnLimit`。每级缩进 2 个空格,代码不用 tab;缩进与对齐由 `.clang-format` 统一,不在条文里另定一套。
|
|
80
87
|
- 浮点输出固定精度,整数不依赖隐式格式;`std::format` 的格式串写全。
|
|
@@ -82,6 +89,6 @@ Google 原文不使用 C++ 异常;本工作流只在开发阶段的源码指
|
|
|
82
89
|
## 注释
|
|
83
90
|
|
|
84
91
|
- 注释写意图、契约、取舍,不复述代码。
|
|
85
|
-
-
|
|
86
|
-
- 文件头、声明注释、实现注释与配置注释的格式见 `references/comments/
|
|
92
|
+
- 注释格式是本项目的第四处偏离:用 Doxygen 的 `///` 加 `@file`、`@brief`、`@param` 组织,Google 原文用 `//` 散文、文件头放 license boilerplate;本项目源码不带 license 头,分发或开源前补上。
|
|
93
|
+
- 文件头、声明注释、实现注释与配置注释的格式见 `references/comments/02-style.md`,写什么见 `01-comment-rules.md`。
|
|
87
94
|
- TODO 统一写 `// TODO: <阶段或 issue> - <要做什么>`;不要写 `@todo`、`FIXME` 或只有「参考」没有动作的占位。
|