@chengzhao-dev/dsh-cpp-workflow 0.6.20 → 0.7.3
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 +23 -32
- package/README.md +24 -33
- package/cordis.patch.yml +6 -6
- package/lib/client.js +35 -23
- package/lib/comment-policy.js +15 -6
- package/lib/index.js +45 -37
- package/lib/workspace.js +10 -32
- package/package.json +7 -7
- package/skills/building-cpp/SKILL.md +24 -24
- package/skills/building-cpp/assets/templates/projects/common/running-build.sh +5 -1
- package/skills/building-cpp/project/vscode/01-extensions.md +6 -0
- package/skills/building-cpp/project/vscode/02-settings.md +7 -1
- package/skills/building-cpp/references/build-script.md +10 -3
- package/skills/building-cpp/references/environment.md +8 -1
- package/skills/building-cpp/scripts/checking-environment.mjs +11 -13
- package/skills/building-cpp/steps/01-build.md +5 -4
- package/skills/building-cpp/steps/02-tooling.md +10 -10
- package/skills/building-cpp/steps/03-check.md +13 -11
- package/skills/developing-cpp/SKILL.md +39 -40
- package/skills/developing-cpp/references/comments/comment-rules.md +195 -0
- package/skills/developing-cpp/references/comments/comment-style.md +76 -0
- package/skills/developing-cpp/references/comments/examples/declaration-class.md +65 -0
- package/skills/developing-cpp/references/comments/examples/declaration-constructor.md +22 -0
- package/skills/developing-cpp/references/comments/examples/declaration-enum.md +48 -0
- package/skills/developing-cpp/{source/07-comments → references/comments}/examples/declaration-file.md +5 -5
- package/skills/developing-cpp/references/comments/examples/declaration-function.md +135 -0
- package/skills/developing-cpp/references/comments/examples/declaration-member.md +23 -0
- package/skills/developing-cpp/{source/07-comments → references/comments}/examples/declaration-predicate.md +3 -2
- package/skills/developing-cpp/references/comments/examples/declaration-struct.md +18 -0
- package/skills/developing-cpp/references/comments/examples/implementation-body.md +81 -0
- package/skills/developing-cpp/references/comments/glossary/comment-terms.md +92 -0
- package/skills/developing-cpp/{source/03-design → references/design}/examples/coordinate-table.md +3 -3
- package/skills/developing-cpp/{source/03-design → references/design}/examples/header-place.md +1 -1
- package/skills/developing-cpp/references/design/examples/move-result.md +48 -0
- package/skills/developing-cpp/references/design/examples/preview-read.md +36 -0
- package/skills/developing-cpp/references/design/project-design.md +160 -0
- package/skills/developing-cpp/{source/01-editing → references/editing}/01-edit-operations.md +5 -3
- package/skills/developing-cpp/{source/01-editing → references/editing}/02-rule-priority.md +2 -2
- package/skills/developing-cpp/{source/08-exceptions → references/exceptions}/examples/throw-build-mode.md +4 -4
- package/skills/developing-cpp/{source/08-exceptions → references/exceptions}/examples/throw-message.md +10 -3
- package/skills/developing-cpp/references/exceptions/throw-rules.md +216 -0
- package/skills/developing-cpp/{source/05-language → references/language}/glossary/language-terms.md +2 -2
- package/skills/developing-cpp/{source/05-language/01-language-basics → references/language/language-basics}/01-objects.md +8 -2
- package/skills/developing-cpp/{source/05-language/01-language-basics → references/language/language-basics}/02-initialization.md +8 -1
- package/skills/developing-cpp/{source/05-language/01-language-basics → references/language/language-basics}/03-deduction.md +6 -0
- package/skills/developing-cpp/references/language/language-basics/04-const.md +25 -0
- package/skills/developing-cpp/references/language/language-basics/05-expressions.md +28 -0
- package/skills/developing-cpp/references/language/language-basics/06-functions.md +17 -0
- package/skills/developing-cpp/{source/05-language/01-language-basics → references/language/language-basics}/07-classes.md +7 -1
- package/skills/developing-cpp/references/language/language-basics/08-raii.md +15 -0
- package/skills/developing-cpp/{source/05-language/01-language-basics → references/language/language-basics}/09-templates.md +6 -0
- package/skills/developing-cpp/{source/05-language/01-language-basics → references/language/language-basics}/10-checklist.md +2 -1
- package/skills/developing-cpp/references/language/performance/01-measure.md +22 -0
- package/skills/developing-cpp/{source/05-language/04-performance → references/language/performance}/02-complexity.md +6 -0
- package/skills/developing-cpp/{source/05-language/04-performance → references/language/performance}/03-memory.md +7 -1
- package/skills/developing-cpp/{source/05-language/04-performance → references/language/performance}/04-undefined-behavior.md +9 -1
- package/skills/developing-cpp/{source/05-language/04-performance → references/language/performance}/05-pitfalls.md +6 -0
- package/skills/developing-cpp/references/language/performance/06-false-optimizations.md +13 -0
- package/skills/developing-cpp/{source/05-language/03-standard-library → references/language/standard-library}/01-containers.md +6 -0
- package/skills/developing-cpp/references/language/standard-library/02-access.md +14 -0
- package/skills/developing-cpp/{source/05-language/03-standard-library → references/language/standard-library}/03-iteration.md +6 -0
- package/skills/developing-cpp/{source/05-language/03-standard-library → references/language/standard-library}/04-algorithms.md +6 -0
- package/skills/developing-cpp/{source/05-language/03-standard-library → references/language/standard-library}/05-string.md +6 -0
- package/skills/developing-cpp/references/language/standard-library/06-format.md +114 -0
- package/skills/developing-cpp/references/language/standard-library/07-time.md +13 -0
- package/skills/developing-cpp/references/language/standard-library/08-random.md +13 -0
- package/skills/developing-cpp/references/language/standard-library/09-files.md +14 -0
- package/skills/developing-cpp/{source/05-language/02-value-types → references/language/value-types}/01-width.md +6 -0
- package/skills/developing-cpp/{source/05-language/02-value-types → references/language/value-types}/02-signed.md +7 -1
- package/skills/developing-cpp/references/language/value-types/03-integer-roles.md +38 -0
- package/skills/developing-cpp/references/language/value-types/04-enum.md +24 -0
- package/skills/developing-cpp/{source/05-language/02-value-types → references/language/value-types}/06-const-method.md +5 -0
- package/skills/developing-cpp/{source/05-language/02-value-types → references/language/value-types}/07-nodiscard.md +6 -0
- package/skills/developing-cpp/references/language/value-types/08-comparison.md +10 -0
- package/skills/developing-cpp/{source/05-language/02-value-types → references/language/value-types}/09-subscript.md +2 -1
- package/skills/developing-cpp/{source/05-language/02-value-types → references/language/value-types}/10-float-bool.md +6 -0
- package/skills/developing-cpp/{source/05-language/02-value-types → references/language/value-types}/11-checklist.md +1 -1
- package/skills/developing-cpp/{source/04-layout → references/layout}/01-source-layout.md +8 -3
- package/skills/developing-cpp/{source/04-layout → references/layout}/02-file-template.md +14 -8
- package/skills/developing-cpp/references/layout/03-declaration-order.md +141 -0
- package/skills/developing-cpp/references/naming/examples/naming-accessor.md +28 -0
- package/skills/developing-cpp/references/naming/examples/naming-basic.md +3 -0
- package/skills/developing-cpp/references/naming/examples/naming-local.md +45 -0
- package/skills/developing-cpp/{source/06-naming → references/naming}/examples/naming-lookup.md +1 -1
- package/skills/developing-cpp/references/naming/examples/naming-lower-layer.md +46 -0
- package/skills/developing-cpp/references/naming/examples/naming-player-value.md +15 -0
- package/skills/developing-cpp/{source/06-naming → references/naming}/examples/naming-predicate.md +1 -1
- package/skills/developing-cpp/{source/06-naming → references/naming}/examples/naming-role.md +1 -1
- package/skills/developing-cpp/references/naming/examples/naming-state-change.md +70 -0
- package/skills/developing-cpp/references/naming/glossary/naming-terms.md +23 -0
- package/skills/developing-cpp/references/naming/glossary/position.md +23 -0
- package/skills/developing-cpp/references/naming/glossary/square.md +47 -0
- package/skills/developing-cpp/references/naming/identifier-names.md +109 -0
- package/skills/developing-cpp/{source/02-style/01-google-cpp-style.md → references/style/google-cpp-style.md} +14 -13
- package/skills/developing-cpp/scripts/checking-comments.mjs +80 -15
- package/skills/developing-cpp/scripts/checking-declaration-order.mjs +383 -0
- package/skills/developing-cpp/steps/01-behavior.md +5 -5
- package/skills/developing-cpp/steps/02-boundary.md +9 -9
- package/skills/developing-cpp/steps/03-declaration.md +16 -14
- package/skills/developing-cpp/steps/04-implementation.md +22 -18
- package/skills/engineering/SKILL.md +43 -40
- package/skills/engineering/{cmake → references/cmake}/assets/CompileSettings.cmake +2 -0
- package/skills/engineering/{cmake → references/cmake}/linking/01-libraries.md +6 -0
- package/skills/engineering/references/cmake/linking/02-rpath.md +25 -0
- package/skills/engineering/{cmake → references/cmake}/linking/03-undefined-reference.md +7 -1
- package/skills/engineering/{cmake → references/cmake}/linking/04-inspect.md +6 -0
- package/skills/engineering/{cmake/02-modules/01-functions.md → references/cmake/modules/functions.md} +8 -5
- package/skills/engineering/{cmake/03-standard → references/cmake}/standards/README.md +12 -12
- package/skills/engineering/{cmake/03-standard → references/cmake}/standards/c++23/README.md +3 -3
- package/skills/engineering/{cmake/03-standard/01-cxx-standard.md → references/cmake/standards/cxx-standard.md} +8 -7
- package/skills/engineering/{cmake/01-targets → references/cmake/targets}/examples/target.md +2 -2
- package/skills/engineering/{cmake/01-targets/01-rules.md → references/cmake/targets/rules.md} +18 -11
- package/skills/engineering/{cmake → references/cmake}/testing/ctest.md +10 -3
- package/skills/engineering/{formatting → references/formatting}/01-encoding.md +1 -1
- package/skills/engineering/{formatting → references/formatting}/02-clang-format.md +7 -1
- package/skills/engineering/{formatting → references/formatting}/03-clang-tidy.md +7 -0
- package/skills/engineering/{formatting → references/formatting}/04-clangd.md +7 -0
- package/skills/engineering/references/formatting/examples/clang-format.md +38 -0
- package/skills/engineering/{formatting → references/formatting}/examples/clang-tidy.md +0 -21
- package/skills/engineering/{formatting → references/formatting}/scripts/checking-encoding.mjs +6 -7
- package/skills/engineering/{cmake/steps → steps}/01-targets.md +10 -9
- package/skills/engineering/{cmake/steps → steps}/02-modules.md +4 -4
- package/skills/engineering/{cmake/steps → steps}/03-check.md +5 -3
- package/skills/developing-cpp/source/03-design/01-project-design.md +0 -105
- package/skills/developing-cpp/source/03-design/examples/move-result.md +0 -39
- package/skills/developing-cpp/source/03-design/examples/preview-read.md +0 -40
- package/skills/developing-cpp/source/04-layout/03-declaration-order.md +0 -118
- package/skills/developing-cpp/source/05-language/01-language-basics/04-const.md +0 -15
- package/skills/developing-cpp/source/05-language/01-language-basics/05-expressions.md +0 -8
- package/skills/developing-cpp/source/05-language/01-language-basics/06-functions.md +0 -9
- package/skills/developing-cpp/source/05-language/01-language-basics/08-raii.md +0 -9
- package/skills/developing-cpp/source/05-language/02-value-types/03-integer-roles.md +0 -23
- package/skills/developing-cpp/source/05-language/02-value-types/04-enum.md +0 -17
- package/skills/developing-cpp/source/05-language/02-value-types/08-comparison.md +0 -4
- package/skills/developing-cpp/source/05-language/03-standard-library/02-access.md +0 -8
- package/skills/developing-cpp/source/05-language/03-standard-library/06-format.md +0 -44
- package/skills/developing-cpp/source/05-language/03-standard-library/07-time.md +0 -3
- package/skills/developing-cpp/source/05-language/03-standard-library/08-random.md +0 -3
- package/skills/developing-cpp/source/05-language/03-standard-library/09-files.md +0 -4
- package/skills/developing-cpp/source/05-language/04-performance/01-measure.md +0 -15
- package/skills/developing-cpp/source/05-language/04-performance/06-false-optimizations.md +0 -6
- package/skills/developing-cpp/source/06-naming/01-identifier-names.md +0 -78
- package/skills/developing-cpp/source/06-naming/examples/naming-accessor.md +0 -17
- package/skills/developing-cpp/source/06-naming/examples/naming-basic.md +0 -3
- package/skills/developing-cpp/source/06-naming/examples/naming-lower-layer.md +0 -39
- package/skills/developing-cpp/source/06-naming/examples/naming-player-mark.md +0 -17
- package/skills/developing-cpp/source/06-naming/examples/naming-state-change.md +0 -57
- package/skills/developing-cpp/source/07-comments/01-comment-rules.md +0 -239
- package/skills/developing-cpp/source/07-comments/examples/declaration-class.md +0 -44
- package/skills/developing-cpp/source/07-comments/examples/declaration-constructor.md +0 -22
- package/skills/developing-cpp/source/07-comments/examples/declaration-enum.md +0 -24
- package/skills/developing-cpp/source/07-comments/examples/declaration-function.md +0 -100
- package/skills/developing-cpp/source/07-comments/examples/declaration-member.md +0 -25
- package/skills/developing-cpp/source/07-comments/examples/declaration-struct.md +0 -18
- package/skills/developing-cpp/source/07-comments/examples/implementation-body.md +0 -42
- package/skills/developing-cpp/source/07-comments/glossary/comment-terms.md +0 -75
- package/skills/developing-cpp/source/08-exceptions/01-throw-rules.md +0 -160
- package/skills/engineering/cmake/linking/02-rpath.md +0 -17
- package/skills/engineering/formatting/examples/clang-format.md +0 -17
- /package/skills/developing-cpp/{source/07-comments → references/comments}/examples/implementation-todo.md +0 -0
- /package/skills/developing-cpp/{source/03-design → references/design}/examples/behavior-sentence.md +0 -0
- /package/skills/developing-cpp/{source/03-design → references/design}/examples/library-boundary.md +0 -0
- /package/skills/developing-cpp/{source/03-design → references/design}/examples/library-split.md +0 -0
- /package/skills/developing-cpp/{source/08-exceptions → references/exceptions}/examples/throw-boundary.md +0 -0
- /package/skills/developing-cpp/{source/08-exceptions → references/exceptions}/examples/throw-catch.md +0 -0
- /package/skills/developing-cpp/{source/08-exceptions → references/exceptions}/examples/throw-destructor.md +0 -0
- /package/skills/developing-cpp/{source/08-exceptions → references/exceptions}/examples/throw-format.md +0 -0
- /package/skills/developing-cpp/{source/08-exceptions → references/exceptions}/examples/throw-when.md +0 -0
- /package/skills/developing-cpp/{source/05-language/04-performance → references/language/performance}/07-checklist.md +0 -0
- /package/skills/developing-cpp/{source/05-language/03-standard-library → references/language/standard-library}/10-checklist.md +0 -0
- /package/skills/developing-cpp/{source/05-language/02-value-types → references/language/value-types}/05-strong-type.md +0 -0
- /package/skills/developing-cpp/{source/06-naming → references/naming}/examples/naming-current-prefix.md +0 -0
- /package/skills/developing-cpp/{source/06-naming → references/naming}/examples/naming-multiple-return.md +0 -0
- /package/skills/developing-cpp/{source/06-naming → references/naming}/examples/naming-rename.md +0 -0
- /package/skills/developing-cpp/{source/06-naming → references/naming}/examples/naming-responsibility.md +0 -0
- /package/skills/engineering/{cmake → references/cmake}/assets/LinkSettings.cmake +0 -0
- /package/skills/engineering/{cmake/03-standard → references/cmake}/standards/c++11/README.md +0 -0
- /package/skills/engineering/{cmake/03-standard → references/cmake}/standards/c++11/cmake-snippet.cmake +0 -0
- /package/skills/engineering/{cmake/03-standard → references/cmake}/standards/c++14/README.md +0 -0
- /package/skills/engineering/{cmake/03-standard → references/cmake}/standards/c++14/cmake-snippet.cmake +0 -0
- /package/skills/engineering/{cmake/03-standard → references/cmake}/standards/c++17/README.md +0 -0
- /package/skills/engineering/{cmake/03-standard → references/cmake}/standards/c++17/cmake-snippet.cmake +0 -0
- /package/skills/engineering/{cmake/03-standard → references/cmake}/standards/c++20/README.md +0 -0
- /package/skills/engineering/{cmake/03-standard → references/cmake}/standards/c++20/cmake-snippet.cmake +0 -0
- /package/skills/engineering/{cmake/03-standard → references/cmake}/standards/c++23/cmake-snippet.cmake +0 -0
- /package/skills/engineering/{cmake/03-standard → references/cmake/standards}/examples/standard.md +0 -0
- /package/skills/engineering/{cmake/01-targets → references/cmake/targets}/examples/glob.md +0 -0
- /package/skills/engineering/{cmake/01-targets → references/cmake/targets}/examples/rpath.md +0 -0
- /package/skills/engineering/{formatting → references/formatting}/examples/clangd.md +0 -0
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# 声明顺序
|
|
2
|
+
|
|
3
|
+
顺序的作用是让读者先看类型和契约,再看数据。`clang-format` 不管顺序,靠人工和 review 维持。
|
|
4
|
+
|
|
5
|
+
## 类声明
|
|
6
|
+
|
|
7
|
+
```cpp
|
|
8
|
+
class TaskTable {
|
|
9
|
+
public:
|
|
10
|
+
// 1. 类型与别名
|
|
11
|
+
using Tasks = std::array<TaskState, 16>;
|
|
12
|
+
|
|
13
|
+
// 2. 静态常量
|
|
14
|
+
static constexpr int kTaskCount = 16;
|
|
15
|
+
|
|
16
|
+
// 3. 工厂
|
|
17
|
+
static TaskTable CreateEmpty();
|
|
18
|
+
|
|
19
|
+
// 4. 构造、赋值、析构
|
|
20
|
+
TaskTable();
|
|
21
|
+
TaskTable(const TaskTable&) = default;
|
|
22
|
+
TaskTable& operator=(const TaskTable&) = default;
|
|
23
|
+
~TaskTable();
|
|
24
|
+
|
|
25
|
+
// 5. 其他成员函数
|
|
26
|
+
[[nodiscard]] bool Contains(int id) const;
|
|
27
|
+
[[nodiscard]] TaskState GetTaskState(int id) const;
|
|
28
|
+
void SetTaskState(int id, TaskState state);
|
|
29
|
+
|
|
30
|
+
// 6. 数据成员
|
|
31
|
+
private:
|
|
32
|
+
Tasks tasks_{};
|
|
33
|
+
bool is_open_{false};
|
|
34
|
+
};
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
- `struct` 的数据成员紧跟类型之后;有 invariant 时改用 `class`。
|
|
38
|
+
- `public:` 在前,`private:` 在后;`private:` 前空一行,数据成员之间不空行。
|
|
39
|
+
- 静态常量用 `static constexpr`;跨文件共享的常量放命名空间并 `inline constexpr`。
|
|
40
|
+
- 工厂放在构造函数之前;返回 `TaskTable` 的工厂用 `Create` 开头,名字说明创建的是什么状态。
|
|
41
|
+
- 用户声明的特殊成员函数集中放第 4 组;`= default` 也写出来,读者才知道是故意默认。
|
|
42
|
+
- 不要把数据成员插在函数之间;数据成员放在最后一组,`struct` 是唯一例外(紧跟类型,见上)。
|
|
43
|
+
|
|
44
|
+
### 数据成员之间
|
|
45
|
+
|
|
46
|
+
- 数据成员按初始化依赖、逻辑分组和阅读顺序排列,不按字母序。初始化依赖是硬约束:先声明的成员先初始化,初始化器只能安全使用已经完成初始化的成员。
|
|
47
|
+
- 聚合类型的字段顺序参与聚合初始化与指定初始化器,也决定对象布局与 ABI;它不是 standard-layout 的成立条件。重排要同步所有 `T{a, b}` 调用点,并检查序列化或 ABI 假设。
|
|
48
|
+
- 先主值、后限定值,同类字段放一起。例如落子记录先放位置、再放玩家,读作「在哪、谁」。
|
|
49
|
+
- 互不依赖的成员按「读的时候先要哪样」排:`Game` 依次是 `current_player_`、`board_`、`history_`,读作「轮到谁、棋盘什么样、走过哪些手」;`board_` 是落子改动的主体,`history_` 是它的变更日志,所以排在最后。
|
|
50
|
+
- Google 的 Declaration Order 与 C++ Core Guidelines 的 NL.16 只规定访问级别和组间顺序,都没有字母序要求;沿用现有语义顺序即可。
|
|
51
|
+
- 团队若另定字母序,只作为语义相同时的最后兜底并写进项目规则,不和语义顺序混用。
|
|
52
|
+
|
|
53
|
+
### 其他函数的组内次序
|
|
54
|
+
|
|
55
|
+
Google、LLVM、OpenCV 都没有规定这一组内部的次序;本工作流补上:
|
|
56
|
+
|
|
57
|
+
- 有比较运算符时,开头是 `operator==`:成员默认比较和 `friend` 都放这里,然后才是谓词 `Is` / `Has` / `Can`、`Get`、普通函数按使用先后。`friend` 不再放到这一组末尾。
|
|
58
|
+
- `CheckInRange` 是诊断函数,紧挨 `IsInRange` 写,排在 `Get` 之前;它是本条谓词顺序的例外,见 `references/exceptions/throw-rules.md`。
|
|
59
|
+
- 多个直接返回成员的 `Get` 按对应数据成员的声明顺序排列,不按调用频率或字母序:成员依次是 `current_player_`、`board_`、`history_`,getter 就依次是 `GetCurrentPlayer()`、`GetBoard()`、`GetHistory()`;成员顺序调整时 getter 一起改,一个类里只留一套顺序。
|
|
60
|
+
- 直接返回成员的 `Get` 按成员顺序;没有独立成员的派生查询排在直接 `Get` 之后,按「先局部元素、后整体状态」阅读,不插进直接 `Get` 之间。例:`Game` 的 `GetResult()` 扫描棋盘后才得到结果,排在直接返回成员的 `GetCurrentPlayer()`、`GetBoard()`、`GetHistory()` 之后。
|
|
61
|
+
- 成对的局部/整体操作沿用同一方向:先 `ClearSquare(pos)` 清一格,再 `Clear()` 清整盘;局部操作在前,整体操作在后,不按调用频率互换。
|
|
62
|
+
- `constexpr` / `consteval` 函数不单独提前:它们属于「其他函数」组,按上面的语义次序排。`constexpr` 与 `consteval` 隐含 `inline`,这才使头文件就地定义可行;它们不决定声明位置;就地定义的短函数也留在该组,不因为写在头文件里就挪到类的最前。
|
|
63
|
+
- 比较运算符的签名与 `[[nodiscard]]` 规则见 `references/language/value-types/`,这里不另写一套。
|
|
64
|
+
- 同一类型上的谓词留在类内,不因为实现只转调公开函数就拆成类外自由函数。
|
|
65
|
+
- `friend` 类型仍跟其他类型放在一起,和 `friend` 函数不是同一组。
|
|
66
|
+
|
|
67
|
+
## 命名空间作用域的头文件
|
|
68
|
+
|
|
69
|
+
文件槽位与守卫名以 `references/layout/02-file-template.md` 为准;本节只看命名空间作用域内的排列顺序。
|
|
70
|
+
|
|
71
|
+
```cpp
|
|
72
|
+
#ifndef TASKFLOW_INCLUDE_TASK_TABLE_H_
|
|
73
|
+
#define TASKFLOW_INCLUDE_TASK_TABLE_H_
|
|
74
|
+
|
|
75
|
+
#include <array>
|
|
76
|
+
#include <cstdint>
|
|
77
|
+
#include <string>
|
|
78
|
+
|
|
79
|
+
namespace taskflow {
|
|
80
|
+
|
|
81
|
+
inline constexpr int kTaskCount = 16;
|
|
82
|
+
|
|
83
|
+
enum class TaskState : std::uint8_t { kPending = 0, kActive = 1, kDone = 2 };
|
|
84
|
+
|
|
85
|
+
struct TaskRecord {
|
|
86
|
+
int id{0};
|
|
87
|
+
TaskState state{TaskState::kPending};
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
[[nodiscard]] bool Contains(int id);
|
|
91
|
+
[[nodiscard]] std::string TaskStateToString(TaskState state);
|
|
92
|
+
|
|
93
|
+
class TaskTable {
|
|
94
|
+
public:
|
|
95
|
+
// 类内仍按上面的六组顺序
|
|
96
|
+
private:
|
|
97
|
+
std::array<TaskState, kTaskCount> tasks_{};
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
} // namespace taskflow
|
|
101
|
+
|
|
102
|
+
#endif // TASKFLOW_INCLUDE_TASK_TABLE_H_
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
顺序:包含 → 命名空间 → `inline constexpr` 常量 → `enum class` → `struct`(被依赖的在前)→ 自由函数 → 类。类的公共接口类型放在类内最前。
|
|
106
|
+
|
|
107
|
+
## `.cpp` 的定义顺序
|
|
108
|
+
|
|
109
|
+
- 本工作流约定:与头文件声明同序,先类型和常量,再自由函数,再成员函数;成员函数按类内声明顺序。Google、LLVM 与 C++ Core Guidelines 都没有规定 `.cpp` 成员定义的次序;这里选同序,是为了让读者能在头文件与实现之间逐项对照。
|
|
110
|
+
- 文件头注释之后先放本文件的 `namespace taskflow {`,再放公共定义;常量仍排在具名命名空间内、成员函数之前;内部链接的写法按 `references/style/google-cpp-style.md`,本文不复述。
|
|
111
|
+
- 构造函数、析构函数、拷贝/移动定义通常放在成员函数组最前;如果头文件把某个成员声明在构造函数之前,仍按头文件声明顺序,不把构造函数硬提到前面。`= default` 的定义放 `.cpp` 时紧跟声明顺序。
|
|
112
|
+
- 不要为了「先编译」重排定义;C++ 不要求定义顺序,读者顺序优先。
|
|
113
|
+
|
|
114
|
+
## 空行
|
|
115
|
+
|
|
116
|
+
| 位置 | 空行 |
|
|
117
|
+
| --- | --- |
|
|
118
|
+
| 类内六个组之间 | 1 行 |
|
|
119
|
+
| 同组函数之间 | 1 行 |
|
|
120
|
+
| 数据成员之间 | 0 行 |
|
|
121
|
+
| `private:` 之前 | 1 行 |
|
|
122
|
+
| 命名空间内顶层实体之间 | 1 行 |
|
|
123
|
+
| 函数体内逻辑组之间 | 1 行,最多 1 行 |
|
|
124
|
+
|
|
125
|
+
连续两个空行视为格式缺陷;`clang-format` 的 `MaxEmptyLinesToKeep: 1` 会压掉,但不要依赖它。
|
|
126
|
+
|
|
127
|
+
### 函数体内的空行
|
|
128
|
+
|
|
129
|
+
- 连续的守卫检查连成一组:都以 `return` / `throw` 结束或都是前置判断时,组内不空行;守卫组与后续处理之间空 1 行。
|
|
130
|
+
- 取回一个结果并按结果分支的语句连成一组(声明、`if`、`throw` / `return`),组内不空行;这一组与收尾(记录这一步、轮到对方、返回成功)之间空 1 行。
|
|
131
|
+
- 同一组语句不拆:三个校验之后的赋值与 `return` 之间不空行。
|
|
132
|
+
- 只有次序依赖的一对 `if`(`GetWinner`、`JudgeAfterPlace`)之间空 1 行。
|
|
133
|
+
- 最多 1 行,不连续空两行。
|
|
134
|
+
|
|
135
|
+
## 依据
|
|
136
|
+
|
|
137
|
+
- [Google C++ Style Guide: Declaration Order](https://google.github.io/styleguide/cppguide.html#Declaration_Order):类内先 public、再 protected、最后 private;同节内类型、常量、函数、数据成员的先后。
|
|
138
|
+
- [Google C++ Style Guide: Vertical Whitespace](https://google.github.io/styleguide/cppguide.html#Vertical_Whitespace):垂直留白越少越好;空行用来分隔逻辑分组,函数体首尾不留空行,函数体内不随意加空行。
|
|
139
|
+
- [C++ Core Guidelines: NL.16](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#rl-order):类型、构造/赋值/析构、函数、数据成员的传统次序。
|
|
140
|
+
- [cppreference: 构造函数](https://en.cppreference.com/w/cpp/language/constructor):非静态数据成员按声明顺序初始化,与 mem-initializer 列表顺序无关。
|
|
141
|
+
- [LLVM Coding Standards](https://llvm.org/docs/CodingStandards.html):只要求每个 `.cpp` 先包含定义其接口的头文件,没有规定成员定义的先后;上一条「与头文件声明同序」因此是本工作流约定,不冒充外部标准。
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# 正反例:访问器
|
|
2
|
+
|
|
3
|
+
**差**:
|
|
4
|
+
|
|
5
|
+
```cpp
|
|
6
|
+
Order current_order() const; // 成员名直接当函数名
|
|
7
|
+
void set_current_order(Order order);
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
**好**:
|
|
11
|
+
|
|
12
|
+
```cpp
|
|
13
|
+
[[nodiscard]] Order GetCurrentOrder() const;
|
|
14
|
+
void AdvanceOrder(); // 领域动作,不是 setter
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Google 允许访问器与成员同名(`int count()`、`void set_count(int)`),本项目统一 `Get` + PascalCase,示例按本项目规则判为差。
|
|
18
|
+
|
|
19
|
+
查询统一 `Get` + PascalCase;修改用领域动词。只有真正需要替换值时,才考虑 `Set` 形式,并说明为什么不是动作。
|
|
20
|
+
|
|
21
|
+
返回集合时给只读视图,接口不绑定内部容器:
|
|
22
|
+
|
|
23
|
+
```cpp
|
|
24
|
+
/// 获取落子历史。
|
|
25
|
+
///
|
|
26
|
+
/// @return 只读视图,按落子先后排列,不复制落子记录。
|
|
27
|
+
[[nodiscard]] std::span<const Move> GetHistory() const; // 不写 const std::vector<Move>&
|
|
28
|
+
```
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# 正反例:局部名字
|
|
2
|
+
|
|
3
|
+
**差**:
|
|
4
|
+
|
|
5
|
+
```cpp
|
|
6
|
+
int moves = 0;
|
|
7
|
+
bool x_to_move = true;
|
|
8
|
+
int best = 0;
|
|
9
|
+
int value = 0;
|
|
10
|
+
bool undone = false;
|
|
11
|
+
bool better = false;
|
|
12
|
+
Game work = game;
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
**好**:
|
|
16
|
+
|
|
17
|
+
```cpp
|
|
18
|
+
int move_count = 0;
|
|
19
|
+
bool is_x_to_move = true;
|
|
20
|
+
int best_score = 0;
|
|
21
|
+
int score = 0;
|
|
22
|
+
bool is_undone = false;
|
|
23
|
+
bool is_better = false;
|
|
24
|
+
Game search_game = game;
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
局部名字先说明它是什么,再说明它在函数里扮什么角色:
|
|
28
|
+
|
|
29
|
+
| 差 | 好 | 理由 |
|
|
30
|
+
| --- | --- | --- |
|
|
31
|
+
| `moves` | `move_count` | 计数用单数名词加 `_count`,不用复数冒充容器 |
|
|
32
|
+
| `x_to_move` | `is_x_to_move` | 布尔用 `is_` / `has_` / `can_` 开头 |
|
|
33
|
+
| `best` | `best_score` | 同一份分数用同一个词,`best_score` 与 `score` 分工 |
|
|
34
|
+
| `value` | `score` | 泛名 `value` 看不出是哪一份值 |
|
|
35
|
+
| `undone` | `is_undone` | 布尔用 `is_` |
|
|
36
|
+
| `better` | `is_better` | 布尔用 `is_` |
|
|
37
|
+
| `work` | `search_game` | 副本写明用途,不写 `work` |
|
|
38
|
+
| `marks` | `line_values` | 容器按内容命名;这是一条线上的三个方格的值 |
|
|
39
|
+
| `target_mark` | `target_square_value` | 角色与类型都写进名字 |
|
|
40
|
+
| `frames` | `boards` | 容器里装的是 `Board` |
|
|
41
|
+
| `rule` | `separator` | 字符串的用途是分隔线,不是规则 |
|
|
42
|
+
| `LineResult` | `ReadResult` | 结果名跟读入动作,`Line` 已表示成线 |
|
|
43
|
+
| `OutcomeText` | `ResultText` | 跟 `GameResult` 用同一个词 |
|
|
44
|
+
| `human_moves` | `human_move_count` | 计数用 `_count`,不用复数 |
|
|
45
|
+
| `against_cpu` | `is_against_cpu` | 布尔用 `is_` |
|
package/skills/developing-cpp/{source/06-naming → references/naming}/examples/naming-lookup.md
RENAMED
|
@@ -15,4 +15,4 @@
|
|
|
15
15
|
[[nodiscard]] bool HasTask(std::string_view name) const; // 只回答存在性
|
|
16
16
|
```
|
|
17
17
|
|
|
18
|
-
`Get` 表示结果一定存在(否则抛出声明过的领域异常),`Find` 表示可能找不到,`Has`
|
|
18
|
+
`Get` 表示结果一定存在(否则抛出声明过的领域异常),`Find` 表示可能找不到,`Has` 只回答是否存在(`Contains` 留给容器成员)。不要用 `Find` 返回 `bool`。
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# 正反例:下层类型不要借用上层词汇
|
|
2
|
+
|
|
3
|
+
**差**:`Board` 定义并返回 `GameResult`。
|
|
4
|
+
|
|
5
|
+
```cpp
|
|
6
|
+
// board.h:棋盘定义了对局结果
|
|
7
|
+
enum class GameResult : std::uint8_t {
|
|
8
|
+
kInProgress = 0,
|
|
9
|
+
kXWin = 1,
|
|
10
|
+
kOWin = 2,
|
|
11
|
+
kDraw = 3,
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
class Board {
|
|
15
|
+
public:
|
|
16
|
+
[[nodiscard]] GameResult GetGameResult() const;
|
|
17
|
+
};
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
**为什么错**:`Game` 组合 `Board`,调用方向是 `Game -> Board`;`Board` 不知道「一局对局」的生命周期与终局规则,却用 `GameResult` 命名自己的返回值。读者会以为棋盘负责对局状态,`Game` 里再出现同名概念时也无法区分。**下层类型不出现上层类型名。**
|
|
21
|
+
|
|
22
|
+
**好**:
|
|
23
|
+
|
|
24
|
+
```cpp
|
|
25
|
+
// board.h:棋盘只检查坐标并写入,没有结果类型
|
|
26
|
+
class Board {
|
|
27
|
+
public:
|
|
28
|
+
void SetSquare(Position pos, SquareValue value);
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
// game.h:落子结果跟着对局这一个动作走
|
|
32
|
+
enum class PlaceResult : std::uint8_t {
|
|
33
|
+
kPlaced = 0,
|
|
34
|
+
kInvalidPlayer = 1,
|
|
35
|
+
kFinishedGame = 2,
|
|
36
|
+
kEmptySquare = 3,
|
|
37
|
+
kOccupiedSquare = 4,
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
class Game {
|
|
41
|
+
public:
|
|
42
|
+
[[nodiscard]] PlaceResult Place(Move move);
|
|
43
|
+
};
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
`Board` 只写入,没有结果类型,也不出现 `Game` 的词汇;`PlaceResult` 与 `GameResult` 都留在 `Game` 层。下层一旦要回答「这一手成没成」,那已经是上层的问题,交给 `Game::Place`。反例里的 `Board::GetGameResult` 也按同样的方式处理:终局由 `Game::GetResult` 计算。
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# 正反例:玩家与方格的值
|
|
2
|
+
|
|
3
|
+
**差**:
|
|
4
|
+
|
|
5
|
+
```cpp
|
|
6
|
+
Player Opponent(Player player); // 缺动词,也看不出换的是什么
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
**好**:
|
|
10
|
+
|
|
11
|
+
```cpp
|
|
12
|
+
[[nodiscard]] constexpr Player PlayerToOpponent(Player player) noexcept;
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
自由函数用 `来源To结果` 加返回值类型:玩家换方是 `PlayerToOpponent`,玩家变成方格的值是 `PlayerToSquareValue`,不写 `SquareValueOf`。`Get` 留给类里的成员查询,如直接返回成员的 `GetCurrentPlayer`。`To` 后面先写结果类型再写角色,所以 `PlayerToOpponent` 不再重复 `Player`。
|
package/skills/developing-cpp/{source/06-naming → references/naming}/examples/naming-predicate.md
RENAMED
|
@@ -15,4 +15,4 @@ auto is_done = [](const Task& task) { return task.done; };
|
|
|
15
15
|
auto has_owner = [](const Task& task) { return !task.owner.empty(); };
|
|
16
16
|
```
|
|
17
17
|
|
|
18
|
-
返回 `bool` 的自由函数用 `Is` / `Has` / `Can`
|
|
18
|
+
返回 `bool` 的自由函数用 `Is` / `Has` / `Can` 开头;只有与成对规则一一对应时才用 `Completes`、`Would` 这类限定式;Lambda 用 snake_case 的 `is_` / `has_` / `can_`,读起来像条件。
|
package/skills/developing-cpp/{source/06-naming → references/naming}/examples/naming-role.md
RENAMED
|
@@ -11,7 +11,7 @@ TaskState To(TaskState state); // 从什么到什么?
|
|
|
11
11
|
**好**:
|
|
12
12
|
|
|
13
13
|
```cpp
|
|
14
|
-
[[nodiscard]] bool
|
|
14
|
+
[[nodiscard]] bool HasId(int id) const;
|
|
15
15
|
[[nodiscard]] ParseResult TryParse(std::string_view text);
|
|
16
16
|
[[nodiscard]] TaskState StatusCodeToTaskState(int code);
|
|
17
17
|
```
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# 正反例:状态变更:动作与结果枚举
|
|
2
|
+
|
|
3
|
+
**差**:
|
|
4
|
+
|
|
5
|
+
```cpp
|
|
6
|
+
enum class PlaceResult : std::uint8_t {
|
|
7
|
+
kPlaced = 0,
|
|
8
|
+
kEmptySquare = 1,
|
|
9
|
+
kOccupiedSquare = 2,
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
class Square {
|
|
13
|
+
public:
|
|
14
|
+
[[nodiscard]] PlaceResult Place(SquareValue value);
|
|
15
|
+
void Clear();
|
|
16
|
+
};
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
`Square` 只保存一格的值;`Place` 需要坐标、占用与终局,`Clear` 也要由容器决定。把两者放进单格,等于让值对象承担棋盘规则。
|
|
20
|
+
|
|
21
|
+
**好**:
|
|
22
|
+
|
|
23
|
+
```cpp
|
|
24
|
+
class Square {
|
|
25
|
+
public:
|
|
26
|
+
[[nodiscard]] bool IsEmpty() const;
|
|
27
|
+
[[nodiscard]] SquareValue GetValue() const;
|
|
28
|
+
void SetValue(SquareValue value) noexcept;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
class Board {
|
|
32
|
+
public:
|
|
33
|
+
[[nodiscard]] Square GetSquare(Position pos) const;
|
|
34
|
+
void SetSquare(Position pos, SquareValue value);
|
|
35
|
+
void ClearSquare(Position pos);
|
|
36
|
+
void Clear();
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// game.h:一次落子的结果留在对局层
|
|
40
|
+
enum class PlaceResult : std::uint8_t {
|
|
41
|
+
kPlaced = 0,
|
|
42
|
+
kInvalidPlayer = 1,
|
|
43
|
+
kFinishedGame = 2,
|
|
44
|
+
kEmptySquare = 3,
|
|
45
|
+
kOccupiedSquare = 4,
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
class Game {
|
|
49
|
+
public:
|
|
50
|
+
[[nodiscard]] PlaceResult Place(Move move);
|
|
51
|
+
};
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
`SetValue` 只改方格的值;`Board::SetSquare` 检查坐标后原样写入,落子成没成由 `Game::Place` 回答。`PlaceResult` 与 `Game::Place` 都留在对局层。`ClearSquare` 清一格,`Clear` 清整盘;单格不再有 `Clear`。
|
|
55
|
+
|
|
56
|
+
改名按这个顺序做,每一步都能编译:
|
|
57
|
+
|
|
58
|
+
| 旧名 | 新名 | 理由 |
|
|
59
|
+
| --- | --- | --- |
|
|
60
|
+
| `Square::Place` | `Square::SetValue` | 单格只做赋值,不承担落子规则 |
|
|
61
|
+
| `Square::PlaceResult` | `PlaceResult`(`Game`) | 结果跟随产生它的对象,不留在值对象里 |
|
|
62
|
+
| `Square::Clear` | `Board::ClearSquare` | 清空需要棋盘位置,单格只改值 |
|
|
63
|
+
| `kEmptyInput` | `kEmptySquare` | 失败值写原因,和其他结果值同一句式 |
|
|
64
|
+
|
|
65
|
+
`SetValue` 允许写入任意 `SquareValue`,后写覆盖前值。注释也要跟着改名,但不要把「不为空」当状态:
|
|
66
|
+
|
|
67
|
+
```cpp
|
|
68
|
+
///< 失败:方格不为空 // 差
|
|
69
|
+
///< 失败:目标方格已有值,没有写入 // 好
|
|
70
|
+
```
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# 命名术语与用词
|
|
2
|
+
|
|
3
|
+
本文件是 `references/naming/identifier-names.md` 的附录,只在判不准某个词该用完整写法还是缩写时打开;它不随该文件一起加载,也不重复命名规则表。
|
|
4
|
+
|
|
5
|
+
一词一文件只用于命名主题的词汇;注释与语言主题仍用单份术语文件,不按这个结构拆。
|
|
6
|
+
|
|
7
|
+
## 词表
|
|
8
|
+
|
|
9
|
+
| 词 | 结论 | 条目 |
|
|
10
|
+
| --- | --- | --- |
|
|
11
|
+
| `position` / `pos` | 坐标值的参数与局部变量用 `pos`;公开数据成员用 `position`;迭代器式位置对象用 `position` | [position.md](position.md) |
|
|
12
|
+
| `square` / `SquareValue` | 方格;方格的值;不用 `Mark`、`Cell`、`Token` | [square.md](square.md) |
|
|
13
|
+
|
|
14
|
+
## 加词流程
|
|
15
|
+
|
|
16
|
+
1. 查 ISO C++ 草案对应条款,记下标准自己的写法。
|
|
17
|
+
2. 查一个标准库实现(libstdc++ 或 libc++)与一个同类库(Qt、SFML、Godot)。
|
|
18
|
+
3. 结论写进 `glossary/<词>.md`,词表加一行链接;只有一词多义或社区争议大时,才在词条里展开。
|
|
19
|
+
4. 同一事实只写一遍:规则在 `identifier-names.md`,依据与取舍在词条里。
|
|
20
|
+
|
|
21
|
+
## 词条格式
|
|
22
|
+
|
|
23
|
+
每个词条按 [position.md](position.md) 的结构写:一句话结论、语义对照表(语义 / 写法 / 依据)、取舍说明、依据链接。
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# position 与 pos
|
|
2
|
+
|
|
3
|
+
本文件是 `references/naming/identifier-names.md` 的附录,只讨论 `position` 这个词的写法;词表与加词流程见 [naming-terms.md](naming-terms.md)。
|
|
4
|
+
|
|
5
|
+
同一个词按语义分三种写法,不按长短取舍。
|
|
6
|
+
|
|
7
|
+
| 语义 | 写法 | 依据 |
|
|
8
|
+
| --- | --- | --- |
|
|
9
|
+
| 标量下标或偏移 | `pos` | ISO C++ 草案 `basic_string::find(const T& t, size_type pos = 0)`;libstdc++ 的 `basic_string` 内部统一用 `__pos` |
|
|
10
|
+
| 坐标值 | 参数、局部变量用 `pos`;公开数据成员用 `position` | Qt 的 `QPoint pos()` 与 `QWidget::move(const QPoint& pos)`;本项目 `Position pos`、`Move::position` |
|
|
11
|
+
| 迭代器式位置对象 | `position` | ISO C++ 草案 `vector::insert(const_iterator position, const T& x)`、`vector::erase(const_iterator position)` |
|
|
12
|
+
|
|
13
|
+
- `Position` 是坐标值,不是迭代器,所以参数写 `pos`;`Move::position` 是公开 API 的成员,作用域最大,写完整词。两者是同一条作用域原则(NL.7)的两端。
|
|
14
|
+
- 完整词同样是主流:SFML 写 `setPosition(const Vector2f& position)`,Godot 与 Unity 的属性叫 `position`,Unreal 用 `Location`。本项目按作用域取舍,不把它们当反例。
|
|
15
|
+
- 不采用的缩写:`pt`(point 是类型)、`loc`(编译器语境指 `source_location`)、`coord`(偏数学分量)、`idx`(index 语义不同)。
|
|
16
|
+
|
|
17
|
+
## 依据
|
|
18
|
+
|
|
19
|
+
- ISO C++ `[string.find]`:https://eel.is/c++draft/string.find
|
|
20
|
+
- ISO C++ `[vector.modifiers]`:https://eel.is/c++draft/vector.modifiers
|
|
21
|
+
- libstdc++ `basic_string.h`:https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/bits/basic_string.h
|
|
22
|
+
- Qt `QWidget`:https://code.qt.io/cgit/qt/qtbase.git/tree/src/widgets/kernel/qwidget.h
|
|
23
|
+
- SFML `Transformable`:https://github.com/SFML/SFML/blob/master/include/SFML/Graphics/Transformable.hpp
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# square 与 SquareValue
|
|
2
|
+
|
|
3
|
+
本文件是 `references/naming/identifier-names.md` 的附录,只讨论 `square` / `SquareValue` 这个词的写法;词表与加词流程见 [naming-terms.md](naming-terms.md)。
|
|
4
|
+
|
|
5
|
+
`Square` 是棋盘上的一格,中文「方格」;格中的内容写 `SquareValue`,中文「方格的值」。不用 `Mark`、`Cell`、`Token`。
|
|
6
|
+
|
|
7
|
+
| 语义 | 写法 | 依据 |
|
|
8
|
+
| --- | --- | --- |
|
|
9
|
+
| 棋盘上的一格 | `Square`;参数、局部变量 `square`;查询 `GetSquare` / `SetSquare` | CPW `Squares`、Stockfish `enum Square`、chess.hpp `Square`、python-chess `chess.Square`;有道 `square`「(棋盘上的)方格」;汉典、海词「方格=方形的格子」 |
|
|
10
|
+
| 格中的内容 | `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
|
+
| 一条线的三个值 | `line_values` | 本项目 `game.cpp`;容器用复数 |
|
|
13
|
+
| 目标方格的值 | `target_square_value` | 本项目 `game.cpp`;角色写进名字 |
|
|
14
|
+
| 避免 | `Mark`、`Cell`、`Token` | 井字棋英文常用 `mark`(Exploratorium「marks in empty squares」),但 `Value` 在 Stockfish 是搜索分值、中文项目 `Value[3][3]` 是点位评估分;本项目按裁决统一 `SquareValue`。`cell` 是「单元格」「格网单元」;`token` 是词法单元/占用物 |
|
|
15
|
+
|
|
16
|
+
## 取舍
|
|
17
|
+
|
|
18
|
+
- 英文棋类资料把 `Square` 当坐标或位置:CPW 的 `Squares`、Stockfish 的 `enum Square`、chess.hpp、python-chess、Chess.com 都这样用;`Square` 只表示「哪一格」,不表示「格中是什么」。
|
|
19
|
+
- 井字棋英文资料更常用 `mark`:Exploratorium 写「putting their marks in empty squares」,SciMigo 的 `enum class Mark { Empty, X, O }` 也是直接先例。`Mark` 比 `SquareValue` 短,也不会把容器名重复进值类型。
|
|
20
|
+
- `SquareValue` 也有先例:LucHermitte 的 C++ 井字棋用 `enum class SquareValue` 与 `Square::value()`;Rust 的 tictacrustle 用 `SquareValue` 与 `set_value`;React 井字棋用 `value`。它和 `Square` 配对,读作「方格的值」。
|
|
21
|
+
- 风险在 `Value` 的歧义:Stockfish 的 `using Value = int` 是搜索分值;中文项目的 `Value[3][3]` 是点位评估分。`SquareValue` 有被读成「分值」的可能,所以类型注释必须写「方格的值:kEmpty(空)/ kX(X)/ kO(O)」,行尾写「空」「X」「O」;函数名跟类型走 `GetValue` / `SetValue`。
|
|
22
|
+
- 本项目按用户裁决使用 `SquareValue`,与插件规则、代码和书稿统一;`Mark` 只在英文资料的取舍说明里出现,不作为代码名。
|
|
23
|
+
- 中文用词:`Square` 写「方格」,`SquareValue` 写「方格的值」;`格子` 更泛(汉典「方形的空栏或框子」),`棋格` 是「下棋的技艺水平」(汉典),都不能替代「方格」;`棋盘` 是整块盘面。`值` 用「方格的值」「空」「X」「O」;`标记` 不再用于这个类型。
|
|
24
|
+
|
|
25
|
+
## 依据
|
|
26
|
+
|
|
27
|
+
- 有道 `square`:https://dict.youdao.com/result?word=square&lang=en
|
|
28
|
+
- 汉典 `方格`:https://www.zdic.net/hans/%E6%96%B9%E6%A0%BC
|
|
29
|
+
- 海词 `方格`:https://dict.cn/%E6%96%B9%E6%A0%BC
|
|
30
|
+
- 汉典 `格子`:https://www.zdic.net/hans/%E6%A0%BC%E5%AD%90
|
|
31
|
+
- 汉典 `棋格`:https://www.zdic.net/hans/%E6%A3%8B%E6%A0%BC
|
|
32
|
+
- 海词 `value`:https://dict.cn/value
|
|
33
|
+
- 汉典 `值`:https://www.zdic.net/hans/%E5%80%BC
|
|
34
|
+
- 柯林斯 `mark`:https://www.collinsdictionary.com/dictionary/english/mark
|
|
35
|
+
- CPW `Squares`:https://www.chessprogramming.org/Squares
|
|
36
|
+
- Stockfish `types.h`:https://github.com/official-stockfish/Stockfish/blob/master/src/types.h
|
|
37
|
+
- chess.hpp:https://github.com/lichess-org/chess.hpp
|
|
38
|
+
- python-chess `Core`:https://python-chess.readthedocs.io/en/latest/core.html
|
|
39
|
+
- Chess.com `square`:https://www.chess.com/terms/chess-squares
|
|
40
|
+
- LucHermitte/tictactoe:https://github.com/LucHermitte/tictactoe
|
|
41
|
+
- tictacrustle:https://github.com/stevenlr/tictacrustle
|
|
42
|
+
- React 井字棋:https://react.dev/learn/tutorial-tic-tac-toe
|
|
43
|
+
- Wikipedia `Tic-tac-toe`:https://en.wikipedia.org/wiki/Tic-tac-toe
|
|
44
|
+
- ISO C++ `[lex.name]`:https://eel.is/c++draft/lex.name
|
|
45
|
+
- ISO C++ `[dcl.enum]`:https://eel.is/c++draft/dcl.enum
|
|
46
|
+
- Google C++ Style, Naming:https://google.github.io/styleguide/cppguide.html#Naming
|
|
47
|
+
- C++ Core Guidelines, NL:https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#s-naming
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# 命名
|
|
2
|
+
|
|
3
|
+
命名先回答「它是什么角色」,再选词。规则表是底线,语义角色表决定用哪个动词。
|
|
4
|
+
|
|
5
|
+
## 规则表
|
|
6
|
+
|
|
7
|
+
| 种类 | 规则 | 好 | 差 |
|
|
8
|
+
| --- | --- | --- | --- |
|
|
9
|
+
| 类型 / 类 / 结构 / 枚举 | PascalCase | `TaskTable`, `Order`, `TaskState` | `task_table_t`, `order_result` |
|
|
10
|
+
| 函数 / 成员函数 | PascalCase | `Contains`, `GetTaskState` | `contains`, `gettaskstate` |
|
|
11
|
+
| 变量 / 参数 | snake_case | `task_count`, `order` | `TaskCount`, `o` |
|
|
12
|
+
| 类成员 | snake_case + `_` | `tasks_`, `orders_` | `m_tasks`, `tasks` |
|
|
13
|
+
| 结构体成员 | snake_case,无尾随 `_` | `name`, `priority` | `name_`, `priority_` |
|
|
14
|
+
| 常量 / 枚举子 | `k` + PascalCase | `kTaskCount`, `Order::kDraft` | `TASK_COUNT`, `Draft` |
|
|
15
|
+
| 命名空间 | snake_case | `taskflow`, `warehouse` | `Taskflow`, `Warehouse` |
|
|
16
|
+
| 宏 | 全大写 + 项目前缀,尽量不用 | `CPP_WORKFLOW_ASSERT` | `ASSERT` |
|
|
17
|
+
| 模板类型参数 | PascalCase | `T`, `Item` | `t`, `item_t` |
|
|
18
|
+
| 非类型模板参数 | 按常量或变量 | `kMaxItems`, `count` | `N`, `MAX` |
|
|
19
|
+
| 概念 | PascalCase | `Sortable`, `Hashable` | `sortable`, `is_hashable` |
|
|
20
|
+
|
|
21
|
+
- 文件名用 snake_case:`order_result.h`、`task_table.cpp`;测试文件 `task_table_test.cpp`。Google 也允许 `-`,本项目统一 `_`,与 target 和测试名保持一致。
|
|
22
|
+
- 查询名与返回类型一致:返回方格的值时写 `GetCurrentValue`,返回玩家时写 `GetCurrentPlayer`;不写项目里没有对应类型的 `GetPlayer`。
|
|
23
|
+
- 命名空间的自由函数用 `来源To结果` 加返回值类型,不用 `Get`:玩家换方写 `PlayerToOpponent`,玩家变成方格的值写 `PlayerToSquareValue`,不写 `SquareValueOf`。`Get` 留给类里直接返回成员的查询,如 `GetCurrentPlayer`。`To` 后面先写结果类型再写角色,`PlayerToOpponent` 不带重复的 `Player`;没有 `To` 承接时单个 `Opponent` 读不出是玩家还是方格,`To` 语境下反而清楚。名字要点明两端角色,与注释同一个说法。
|
|
24
|
+
- 布尔变量用 `is_` / `has_` / `can_` 开头:`is_valid`、`has_stock`;不要用 `flag`、`status`。
|
|
25
|
+
- 容器用复数:`tasks`、`orders`;单个元素用单数。
|
|
26
|
+
- 缩写按单词处理:`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
|
+
- 参数或局部变量的类型已在签名里写明时,用主流缩写而不是重复类型名:`Position pos`,不写 `Position position`;缩写只在含义无歧义时使用。
|
|
29
|
+
- 局部变量与类成员用同一套词根:成员叫 `current_player_`,局部就叫 `current_player`,不写 `currentPlayer` 这类 camelCase。涉及多个对象时把角色写进名字,如 `target_square`、`target_square_value`,不要用 `target` 这种离开上下文读不出对象的词。规则表里的 snake_case 同样约束局部变量,`.clang-tidy` 的 `readability-identifier-naming` 会检查。
|
|
30
|
+
|
|
31
|
+
## 字符集与保留标识符
|
|
32
|
+
|
|
33
|
+
- 代码标识符只用 ASCII:字母、数字、下划线。标准允许 Unicode XID 标识符,但工具链、字体与跨平台编辑不一致,Google 同样要求 ASCII。
|
|
34
|
+
- 标识符大小写敏感;不要用只差大小写的两个名字区分不同实体。
|
|
35
|
+
- 以下名字保留给实现,写了也不保证有诊断:任何位置出现双下划线 `__`;以下划线加大写字母开头;在全局命名空间中以下划线开头。
|
|
36
|
+
- 不向 `std` 添加声明;只有在标准允许且依赖用户定义类型时,才特化标准模板(如 `std::formatter`、`std::hash`)。
|
|
37
|
+
- 顶层命名空间取项目或团队独有的名字,避免与已有库冲突;不要嵌套 `std`、`absl` 这类知名顶层命名空间。
|
|
38
|
+
|
|
39
|
+
## 语义角色
|
|
40
|
+
|
|
41
|
+
| 角色 | 前缀 / 动词 | 例子 | 说明 |
|
|
42
|
+
| --- | --- | --- | --- |
|
|
43
|
+
| 查询 | `Get` 或访问器风格 | `GetCurrentOrder()`、`CurrentOrder()` | 只读、不改变状态;同一 API 选一种 |
|
|
44
|
+
| 判断 | `Is` / `Has` / `Can` | `IsEmpty()`、`HasLine()`、`CanPlace()` | 返回 `bool`,不抛异常;`Is` / `Has` / `Can` 后面写概念名,不逐个念字段(写 `IsEmpty()`,不写 `IsAllFieldsEmpty()`) |
|
|
45
|
+
| 查找 | `Find` | `FindTask()` | 找不到是正常结果,返回 `optional`、指针或迭代器,不抛 |
|
|
46
|
+
| 计数 | `Count` | `CountMoves()` | 返回数量,不修改状态;`Count` 的对象是集合里的元素 |
|
|
47
|
+
| 尝试 | `Try` / 领域动词 | `TryParse()`、`Place()` | 可能因多个前置条件失败但不抛异常,返回 `enum class` 结果;调用方按名区分失败原因。只有一个失败原因、调用方不需要区分时返回 `bool` |
|
|
48
|
+
| 确定性动作 | 领域动词 | `CompleteTurn()`、`Reset()` | 前置条件满足时一定成功;无法完成任务时抛 |
|
|
49
|
+
| 计算 | 动词开头 | `PreviewWin()` | 从输入算出结果,不修改外部状态 |
|
|
50
|
+
| 映射 | `<来源>To<目标>` | `OrderToReceipt()` | 名字必须含两端角色 |
|
|
51
|
+
| 读写 | `Read` / `Write` | `ReadEnvironment()`、`WriteLedger()` | 涉及 I/O 时用;不要在库核心用 |
|
|
52
|
+
| 访问器 | `Get` + PascalCase,或 snake_case | `GetHistory()`、`history()` | Google 允许两种;同一 API 只选一种,不用成员名直接当函数名 |
|
|
53
|
+
|
|
54
|
+
- 返回 `bool` 的 lambda 也用 `is_` / `has_` / `can_` 命名:`auto is_empty = [](const Order& order) { ... };`。
|
|
55
|
+
- `Find` 不把“没找到”当异常;`Get` 在前置条件不满足时抛标准异常:坐标越界用 `std::out_of_range`,调用时不变量已破坏用 `std::logic_error`;只有调用方必须把这些类型区分开时,才新增派生自它们的类型。两种构建模式的抛出条件相同。两者不要互换。
|
|
56
|
+
- 查询返回多个值时用 `struct`,不要用三个 `GetXxx()` 加输出参数。
|
|
57
|
+
- 返回集合的查询给只读视图,不把内部容器类型写进接口:`std::span<const Move> GetHistory()`,不写 `const std::vector<Move>&`。视图能被区间的用法遍历,改内部存储不影响调用方;跨度只在对象存活期间有效,写进注释。
|
|
58
|
+
|
|
59
|
+
- 禁止裸 `To()` / `Of()`:调用点看不出方向。
|
|
60
|
+
- 修改器用 `Set` + 属性名(`SetCount()` / `set_count()`)或领域动作(`CompleteTask()`);同一 API 只选一种,不要用 `SetItem` 这类暴露存储细节的名字。
|
|
61
|
+
- `current_` 前缀只在有多个候选、且「当前」可能混淆时使用:`current_order_`。只有一个当前对象时不要加。
|
|
62
|
+
|
|
63
|
+
## 谓词与查询命名
|
|
64
|
+
|
|
65
|
+
- 返回 `bool` 的查询用肯定式,读起来像一句断言:`IsEmpty()`、`CanPlace()`、`HasLine()`。
|
|
66
|
+
- 前缀集以 `Is` / `Has` / `Can` 为主,外加与成对规则一一对应的 `Completes`、`Would` 这类限定式;`Contains` 只保留给容器成员(`std::vector::empty` 一类已有标准名),不拿它当一般谓词。
|
|
67
|
+
- `Get` 只用于取值,`Set` 只用于设值;不要用 `CheckLine()` 当布尔 getter。
|
|
68
|
+
- `Check` / `Validate` 留给会报告错误、产生诊断或可能失败的操作,例如 `CheckEnvironment()` 返回问题列表;返回结果类型的校验器也可以用这两个词,只要名字说明它返回结果。它们不做返回纯 `bool` 的查询,bool 查询写在 `Is` / `Has` / `Can` 里。
|
|
69
|
+
- 动作可能因为多个前置条件失败、而调用方需要区分原因时,返回 `enum class`(`PlaceResult`、`GameResult`、`WinPreviewResult`),不要用 `bool` 加输出参数,也不要拆成一串 `IsXxx` 判断再重复检查。
|
|
70
|
+
- 结果枚举的取值写结果类别,两个词以内:成功用 `kPlaced`;失败按发生原因分别取名(`kEmptySquare`、`kOccupiedSquare`),不合并成一个 `kInvalid`。行尾注释以类别开头,写成「成功:…」「失败:…」,冒号后写这次调用的结果与原因,各行按「落子玩家 → 对局状态 → 写入的值 → 目标方格」的顺序写。完整案例见 `references/naming/examples/naming-state-change.md`。
|
|
71
|
+
- 结果枚举的类型用「操作或对象 + Result」,读作「什么的 Result」:`Game::Place` 的结果是 `PlaceResult`,`GetResult` 的结果是 `GameResult`。结果跟随产生它的层:`Board` 只写入,没有结果枚举,也不把 `GameResult` 塞进 `Square` 或 `Board`。
|
|
72
|
+
- 同名操作在不同层返回不同结果时,按各自处理的对象取名:`Board::SetSquare` 只写入,没有返回值;`Game::Place` 处理的是一次落子,返回 `PlaceResult`;整局结果由 `Game::GetResult` 另外回答。不把两个答案压进同一个名字,读者才分得清拿到的是「这一手写成了没有」还是「这一局结束了没有」。
|
|
73
|
+
- 枚举的取值写成短句,与类型名对齐:`GameResult` 用 `kInProgress`、`kXWin`、`kOWin`、`kDraw`;`SquareValue` 用 `kEmpty`、`kX`、`kO`,行尾写「空」「X」「O」;不写 `kXWins` 这类主谓句,也不掺动作名。
|
|
74
|
+
- 词性跟着角色的位置走:前缀 `k` 后面先写角色再写状态,角色作主语、状态作谓语,用不带 `-s` 的动词原形,不用现在分词:`kXWin`、`kOWin`、`kDraw`。谓语读不通时改用名词或形容词,`kInProgress` 是固定说法,`kEmpty` 是形容词。不逐个用 `-ed` 造过去分词(`kXWon`、`kDrawn`),也不写成主谓句 `kXWins`。
|
|
75
|
+
- 假设性查询在名字里写明条件:`PreviewWin(pos, square)` 读作「这一次落子会成线吗」,返回 `WinPreviewResult` 区分「不能落子」与「能落但不赢」;只有成功与不报错的纯判断才用 `bool`。同一 API 只选一种,改名时同步声明、定义、调用点、测试和注释。
|
|
76
|
+
- 私有谓词同样用肯定式;不要用 `flag`、`status` 或 `check` 这类不说明判据的名字。
|
|
77
|
+
|
|
78
|
+
## 类型命名的判断顺序
|
|
79
|
+
|
|
80
|
+
1. 纯数据、无 invariant、可以直接聚合初始化 → `struct`,名字是名词:`Order`, `TaskRecord`。
|
|
81
|
+
2. 有 invariant、需要构造函数维持 → `class`,名字是名词:`TaskTable`, `Warehouse`。
|
|
82
|
+
3. 封闭的状态集合 → `enum class`,名字是单数名词:`TaskState`, `OrderResult`;枚举子用 `k` 前缀。
|
|
83
|
+
- 类型名用拥有它的那一层的词汇:`PlaceResult` 与 `GameResult` 都在 `Game` 层;`Board` 只做写入,不出现 `Game` 前缀,也不承担终局。反例见 `references/naming/examples/naming-lower-layer.md`。
|
|
84
|
+
4. 只有行为、没有状态,且调用方需要替换实现 → 抽象基类;只有一个实现时先用普通类。
|
|
85
|
+
5. 一组自由函数 → 命名空间 + 动词;不要为了「像 Java」造一个 `XxxUtil` 类。
|
|
86
|
+
|
|
87
|
+
## 改名
|
|
88
|
+
|
|
89
|
+
- 改名是一次独立改动:先搜全部调用点,再改声明、定义、测试和注释。
|
|
90
|
+
- 不要把改名和改行为混在一个提交里;改名后测试必须仍然绿色且行为句不变。
|
|
91
|
+
- 公共 API 改名视为破坏性变更;需要兼容时保留旧名转发,并在注释里写弃用期限。
|
|
92
|
+
|
|
93
|
+
## 与 Google、Core Guidelines 的关系
|
|
94
|
+
|
|
95
|
+
- 本工作流的命名基线是 Google C++ Style:类型与函数 PascalCase,变量 snake_case,常量与枚举子 `k` 前缀。项目内不再另立一套。
|
|
96
|
+
- C++ Core Guidelines 的 NL 系列是“没有更好想法时的建议”。NL.10 偏好全 `underscore_style`,与本基线不同;这里采用它的 NL.5(名字不编码类型)、NL.7(名字长度与作用域成比例)、NL.8(风格一致)、NL.9(全大写只给宏)与 NL.19(避免易误读),不整体切换到 `underscore_style`。
|
|
97
|
+
- 两者冲突时以本文件的明确规则为准;本文件没写的,按 Google 基线处理。
|
|
98
|
+
|
|
99
|
+
## 依据
|
|
100
|
+
|
|
101
|
+
- ISO C++ `[lex.name]`:标识符字符集、大小写敏感与保留标识符。https://eel.is/c++draft/lex.name
|
|
102
|
+
- ISO C++ `[namespace.std]`:向 `std` 添加声明与特化的限制。https://eel.is/c++draft/namespace.std
|
|
103
|
+
- Google C++ Style Guide, Naming:https://google.github.io/styleguide/cppguide.html#Naming
|
|
104
|
+
- C++ Core Guidelines, NL: Naming and layout:https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#s-naming
|
|
105
|
+
- `square` / `SquareValue` 的取舍与词义来源见 [square.md](glossary/square.md)。
|
|
106
|
+
|
|
107
|
+
## 术语
|
|
108
|
+
|
|
109
|
+
中文术语与英文标识符的对应见 `references/language/glossary/language-terms.md`。代码标识符用英文;注释可以用中文,但同一个概念只用一种译法。
|