@chengzhao-dev/dsh-cpp-workflow 0.6.19 → 0.7.1

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.
Files changed (163) hide show
  1. package/README.en.md +11 -26
  2. package/README.md +13 -28
  3. package/cordis.patch.yml +1 -1
  4. package/lib/client.js +33 -21
  5. package/lib/comment-policy.js +15 -6
  6. package/lib/index.js +1 -1
  7. package/lib/workspace.js +0 -15
  8. package/package.json +7 -7
  9. package/skills/building-cpp/SKILL.md +34 -21
  10. package/skills/building-cpp/assets/templates/projects/common/running-build.sh +5 -1
  11. package/skills/building-cpp/project/vscode/01-extensions.md +28 -0
  12. package/skills/building-cpp/project/vscode/02-settings.md +39 -0
  13. package/skills/building-cpp/references/build-script.md +14 -7
  14. package/skills/building-cpp/references/environment.md +8 -1
  15. package/skills/building-cpp/steps/01-build.md +8 -8
  16. package/skills/building-cpp/steps/02-tooling.md +17 -9
  17. package/skills/building-cpp/steps/03-check.md +16 -14
  18. package/skills/developing-cpp/SKILL.md +37 -25
  19. package/skills/developing-cpp/references/comments/comment-rules.md +181 -0
  20. package/skills/developing-cpp/references/comments/comment-style.md +80 -0
  21. package/skills/developing-cpp/references/comments/examples/declaration-class.md +66 -0
  22. package/skills/developing-cpp/references/comments/examples/declaration-constructor.md +22 -0
  23. package/skills/developing-cpp/references/comments/examples/declaration-enum.md +48 -0
  24. package/skills/developing-cpp/references/comments/examples/declaration-file.md +66 -0
  25. package/skills/developing-cpp/references/comments/examples/declaration-function.md +127 -0
  26. package/skills/developing-cpp/references/comments/examples/declaration-member.md +23 -0
  27. package/skills/developing-cpp/references/comments/examples/declaration-predicate.md +14 -0
  28. package/skills/developing-cpp/references/comments/examples/declaration-struct.md +18 -0
  29. package/skills/developing-cpp/references/comments/examples/implementation-body.md +64 -0
  30. package/skills/developing-cpp/references/comments/examples/implementation-todo.md +16 -0
  31. package/skills/developing-cpp/references/comments/glossary/comment-terms.md +93 -0
  32. package/skills/developing-cpp/references/design/examples/behavior-sentence.md +14 -0
  33. package/skills/developing-cpp/references/design/examples/coordinate-table.md +31 -0
  34. package/skills/developing-cpp/references/design/examples/header-place.md +26 -0
  35. package/skills/developing-cpp/references/design/examples/library-boundary.md +30 -0
  36. package/skills/developing-cpp/references/design/examples/library-split.md +7 -0
  37. package/skills/developing-cpp/references/design/examples/move-result.md +39 -0
  38. package/skills/developing-cpp/references/design/examples/preview-read.md +40 -0
  39. package/skills/developing-cpp/references/{project-design.md → design/project-design.md} +57 -5
  40. package/skills/developing-cpp/references/{change-model.md → editing/01-edit-operations.md} +5 -3
  41. package/skills/developing-cpp/references/{priority.md → editing/02-rule-priority.md} +2 -2
  42. package/skills/developing-cpp/references/exceptions/examples/throw-boundary.md +28 -0
  43. package/skills/developing-cpp/references/exceptions/examples/throw-build-mode.md +38 -0
  44. package/skills/developing-cpp/references/exceptions/examples/throw-catch.md +21 -0
  45. package/skills/developing-cpp/references/exceptions/examples/throw-destructor.md +21 -0
  46. package/skills/developing-cpp/references/exceptions/examples/throw-format.md +13 -0
  47. package/skills/developing-cpp/references/exceptions/examples/throw-message.md +16 -0
  48. package/skills/developing-cpp/references/exceptions/examples/throw-when.md +27 -0
  49. package/skills/developing-cpp/references/{exceptions.md → exceptions/throw-rules.md} +78 -23
  50. package/skills/developing-cpp/references/{glossary.md → language/glossary/language-terms.md} +2 -2
  51. package/skills/developing-cpp/references/language/language-basics/01-objects.md +15 -0
  52. package/skills/developing-cpp/references/language/language-basics/02-initialization.md +26 -0
  53. package/skills/developing-cpp/references/language/language-basics/03-deduction.md +12 -0
  54. package/skills/developing-cpp/references/language/language-basics/04-const.md +25 -0
  55. package/skills/developing-cpp/references/language/language-basics/05-expressions.md +23 -0
  56. package/skills/developing-cpp/references/language/language-basics/06-functions.md +15 -0
  57. package/skills/developing-cpp/references/language/language-basics/07-classes.md +15 -0
  58. package/skills/developing-cpp/references/language/language-basics/08-raii.md +15 -0
  59. package/skills/developing-cpp/references/language/language-basics/09-templates.md +26 -0
  60. package/skills/developing-cpp/references/language/language-basics/10-checklist.md +10 -0
  61. package/skills/developing-cpp/references/language/performance/01-measure.md +22 -0
  62. package/skills/developing-cpp/references/language/performance/02-complexity.md +13 -0
  63. package/skills/developing-cpp/references/language/performance/03-memory.md +14 -0
  64. package/skills/developing-cpp/references/language/performance/04-undefined-behavior.md +28 -0
  65. package/skills/developing-cpp/references/language/performance/05-pitfalls.md +17 -0
  66. package/skills/developing-cpp/references/language/performance/06-false-optimizations.md +13 -0
  67. package/skills/developing-cpp/references/language/performance/07-checklist.md +8 -0
  68. package/skills/developing-cpp/references/language/standard-library/01-containers.md +29 -0
  69. package/skills/developing-cpp/references/language/standard-library/02-access.md +14 -0
  70. package/skills/developing-cpp/references/language/standard-library/03-iteration.md +18 -0
  71. package/skills/developing-cpp/references/language/standard-library/04-algorithms.md +26 -0
  72. package/skills/developing-cpp/references/language/standard-library/05-string.md +13 -0
  73. package/skills/developing-cpp/references/language/standard-library/06-format.md +106 -0
  74. package/skills/developing-cpp/references/language/standard-library/07-time.md +13 -0
  75. package/skills/developing-cpp/references/language/standard-library/08-random.md +13 -0
  76. package/skills/developing-cpp/references/language/standard-library/09-files.md +14 -0
  77. package/skills/developing-cpp/references/language/standard-library/10-checklist.md +9 -0
  78. package/skills/developing-cpp/references/language/value-types/01-width.md +17 -0
  79. package/skills/developing-cpp/references/language/value-types/02-signed.md +26 -0
  80. package/skills/developing-cpp/references/language/value-types/03-integer-roles.md +38 -0
  81. package/skills/developing-cpp/references/language/value-types/04-enum.md +24 -0
  82. package/skills/developing-cpp/references/language/value-types/05-strong-type.md +17 -0
  83. package/skills/developing-cpp/references/language/value-types/06-const-method.md +22 -0
  84. package/skills/developing-cpp/references/language/value-types/07-nodiscard.md +15 -0
  85. package/skills/developing-cpp/references/language/value-types/08-comparison.md +10 -0
  86. package/skills/developing-cpp/references/language/value-types/09-subscript.md +15 -0
  87. package/skills/developing-cpp/references/language/value-types/10-float-bool.md +13 -0
  88. package/skills/developing-cpp/references/language/value-types/11-checklist.md +8 -0
  89. package/skills/developing-cpp/references/{layout.md → layout/01-source-layout.md} +10 -4
  90. package/skills/developing-cpp/references/{file-template.md → layout/02-file-template.md} +10 -4
  91. package/skills/developing-cpp/references/layout/03-declaration-order.md +138 -0
  92. package/skills/developing-cpp/references/naming/examples/naming-accessor.md +17 -0
  93. package/skills/developing-cpp/references/naming/examples/naming-basic.md +3 -0
  94. package/skills/developing-cpp/references/naming/examples/naming-current-prefix.md +19 -0
  95. package/skills/developing-cpp/references/naming/examples/naming-lookup.md +18 -0
  96. package/skills/developing-cpp/references/naming/examples/naming-lower-layer.md +39 -0
  97. package/skills/developing-cpp/references/naming/examples/naming-multiple-return.md +20 -0
  98. package/skills/developing-cpp/references/naming/examples/naming-player-value.md +16 -0
  99. package/skills/developing-cpp/references/naming/examples/naming-predicate.md +18 -0
  100. package/skills/developing-cpp/references/naming/examples/naming-rename.md +5 -0
  101. package/skills/developing-cpp/references/naming/examples/naming-responsibility.md +19 -0
  102. package/skills/developing-cpp/references/naming/examples/naming-role.md +21 -0
  103. package/skills/developing-cpp/references/naming/examples/naming-state-change.md +57 -0
  104. package/skills/developing-cpp/references/{naming.md → naming/identifier-names.md} +37 -9
  105. package/skills/developing-cpp/references/{google-cpp-style.md → style/google-cpp-style.md} +11 -10
  106. package/skills/developing-cpp/scripts/checking-comments.mjs +71 -14
  107. package/skills/developing-cpp/scripts/checking-declaration-order.mjs +357 -0
  108. package/skills/developing-cpp/steps/01-behavior.md +5 -5
  109. package/skills/developing-cpp/steps/02-boundary.md +10 -10
  110. package/skills/developing-cpp/steps/03-declaration.md +16 -14
  111. package/skills/developing-cpp/steps/04-implementation.md +21 -17
  112. package/skills/engineering/SKILL.md +87 -0
  113. package/skills/{configuring-cmake/assets/cmake → engineering/references/cmake/assets}/CompileSettings.cmake +2 -0
  114. package/skills/{building-cpp/references/linking.md → engineering/references/cmake/linking/01-libraries.md} +7 -48
  115. package/skills/engineering/references/cmake/linking/02-rpath.md +25 -0
  116. package/skills/engineering/references/cmake/linking/03-undefined-reference.md +20 -0
  117. package/skills/engineering/references/cmake/linking/04-inspect.md +21 -0
  118. package/skills/{configuring-cmake/references/modules.md → engineering/references/cmake/modules/functions.md} +8 -5
  119. package/skills/{configuring-cmake/references → engineering/references/cmake/standard}/cxx-standard.md +5 -4
  120. package/skills/engineering/references/cmake/standard/examples/standard.md +19 -0
  121. package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/README.md +9 -9
  122. package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++20/README.md +1 -1
  123. package/skills/engineering/references/cmake/targets/examples/glob.md +22 -0
  124. package/skills/engineering/references/cmake/targets/examples/rpath.md +24 -0
  125. package/skills/{configuring-cmake/examples/cmake.md → engineering/references/cmake/targets/examples/target.md} +2 -70
  126. package/skills/{configuring-cmake/references/cmake.md → engineering/references/cmake/targets/rules.md} +18 -11
  127. package/skills/{building-cpp/references/testing.md → engineering/references/cmake/testing/ctest.md} +11 -4
  128. package/skills/{building-cpp/references/encoding.md → engineering/references/formatting/01-encoding.md} +1 -1
  129. package/skills/engineering/references/formatting/02-clang-format.md +20 -0
  130. package/skills/engineering/references/formatting/03-clang-tidy.md +58 -0
  131. package/skills/engineering/references/formatting/04-clangd.md +90 -0
  132. package/skills/engineering/references/formatting/examples/clang-format.md +38 -0
  133. package/skills/engineering/references/formatting/examples/clang-tidy.md +43 -0
  134. package/skills/{building-cpp/examples/tooling.md → engineering/references/formatting/examples/clangd.md} +1 -80
  135. package/skills/{configuring-cmake → engineering}/steps/01-targets.md +6 -6
  136. package/skills/{configuring-cmake → engineering}/steps/02-modules.md +4 -4
  137. package/skills/{configuring-cmake → engineering}/steps/03-check.md +3 -2
  138. package/templates/cpp-workflow-project/SKILL.md +1 -1
  139. package/skills/building-cpp/references/static-analysis.md +0 -148
  140. package/skills/configuring-cmake/SKILL.md +0 -68
  141. package/skills/developing-cpp/examples/comments-declaration.md +0 -313
  142. package/skills/developing-cpp/examples/comments-implementation.md +0 -66
  143. package/skills/developing-cpp/examples/exceptions.md +0 -173
  144. package/skills/developing-cpp/examples/naming.md +0 -168
  145. package/skills/developing-cpp/examples/structure.md +0 -194
  146. package/skills/developing-cpp/references/comment-terms.md +0 -75
  147. package/skills/developing-cpp/references/comments.md +0 -223
  148. package/skills/developing-cpp/references/declaration-order.md +0 -118
  149. package/skills/developing-cpp/references/language.md +0 -123
  150. package/skills/developing-cpp/references/performance.md +0 -83
  151. package/skills/developing-cpp/references/standard-library.md +0 -138
  152. package/skills/developing-cpp/references/types.md +0 -154
  153. /package/skills/{configuring-cmake/assets/cmake → engineering/references/cmake/assets}/LinkSettings.cmake +0 -0
  154. /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++11/README.md +0 -0
  155. /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++11/cmake-snippet.cmake +0 -0
  156. /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++14/README.md +0 -0
  157. /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++14/cmake-snippet.cmake +0 -0
  158. /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++17/README.md +0 -0
  159. /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++17/cmake-snippet.cmake +0 -0
  160. /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++20/cmake-snippet.cmake +0 -0
  161. /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++23/README.md +0 -0
  162. /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++23/cmake-snippet.cmake +0 -0
  163. /package/skills/{building-cpp → engineering/references/formatting}/scripts/checking-encoding.mjs +0 -0
@@ -1,173 +0,0 @@
1
- # 正反例:异常
2
-
3
- 这些例子只用于开发阶段讨论源码写法。插件运行时不编译它们,也不执行它们。
4
-
5
- ## 什么时候抛
6
-
7
- **差**:用户输入格式错误是调用方每天都要处理的正常结果,却用异常表达,调用方被迫用 `try/catch` 做分支。
8
-
9
- ```cpp
10
- Task ParseTask(std::string_view line); // 任何格式错误都抛
11
- ```
12
-
13
- **好**:接口把「解析可能失败」写进返回类型;只有函数无法完成自己的任务、继续执行没有意义时才抛。
14
-
15
- ```cpp
16
- enum class ParseStatus { kOk, kEmptyLine, kMissingPriority, kBadPriority };
17
-
18
- struct ParseResult {
19
- std::optional<Task> task;
20
- ParseStatus status = ParseStatus::kEmptyLine;
21
- };
22
-
23
- [[nodiscard]] ParseResult TryParseTask(std::string_view line);
24
-
25
- /// @throws std::out_of_range index 不在 [0, size()) 内。
26
- [[nodiscard]] Task GetTask(int index) const;
27
- ```
28
-
29
- `TryParseTask` 把失败作为正常结果返回。`GetTask` 的前提是调用方给出有效下标;越界时函数无法返回一个任务,抛标准库已有的 `std::out_of_range`。如果越界其实是正常输入,应该改成返回 `std::optional<Task>`,而不是让调用方捕获异常;只有项目需要把这种越界和别处的 `out_of_range` 区分开时,才定义派生类。
30
-
31
- ## 异常类型与消息
32
-
33
- **差**:抛字符串,或把来源、条件和值揉成一句口语;调用方接到的 `what()` 既不稳定,也无法定位是哪一步失败。
34
-
35
- ```cpp
36
- throw "bad index";
37
- throw std::out_of_range{std::format("GetTask: 下标 {} 越界", index)};
38
- ```
39
-
40
- **好**:语义已经由标准类型给出时直接用标准类型;消息按「来源、条件、值、限度」组织,语言由项目约定,入口需要面向用户的前缀时只加前缀。
41
-
42
- ```cpp
43
- throw std::out_of_range{std::format(
44
- "GetTask: index is out of range (index={})", index)};
45
- ```
46
-
47
- 消息写操作和关键值;示例中的英文消息条件小写、句末不加句号,中文消息按中文标点,两者槽位顺序不变;有实际值才写括号,有分号时条件必须是小句。`std::format` 只是构造消息的方式,句式见 `references/exceptions.md` 的「消息」节。项目若定义 `Error` 基类,按该文件先定基类再派生,调用方捕获基类接住整族。
48
-
49
- ## Debug 与 Release 的三层合同
50
-
51
- **差**:同一个函数在 Debug 抛异常、在 Release 返回 false;调用方被迫按构建模式写两套分支。
52
-
53
- ```cpp
54
- #ifdef NDEBUG
55
- return false;
56
- #else
57
- throw std::out_of_range{row, col};
58
- #endif
59
- ```
60
-
61
- **好**:返回值在两种模式完全一致;需要异常时两种模式都抛,只有 `what()` 的详略不同。
62
-
63
- ```cpp
64
- // 返回值:失败是可恢复结果,两种模式都返回 false。
65
- [[nodiscard]] bool TryPlace(Position position, Player player) noexcept;
66
-
67
- // 越界:两种模式都抛 std::out_of_range;Debug 消息可带 file:line。
68
- void Place(Position position, Player player);
69
- ```
70
-
71
- 越界用标准库已有的 `std::out_of_range`,调用方不需要认识项目私有类型;只有调用方必须把它和其它 `out_of_range` 区分开时,才定义派生类并在 `what()` 里补位置信息。
72
-
73
- **差**:用 `assert` 承担 Release 也必须执行的 invariant 检查;`NDEBUG` 后检查消失。
74
-
75
- ```cpp
76
- assert(game.GetResult() == GameResult::kInProgress);
77
- ```
78
-
79
- **好**:两种模式都必须检查的 invariant 用 `Check`(项目保留时),失败时打印诊断并终止;只在开发期值得做、Release 可以省掉的自检用 `DebugCheck`。没有这两个宏时分别用 `assert` 与 `std::terminate`。
80
-
81
- ```cpp
82
- Check(game.GetResult() == GameResult::kInProgress, "Ai::Choose: game is already over");
83
- DebugCheck(index < history_.size(), "history index out of range");
84
- ```
85
-
86
- `Check` 不是 `[[noreturn]]`:条件成立时正常返回。`DebugCheck` 在 `NDEBUG` 后展开为空,参数不能带副作用。
87
-
88
- ## 捕获
89
-
90
- **差**:捕获一切后静默返回,状态可能已经半坏。
91
-
92
- ```cpp
93
- try {
94
- Load(path);
95
- } catch (...) {
96
- return;
97
- }
98
- ```
99
-
100
- **好**:只捕获当前层能处理的类型;不能处理的继续传播。`catch` 从派生到基类排序,`catch (...)` 只能最后兜底。
101
-
102
- ```cpp
103
- try {
104
- Load(path);
105
- } catch (const FileReadError& error) {
106
- return LoadResult::kNotFound;
107
- }
108
- ```
109
-
110
- ## 边界
111
-
112
- **差**:库代码捕获后打印并吞掉异常。
113
-
114
- ```cpp
115
- void AddTask(Task task) {
116
- try {
117
- tasks_.push_back(task);
118
- } catch (const std::exception& error) {
119
- std::cout << error.what() << "\n"; // 库打印,且吞掉异常
120
- }
121
- }
122
- ```
123
-
124
- **好**:库让异常向上传播,入口统一翻译成用户可读的诊断。
125
-
126
- ```cpp
127
- int main() {
128
- try {
129
- return Run(std::cin, std::cout);
130
- } catch (const std::exception& error) {
131
- std::cerr << std::format("错误: {}\n", error.what());
132
- return 1;
133
- }
134
- }
135
- ```
136
-
137
- 退出码由项目入口合同决定;这里只示范「捕获、打印、非零返回」。
138
-
139
- ## 析构
140
-
141
- **差**:
142
-
143
- ```cpp
144
- ~Writer() {
145
- if (file_) file_->Flush(); // 失败时抛?析构中抛会终止程序
146
- }
147
- ```
148
-
149
- **好**:
150
-
151
- ```cpp
152
- ~Writer() {
153
- if (file_) {
154
- file_->Flush(); // 忽略失败:析构不抛,错误由 Close() 报告
155
- }
156
- }
157
- ```
158
-
159
- 需要报告关闭失败时,提供显式 `Close()` 返回结果,析构只做兜底。
160
-
161
- ## `std::format` 与 `std::print`
162
-
163
- C++23 且标准库支持 `std::print` 时,程序入口或 CLI 可以写:
164
-
165
- ```cpp
166
- std::print("任务: {}\n", task.name);
167
- ```
168
-
169
- 库代码、以及需要兼容 C++11–C++20 的代码仍写:
170
-
171
- ```cpp
172
- std::cout << std::format("任务: {}\n", task.name);
173
- ```
@@ -1,168 +0,0 @@
1
- # 正反例:命名
2
-
3
- ## 基本规则
4
-
5
- 大小写、前缀与成员尾随 `_` 的规则表见 `references/naming.md`;下面只收需要语境才能判断的例子。
6
-
7
- ## 访问器
8
-
9
- **差**:
10
-
11
- ```cpp
12
- Order current_order() const; // 成员名直接当函数名
13
- void set_current_order(Order order);
14
- ```
15
-
16
- **好**:
17
-
18
- ```cpp
19
- [[nodiscard]] Order GetCurrentOrder() const;
20
- void AdvanceOrder(); // 领域动作,不是 setter
21
- ```
22
-
23
- 查询统一 `Get` + PascalCase;修改用领域动词。只有真正需要替换值时,才考虑 `Set` 形式,并说明为什么不是动作。
24
-
25
- ## 玩家与标记
26
-
27
- **差**:
28
-
29
- ```cpp
30
- Player OtherPlayer(Player player); // 另一端是谁不明显
31
- CellState CellStateOf(Player player); // 从什么换到什么没写全
32
- ```
33
-
34
- **好**:
35
-
36
- ```cpp
37
- [[nodiscard]] constexpr Player GetOpponent(Player player) noexcept;
38
- [[nodiscard]] constexpr CellState PlayerToCellState(Player player) noexcept;
39
- ```
40
-
41
- 玩家互换用 `GetOpponent`;玩家换成格子取值用 `PlayerToCellState`。查询名与返回类型一致:返回 `Player` 写 `GetCurrentPlayer`,返回 `CellState` 写 `GetCurrentMark`。
42
-
43
- ## 语义角色
44
-
45
- **差**:
46
-
47
- ```cpp
48
- bool Check(int id) const; // Check 什么?
49
- Result DoSomething(Task task); // 做什么?
50
- TaskState To(TaskState state); // 从什么到什么?
51
- ```
52
-
53
- **好**:
54
-
55
- ```cpp
56
- [[nodiscard]] bool Contains(int id) const;
57
- [[nodiscard]] ParseResult TryParse(std::string_view text);
58
- [[nodiscard]] TaskState StatusCodeToTaskState(int code);
59
- ```
60
-
61
- - 判断用 `Is` / `Has`。
62
- - 可能失败但不抛用 `Try`。
63
- - 映射名字含两端角色。
64
-
65
- ## `current_` 前缀
66
-
67
- **差**:
68
-
69
- ```cpp
70
- Order current_order_; // 只有一个订单时,current 是噪音
71
- ```
72
-
73
- **好**:
74
-
75
- ```cpp
76
- Order order_;
77
- ```
78
-
79
- **该用**:
80
-
81
- ```cpp
82
- Order current_order_; // 有 current_order_ 与 next_order_,不写会混
83
- ```
84
-
85
- ## 名字与职责
86
-
87
- **差**:
88
-
89
- ```cpp
90
- class DataManager {}; // 管什么?
91
- class StringHelper {}; // 为什么是类?
92
- ```
93
-
94
- **好**:
95
-
96
- ```cpp
97
- class Warehouse {}; // 持有库存,维持不变量
98
- namespace string_utils { // 一组自由函数
99
- [[nodiscard]] std::string Trim(std::string_view text);
100
- }
101
- ```
102
-
103
- 避免 `Manager`、`Helper`、`Util`;名字要能回答「它负责什么」。一组无状态函数用命名空间,不要造类。
104
-
105
- ## 查找与读取
106
-
107
- **差**:
108
-
109
- ```cpp
110
- [[nodiscard]] Task GetTask(std::string_view name) const; // 可能找不到时返回什么?
111
- [[nodiscard]] bool FindTask(std::string_view name) const; // 返回 bool 却叫 Find
112
- ```
113
-
114
- **好**:
115
-
116
- ```cpp
117
- [[nodiscard]] const Task& GetTask(std::string_view name) const; // 找不到抛 TaskNotFoundError
118
- [[nodiscard]] const Task* FindTask(std::string_view name) const; // 找不到返回 nullptr
119
- [[nodiscard]] bool HasTask(std::string_view name) const; // 只回答存在性
120
- ```
121
-
122
- `Get` 表示结果一定存在(否则抛出声明过的领域异常),`Find` 表示可能找不到,`Has` / `Contains` 只回答是否存在。不要用 `Find` 返回 `bool`。
123
-
124
- ## 谓词与 Lambda
125
-
126
- **差**:
127
-
128
- ```cpp
129
- bool valid(int value);
130
- auto check = [](const Task& task) { return task.done; };
131
- ```
132
-
133
- **好**:
134
-
135
- ```cpp
136
- [[nodiscard]] bool IsValid(int value);
137
- auto is_done = [](const Task& task) { return task.done; };
138
- auto has_owner = [](const Task& task) { return !task.owner.empty(); };
139
- ```
140
-
141
- 返回 `bool` 的自由函数用 `Is` / `Has` / `Can` / `Should` 开头;Lambda 用 snake_case 的 `is_` / `has_` / `can_`,读起来像条件。
142
-
143
- ## 多返回值
144
-
145
- **差**:
146
-
147
- ```cpp
148
- bool Parse(std::string_view text, int* out_value); // 出参 + 返回 bool
149
- ```
150
-
151
- **好**:
152
-
153
- ```cpp
154
- struct ParseResult {
155
- bool ok{false};
156
- int value{0};
157
- };
158
-
159
- [[nodiscard]] ParseResult TryParse(std::string_view text);
160
- ```
161
-
162
- 需要同时表达成功与结果时,用带字段的结构体或 `std::optional`,不要用输出参数加布尔返回。
163
-
164
- ## 改名检查
165
-
166
- - 搜全部调用点:声明、定义、测试、注释、文档。
167
- - 先改声明,再改定义,再改调用点;每一步都能编译。
168
- - 公共 API 改名保留旧名转发时,注释写弃用期限;不要静默删除。
@@ -1,194 +0,0 @@
1
- # 正反例:行为与边界
2
-
3
- ## 行为句
4
-
5
- **差**:实现一个 `TaskList` 类,用 `std::vector` 保存任务,支持添加、删除、按优先级排序,并在终端打印表格。
6
-
7
- 问题:把类型、容器、实现步骤混进行为;无法验收「表格」长什么样。
8
-
9
- **好**:
10
-
11
- ```text
12
- 给定一组任务(每行「优先级 名称」),程序按优先级从高到低输出名称;
13
- 输入为空时输出「没有任务」并返回 0;某行格式非法时输出行号并返回 1。
14
- ```
15
-
16
- 可以写成测试:输入三行,断言输出顺序;输入空,断言提示与退出码;输入坏行,断言行号。
17
-
18
- ## 边界
19
-
20
- **差**:库函数直接打印,程序只调用它。
21
-
22
- ```cpp
23
- // task_list.cpp(库)
24
- void TaskList::Print() const {
25
- for (const auto& task : tasks_) {
26
- std::cout << task.priority << " " << task.name << "\n";
27
- }
28
- }
29
- ```
30
-
31
- 问题:库依赖终端,测试要捕获 `std::cout`,GUI 或文件输出无法复用。
32
-
33
- **好**:库返回数据,程序决定怎么显示。
34
-
35
- ```cpp
36
- // task_list.h(库)
37
- [[nodiscard]] std::vector<Task> SortedByPriority() const;
38
-
39
- // app/cli.cpp(程序)
40
- void PrintTasks(const std::vector<Task>& tasks, std::ostream& out) {
41
- for (const auto& task : tasks) {
42
- out << std::format("{} {}\n", task.priority, task.name);
43
- }
44
- }
45
- ```
46
-
47
- 测试直接断言 `SortedByPriority()`;输出格式由程序测试负责。
48
-
49
- ## 头文件位置
50
-
51
- **差**:
52
-
53
- ```text
54
- src/task_list.h // 公共头藏在 src,测试无法只依赖公共接口
55
- src/detail.h // 私有头被测试直接包含
56
- ```
57
-
58
- ```cpp
59
- #include "src/task_list.h" // 从仓库根写到了实现目录
60
- ```
61
-
62
- **好**:
63
-
64
- ```text
65
- task_list/include/task_list.h // 公共头
66
- task_list/src/task_list.cpp // 实现
67
- task_list/src/detail.h // 私有头,只被本组件包含
68
- ```
69
-
70
- ```cpp
71
- #include "task_list.h" // 公共头按裸文件名包含
72
- ```
73
-
74
- 构建时把仓库根加入 include 搜索路径;不要用 `#include "../task_list/include/task_list.h"` 这种随文件位置漂移的写法。
75
-
76
- ## 单文件何时拆库
77
-
78
- **先不拆**:一个 `main.cpp`,辅助函数在匿名命名空间,只有程序自己用。
79
-
80
- **该拆**:测试需要直接调用排序逻辑;或第二个程序要复用同一逻辑;或逻辑已经超过一个屏幕。
81
-
82
- 拆的时候先定公共头(只暴露 `SortedByPriority`),把实现移到 `src/`,行为句不变,测试先绿再改构建。
83
-
84
- ## 固定坐标用表
85
-
86
- **差**:每次判断成线,都用循环把八条线的坐标重新拼一遍;行、列、对角线混在一个循环里,读者要跟着跑两遍才知道哪八条。
87
-
88
- ```cpp
89
- // 差:用循环现写八条线,坐标藏在控制流里。
90
- std::vector<std::vector<Position>> BuildLines() {
91
- std::vector<std::vector<Position>> lines;
92
- for (int row = 0; row < kRows; ++row) { /* ... */ }
93
- // 列与两条对角线再各写一段
94
- return lines;
95
- }
96
- ```
97
-
98
- **好**:固定坐标写成具名命名空间里的 `constexpr` 常量表,`HasLine` 与 `PreviewWin` 共用,所以不放进任何一个函数体;格子用 `at()`,整条线 `all_of`、八条线 `any_of`,函数不标 `noexcept`。
99
-
100
- ```cpp
101
- // board.cpp 的 namespace tictactoe:行、列、对角线三组,坐标固定。
102
- constexpr std::array<std::array<Position, 3>, 8> kLines = {{
103
- {{{0, 0}, {0, 1}, {0, 2}}}, // 行 0
104
- {{{1, 0}, {1, 1}, {1, 2}}}, // 行 1
105
- {{{2, 0}, {2, 1}, {2, 2}}}, // 行 2
106
- {{{0, 0}, {1, 0}, {2, 0}}}, // 列 0
107
- {{{0, 1}, {1, 1}, {2, 1}}}, // 列 1
108
- {{{0, 2}, {1, 2}, {2, 2}}}, // 列 2
109
- {{{0, 0}, {1, 1}, {2, 2}}}, // 对角线
110
- {{{0, 2}, {1, 1}, {2, 0}}}, // 反对角线
111
- }};
112
- ```
113
-
114
- 每个格子要分别处理时仍写 `row` / `col` 循环,例如收集空格、终端打印。
115
-
116
- ## 落子结果不能丢,也不要重建一手
117
-
118
- **差**:调用方已经拿着一手,`Game` 又用 `GetCurrentPlayer()` 按位置重建一手;`Board::Place` 的 `[[nodiscard]]` 结果还被 `static_cast<void>` 丢掉。重建的玩家与调用方给的不一致时,历史会记下棋盘没有接受的落子。
119
-
120
- ```cpp
121
- if (!board_.GetCell(move.position).IsEmpty()) {
122
- return MoveResult::kOccupied;
123
- }
124
- const Move rebuilt{move.position, GetCurrentPlayer()};
125
- static_cast<void>(board_.Place(rebuilt.position, PlayerToCellState(rebuilt.player)));
126
- history_.push_back(rebuilt);
127
- return MoveResult::kPlaced;
128
- ```
129
-
130
- **好**:`Game::Place` 直接接收一手,先确认是否轮到该玩家,然后判终局,最后交给 `Board::Place`;坐标范围由棋盘检查。按 `Cell::SetStateResult` 分支写历史并换方。`kNoInput` 说明棋盘收到了空取值,抛 `std::logic_error`;`kFinished` 仍由 `Game` 判,因为棋盘不知道整局已经结束。
131
-
132
- ```cpp
133
- Game::MoveResult Game::Place(Move move) {
134
- // 先确认轮到谁,再判终局;坐标范围由 Board::Place 检查。
135
- if (move.player != current_player_) {
136
- throw std::logic_error{"Game::Place: move player is not the current player"};
137
- }
138
- if (board_.GetGameResult() != GameResult::kInProgress) {
139
- return MoveResult::kFinished;
140
- }
141
- switch (board_.Place(move.position, PlayerToCellState(move.player))) {
142
- case Cell::SetStateResult::kUpdated:
143
- history_.push_back(move);
144
- current_player_ = GetOpponent(current_player_);
145
- return MoveResult::kPlaced;
146
- case Cell::SetStateResult::kOccupied:
147
- return MoveResult::kOccupied;
148
- case Cell::SetStateResult::kNoInput:
149
- throw std::logic_error{"Game::Place: board rejected the move"};
150
- }
151
- throw std::logic_error{"Game::Place: unhandled Cell::SetStateResult"};
152
- }
153
- ```
154
-
155
- ## 推演一步读原数据
156
-
157
- **差**:先复制整个棋盘,再在副本上落子,最后判断成线;调用方看不出副本只是中间量,还要重复处理落子失败。
158
-
159
- ```cpp
160
- Board hypothetical = *this;
161
- const Cell::SetStateResult placed = hypothetical.Place(position, state);
162
- if (placed != Cell::SetStateResult::kUpdated) {
163
- return WinPreviewResult::kInvalidPlace;
164
- }
165
- return hypothetical.HasLine(state) ? WinPreviewResult::kWouldWin
166
- : WinPreviewResult::kWouldNotWin;
167
- ```
168
-
169
- **好**:判断「这一格换成某种棋子后会不会成线」不需要第二份棋盘。越界、空格、已占用交给 `CheckPlaceable` 统一返回原因;通过之后只在包含该坐标的线上把这一格算作 `state`,其余格子读原棋盘。
170
-
171
- ```cpp
172
- WinPreviewResult Board::PreviewWin(Position position, CellState state) const {
173
- if (CheckPlaceable(position, state) != Cell::SetStateResult::kUpdated) {
174
- return WinPreviewResult::kInvalidPlace;
175
- }
176
-
177
- const auto matches = [&](Position cell) {
178
- if (cell.row == position.row && cell.col == position.col) {
179
- return true;
180
- }
181
- return cells_.at(cell.row).at(cell.col) == state;
182
- };
183
-
184
- const bool completes = std::ranges::any_of(kLines, [&](const auto& line) {
185
- const bool contains = std::ranges::find_if(line, [&](Position cell) {
186
- return cell.row == position.row && cell.col == position.col;
187
- }) != line.end();
188
- return contains && std::ranges::all_of(line, matches);
189
- });
190
- return completes ? WinPreviewResult::kWouldWin : WinPreviewResult::kWouldNotWin;
191
- }
192
- ```
193
-
194
- 先用 `IsInRange` 拒绝越界,再用 `at()` 读取;失败路径不碰数组,`noexcept` 也就不再成立。
@@ -1,75 +0,0 @@
1
- # 注释术语与用词
2
-
3
- 本文件是 `comments.md` 的附录,只在判不准用词、改写类型注释或核对书稿摘录时打开;它不随 `comments.md` 一起加载,也不重复句式、位置和长度规则。
4
-
5
- ## 一条概念只认一个词
6
-
7
- | 概念 | 首选 | 避免 | 说明 |
8
- | --- | --- | --- | --- |
9
- | `Position` 类型 | 行列坐标 | 位置类型、坐标点、有序数对 | 类型名和参数名保持 `Position`、`position` |
10
- | 棋盘上的点 | 位置 | 坐标点、格子坐标 | 说语义位置时用「位置」 |
11
- | `Cell` | 格子 | 单元、方格、棋子 | 「棋子」只指对局中的 X 或 O |
12
- | `Board` | 棋盘 | 盘面、棋局 | 「棋局」只指一局对局 |
13
- | `CellState` 类型 | 格子状态 | 格子的状态、格子状态值 | 类型注释写「格子状态。」 |
14
- | `CellState` 的取值 | 格子为空、格子为 X、格子为 O | 状态为空、尚未设置、状态为 X | 每行都以「格子」开头,写完整状态 |
15
- | `SetState` 的动作 | 更新状态 | 写入格子、放置、落子 | `Cell::SetState` 更新一格;`Board::Place` 把取值转给对应格子 |
16
- | `SetStateResult` 类型 | 格子状态变更结果 | 格子状态的变更结果、放置结果 | 类型注释写「格子状态变更结果。」 |
17
- | `SetStateResult` 的取值 | 主体 + 状态:kNoInput、kOccupied、kUpdated | kEmptyInput、kOccupied、kUpdated | 每个取值两个词以内,失败写原因、成功写结果,不重复 Rejected |
18
- | `MoveResult` 类型 | 一次落子的结果 | 落子结果、放置结果 | 类型注释写「一次落子的结果。」 |
19
- | `MoveResult` 的取值 | 落子失败/成功:原因 | 已落子、未落子、成功、失败 | 先写主体与结果,再写原因 |
20
- | `GameResult` 类型 | 对局结果 | 对局的结果、游戏结果、一局的结果 | 类型注释写「对局结果。」 |
21
- | `GameResult` 的取值 | 对局尚未结束、对局由 X 获胜、对局由 O 获胜、对局以平局结束 | 尚未结束、X 成线、O 成线、棋盘已满 | 每行都以「对局」开头,写完整结果 |
22
- | `Player` 类型 | 玩家 | 对局的一方、落子的一方 | 类型注释写「玩家。」,取值由枚举项说明 |
23
- | `Player` 的取值 | 玩家为 X:先手、玩家为 O:后手 | 先手、后手、X 方、O 方 | 每行以「玩家为」开头,再补先后手 |
24
- | `Reset`/`Clear` 的动作 | 重置为空、清空 | 清空值、收回 `kEmpty` | 逐格重置,不叫「清空值」 |
25
- | `GetCell` 的返回 | 格子副本 | 格子、引用 | 按值返回必须说「副本」 |
26
- | 坐标越界 | 坐标越界时抛出 `std::out_of_range` | 检查越界 | 只用于会抛的校验函数 |
27
- | `GetGameResult` | 当前结果 | 现在的结果、最新结果 | 结果是棋盘的快照 |
28
- | `row`、`col` | 行号、列号 | 行、列 | 成员注释写「默认值为 0」,类注释不重复 |
29
-
30
- ## 位置与坐标
31
-
32
- - `Position` 是类型名,中文叫「行列坐标」;参数名用 `position`,不改成 `pos`。
33
- - 「棋盘上的一个位置」指语义位置;「一行一列的坐标」指具体值。两者不混成「坐标点」,也不写成「有序数对」。
34
- - 成员注释写「行号,默认值为 0」「列号,默认值为 0」;类型是 `int`,可为负,不写「从 0 起算」;类注释不再重复默认值,否则和成员注释冲突。
35
-
36
- ## 格子与棋盘
37
-
38
- - `Cell` 只说「格子」;「棋盘上的格子」只在需要强调归属时用,类型注释本身不写棋盘。
39
- - `Board` 只说「棋盘」;`cells_` 说「格子数组」,不写「存储格子的数组」这种复述。
40
- - 「棋子」只指对局中的 X 或 O;格子的内部值叫「状态」,不叫「棋子」。
41
- - `SetStateResult` 的三种结果说「格子状态已变更」「格子状态未变更」,不说「成功」「失败」;拒绝覆盖说「已有状态」,不说「占据」。
42
- - `SetStateResult` 的三个取值都用「主体 + 状态」,每个两个词:失败值写原因(`kNoInput`、`kOccupied`),成功值写结果(`kUpdated`),不再堆叠 `Rejected`。
43
-
44
- ## 玩家
45
-
46
- - `Player` 的类型注释与枚举项统一用「玩家」:类型写「玩家。」,取值写「玩家为 X:先手」「玩家为 O:后手」。
47
- - 不用「对局的一方」「落子的一方」指 `Player`;「一方」只在对局叙述里说明轮换关系。
48
-
49
- ## 更新与写入
50
-
51
- - `Cell::SetState` 的动作统一写「更新状态」:`Cell::SetState` 更新一格,`Board::Place` 把一次落子的位置与取值转给对应格子。
52
- - 「写入」只用于数组、文件、流这类输出目标;不写「写入一个格子」「把状态写入格子」。
53
- - 「设置」表示设置一格状态;「更新」表示这次动作;「已有状态」表示拒绝覆盖。三者不互换。
54
- - `Reset` 说「重置为空」或「重置为 `kEmpty`」,不写「清空值」「删除状态」;`Board::Clear` 说「清空棋盘」,逐格调用 `Cell::Reset`。
55
-
56
- ## 返回值与副本
57
-
58
- - `GetCell` 按值返回:第一句和 `@return` 都写「格子副本」,不写「该位置的格子」,否则读者会以为拿到引用。
59
- - `GetState` 返回枚举值,写「当前状态」,不写「状态值」。
60
- - `GetGameResult` 写「当前结果」,不写「现在的结果」。
61
- - 会抛的查询先写抛出条件,再写 `@throws`;`@param` 写「待检查/读取/更新的坐标」,不把「棋盘内」当前置条件。
62
-
63
- ## 初始状态与特殊成员
64
-
65
- - 类注释拥有初始状态与生命周期;默认构造函数只写「建立初始状态」,不再重复具体状态值。
66
- - 数据成员只写成员自身:`row`、`col` 写行号/列号与默认值;`state_` 写「格子当前的状态」,不重复类注释的初始值。
67
- - 文件头只写 `@file`、`@brief` 与文件组成;不写作者、版权年份、变更史。
68
-
69
- ## 依据
70
-
71
- - [Google 中文 C++ 风格指南·注释](https://zh-google-styleguide.readthedocs.io/en/stable/google-cpp-styleguide/comments.html):类讲用途,函数讲输入输出,不翻译代码。
72
- - [LeviLamina C++ 风格指南](https://docs.levimc.org/LeviLamina/zh/maintainer_guides/cpp_style_guide/):类注释至少简介与公开成员,函数注释含参数、返回值与异常。
73
- - [OpenHarmony C++ 语言编程规范·注释](https://www.bookstack.cn/read/openharmony-1.0-zh-cn/contribute-OpenHarmony-cpp-coding-style-guide.md):按需、简洁、无二义性,不重复代码信息。
74
-
75
- 判不准时先查本表,再查 `comments.md` 的句式与位置;改完运行 `node "<skill-dir>/scripts/checking-comments.mjs" --root <项目根>`。