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