@chengzhao-dev/dsh-cpp-workflow 0.7.1 → 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 +16 -10
- package/README.md +16 -10
- package/cordis.patch.yml +6 -6
- package/lib/client.js +2 -2
- package/lib/index.js +45 -37
- package/lib/workspace.js +10 -17
- package/package.json +1 -1
- package/skills/building-cpp/SKILL.md +3 -3
- package/skills/building-cpp/assets/templates/projects/common/running-build.sh +0 -0
- package/skills/building-cpp/references/environment.md +1 -1
- package/skills/building-cpp/scripts/checking-environment.mjs +11 -13
- package/skills/building-cpp/steps/01-build.md +3 -2
- package/skills/building-cpp/steps/02-tooling.md +2 -2
- package/skills/developing-cpp/SKILL.md +10 -7
- package/skills/developing-cpp/references/comments/comment-rules.md +56 -42
- package/skills/developing-cpp/references/comments/comment-style.md +8 -12
- package/skills/developing-cpp/references/comments/examples/declaration-class.md +15 -16
- package/skills/developing-cpp/references/comments/examples/declaration-constructor.md +1 -1
- package/skills/developing-cpp/references/comments/examples/declaration-enum.md +17 -17
- package/skills/developing-cpp/references/comments/examples/declaration-file.md +3 -3
- package/skills/developing-cpp/references/comments/examples/declaration-function.md +30 -22
- package/skills/developing-cpp/references/comments/examples/declaration-member.md +3 -3
- package/skills/developing-cpp/references/comments/examples/declaration-predicate.md +1 -1
- package/skills/developing-cpp/references/comments/examples/declaration-struct.md +3 -3
- package/skills/developing-cpp/references/comments/examples/implementation-body.md +34 -17
- package/skills/developing-cpp/references/comments/glossary/comment-terms.md +30 -31
- package/skills/developing-cpp/references/design/examples/coordinate-table.md +3 -3
- package/skills/developing-cpp/references/design/examples/header-place.md +1 -1
- package/skills/developing-cpp/references/design/examples/move-result.md +31 -22
- package/skills/developing-cpp/references/design/examples/preview-read.md +6 -10
- package/skills/developing-cpp/references/design/project-design.md +17 -14
- package/skills/developing-cpp/references/editing/01-edit-operations.md +1 -1
- package/skills/developing-cpp/references/exceptions/examples/throw-build-mode.md +4 -4
- package/skills/developing-cpp/references/exceptions/examples/throw-message.md +8 -0
- package/skills/developing-cpp/references/exceptions/throw-rules.md +62 -59
- package/skills/developing-cpp/references/language/language-basics/01-objects.md +1 -1
- package/skills/developing-cpp/references/language/language-basics/02-initialization.md +1 -1
- package/skills/developing-cpp/references/language/language-basics/04-const.md +4 -4
- package/skills/developing-cpp/references/language/language-basics/05-expressions.md +8 -3
- package/skills/developing-cpp/references/language/language-basics/06-functions.md +2 -0
- package/skills/developing-cpp/references/language/standard-library/02-access.md +1 -1
- package/skills/developing-cpp/references/language/standard-library/06-format.md +13 -5
- package/skills/developing-cpp/references/language/value-types/04-enum.md +1 -1
- package/skills/developing-cpp/references/language/value-types/07-nodiscard.md +1 -1
- package/skills/developing-cpp/references/language/value-types/08-comparison.md +1 -1
- package/skills/developing-cpp/references/layout/01-source-layout.md +2 -3
- package/skills/developing-cpp/references/layout/02-file-template.md +7 -7
- package/skills/developing-cpp/references/layout/03-declaration-order.md +12 -9
- package/skills/developing-cpp/references/naming/examples/naming-accessor.md +11 -0
- package/skills/developing-cpp/references/naming/examples/naming-local.md +45 -0
- package/skills/developing-cpp/references/naming/examples/naming-lookup.md +1 -1
- package/skills/developing-cpp/references/naming/examples/naming-lower-layer.md +20 -13
- package/skills/developing-cpp/references/naming/examples/naming-player-value.md +4 -5
- package/skills/developing-cpp/references/naming/examples/naming-predicate.md +1 -1
- package/skills/developing-cpp/references/naming/examples/naming-role.md +1 -1
- package/skills/developing-cpp/references/naming/examples/naming-state-change.md +38 -25
- 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 +24 -19
- package/skills/developing-cpp/references/style/google-cpp-style.md +4 -4
- package/skills/developing-cpp/scripts/checking-comments.mjs +9 -1
- package/skills/developing-cpp/scripts/checking-declaration-order.mjs +27 -1
- package/skills/developing-cpp/steps/03-declaration.md +4 -4
- package/skills/developing-cpp/steps/04-implementation.md +8 -8
- package/skills/engineering/SKILL.md +7 -7
- package/skills/engineering/references/cmake/linking/03-undefined-reference.md +1 -1
- package/skills/engineering/references/cmake/modules/functions.md +1 -1
- package/skills/engineering/references/cmake/{standard/standards → standards}/README.md +10 -10
- package/skills/engineering/references/cmake/{standard/standards → standards}/c++23/README.md +3 -3
- package/skills/engineering/references/cmake/{standard → standards}/cxx-standard.md +3 -3
- package/skills/engineering/references/cmake/targets/examples/target.md +1 -1
- package/skills/engineering/references/cmake/targets/rules.md +1 -1
- package/skills/engineering/references/cmake/testing/ctest.md +1 -1
- package/skills/engineering/references/formatting/02-clang-format.md +1 -1
- package/skills/engineering/references/formatting/scripts/checking-encoding.mjs +6 -7
- package/skills/engineering/steps/01-targets.md +7 -6
- package/skills/engineering/steps/03-check.md +3 -2
- /package/skills/engineering/references/cmake/{standard/standards → standards}/c++11/README.md +0 -0
- /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/README.md +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/README.md +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/README.md +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
- /package/skills/engineering/references/cmake/{standard → standards}/examples/standard.md +0 -0
|
@@ -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_` |
|
|
@@ -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`。
|
|
@@ -6,9 +6,9 @@
|
|
|
6
6
|
// board.h:棋盘定义了对局结果
|
|
7
7
|
enum class GameResult : std::uint8_t {
|
|
8
8
|
kInProgress = 0,
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
kXWin = 1,
|
|
10
|
+
kOWin = 2,
|
|
11
|
+
kDraw = 3,
|
|
12
12
|
};
|
|
13
13
|
|
|
14
14
|
class Board {
|
|
@@ -17,23 +17,30 @@ class Board {
|
|
|
17
17
|
};
|
|
18
18
|
```
|
|
19
19
|
|
|
20
|
-
**为什么错**:`Game` 组合 `Board`,调用方向是 `Game -> Board`;`Board`
|
|
20
|
+
**为什么错**:`Game` 组合 `Board`,调用方向是 `Game -> Board`;`Board` 不知道「一局对局」的生命周期与终局规则,却用 `GameResult` 命名自己的返回值。读者会以为棋盘负责对局状态,`Game` 里再出现同名概念时也无法区分。**下层类型不出现上层类型名。**
|
|
21
21
|
|
|
22
22
|
**好**:
|
|
23
23
|
|
|
24
24
|
```cpp
|
|
25
|
-
// board.h
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
kOWon = 2,
|
|
30
|
-
kDrawn = 3,
|
|
25
|
+
// board.h:棋盘只检查坐标并写入,没有结果类型
|
|
26
|
+
class Board {
|
|
27
|
+
public:
|
|
28
|
+
void SetSquare(Position pos, SquareValue value);
|
|
31
29
|
};
|
|
32
30
|
|
|
33
|
-
|
|
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 {
|
|
34
41
|
public:
|
|
35
|
-
[[nodiscard]]
|
|
42
|
+
[[nodiscard]] PlaceResult Place(Move move);
|
|
36
43
|
};
|
|
37
44
|
```
|
|
38
45
|
|
|
39
|
-
`Board`
|
|
46
|
+
`Board` 只写入,没有结果类型,也不出现 `Game` 的词汇;`PlaceResult` 与 `GameResult` 都留在 `Game` 层。下层一旦要回答「这一手成没成」,那已经是上层的问题,交给 `Game::Place`。反例里的 `Board::GetGameResult` 也按同样的方式处理:终局由 `Game::GetResult` 计算。
|
|
@@ -1,16 +1,15 @@
|
|
|
1
|
-
#
|
|
1
|
+
# 正反例:玩家与方格的值
|
|
2
2
|
|
|
3
3
|
**差**:
|
|
4
4
|
|
|
5
5
|
```cpp
|
|
6
|
-
Player
|
|
6
|
+
Player Opponent(Player player); // 缺动词,也看不出换的是什么
|
|
7
7
|
```
|
|
8
8
|
|
|
9
9
|
**好**:
|
|
10
10
|
|
|
11
11
|
```cpp
|
|
12
|
-
[[nodiscard]] constexpr Player
|
|
13
|
-
[[nodiscard]] constexpr SquareValue PlayerToSquareValue(Player player) noexcept;
|
|
12
|
+
[[nodiscard]] constexpr Player PlayerToOpponent(Player player) noexcept;
|
|
14
13
|
```
|
|
15
14
|
|
|
16
|
-
|
|
15
|
+
自由函数用 `来源To结果` 加返回值类型:玩家换方是 `PlayerToOpponent`,玩家变成方格的值是 `PlayerToSquareValue`,不写 `SquareValueOf`。`Get` 留给类里的成员查询,如直接返回成员的 `GetCurrentPlayer`。`To` 后面先写结果类型再写角色,所以 `PlayerToOpponent` 不再重复 `Player`。
|
|
@@ -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_`,读起来像条件。
|
|
@@ -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
|
```
|
|
@@ -3,55 +3,68 @@
|
|
|
3
3
|
**差**:
|
|
4
4
|
|
|
5
5
|
```cpp
|
|
6
|
-
enum class
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
enum class PlaceResult : std::uint8_t {
|
|
7
|
+
kPlaced = 0,
|
|
8
|
+
kEmptySquare = 1,
|
|
9
|
+
kOccupiedSquare = 2,
|
|
10
10
|
};
|
|
11
11
|
|
|
12
12
|
class Square {
|
|
13
13
|
public:
|
|
14
|
-
|
|
15
|
-
void
|
|
14
|
+
[[nodiscard]] PlaceResult Place(SquareValue value);
|
|
15
|
+
void Clear();
|
|
16
16
|
};
|
|
17
17
|
```
|
|
18
18
|
|
|
19
|
+
`Square` 只保存一格的值;`Place` 需要坐标、占用与终局,`Clear` 也要由容器决定。把两者放进单格,等于让值对象承担棋盘规则。
|
|
20
|
+
|
|
19
21
|
**好**:
|
|
20
22
|
|
|
21
23
|
```cpp
|
|
22
|
-
enum class PlaceResult : std::uint8_t {
|
|
23
|
-
kSuccess = 0,
|
|
24
|
-
kIgnored = 1,
|
|
25
|
-
kOccupied = 2,
|
|
26
|
-
};
|
|
27
|
-
|
|
28
24
|
class Square {
|
|
29
25
|
public:
|
|
30
26
|
[[nodiscard]] bool IsEmpty() const;
|
|
31
27
|
[[nodiscard]] SquareValue GetValue() const;
|
|
32
|
-
|
|
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);
|
|
33
36
|
void Clear();
|
|
34
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
|
+
};
|
|
35
52
|
```
|
|
36
53
|
|
|
37
|
-
`
|
|
54
|
+
`SetValue` 只改方格的值;`Board::SetSquare` 检查坐标后原样写入,落子成没成由 `Game::Place` 回答。`PlaceResult` 与 `Game::Place` 都留在对局层。`ClearSquare` 清一格,`Clear` 清整盘;单格不再有 `Clear`。
|
|
38
55
|
|
|
39
56
|
改名按这个顺序做,每一步都能编译:
|
|
40
57
|
|
|
41
58
|
| 旧名 | 新名 | 理由 |
|
|
42
59
|
| --- | --- | --- |
|
|
43
|
-
| `
|
|
44
|
-
| `
|
|
45
|
-
| `
|
|
46
|
-
| `kEmptyInput` | `
|
|
60
|
+
| `Square::Place` | `Square::SetValue` | 单格只做赋值,不承担落子规则 |
|
|
61
|
+
| `Square::PlaceResult` | `PlaceResult`(`Game`) | 结果跟随产生它的对象,不留在值对象里 |
|
|
62
|
+
| `Square::Clear` | `Board::ClearSquare` | 清空需要棋盘位置,单格只改值 |
|
|
63
|
+
| `kEmptyInput` | `kEmptySquare` | 失败值写原因,和其他结果值同一句式 |
|
|
47
64
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
注释也要跟着改名,但不要把「不为空」当状态:
|
|
65
|
+
`SetValue` 允许写入任意 `SquareValue`,后写覆盖前值。注释也要跟着改名,但不要把「不为空」当状态:
|
|
51
66
|
|
|
52
67
|
```cpp
|
|
53
|
-
///<
|
|
54
|
-
///<
|
|
68
|
+
///< 失败:方格不为空 // 差
|
|
69
|
+
///< 失败:目标方格已有值,没有写入 // 好
|
|
55
70
|
```
|
|
56
|
-
|
|
57
|
-
`kOccupied` 要从传入的值起句:先写「传入的是 kX 或 kO」,再写「格子已有值」,末尾用「没有写入」说明这次调用没有改变格子;这比只写「格子不为空」既能核对原因,也交代了结果。`kIgnored` 写「传入的是 kEmpty,没有值可写」,`kSuccess` 写「传入的是 kX 或 kO,值已写入格子」,都不重复类型注释已经交代的失败后果。
|
|
@@ -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
|
|
@@ -19,13 +19,14 @@
|
|
|
19
19
|
| 概念 | PascalCase | `Sortable`, `Hashable` | `sortable`, `is_hashable` |
|
|
20
20
|
|
|
21
21
|
- 文件名用 snake_case:`order_result.h`、`task_table.cpp`;测试文件 `task_table_test.cpp`。Google 也允许 `-`,本项目统一 `_`,与 target 和测试名保持一致。
|
|
22
|
-
-
|
|
23
|
-
-
|
|
22
|
+
- 查询名与返回类型一致:返回方格的值时写 `GetCurrentValue`,返回玩家时写 `GetCurrentPlayer`;不写项目里没有对应类型的 `GetPlayer`。
|
|
23
|
+
- 命名空间的自由函数用 `来源To结果` 加返回值类型,不用 `Get`:玩家换方写 `PlayerToOpponent`,玩家变成方格的值写 `PlayerToSquareValue`,不写 `SquareValueOf`。`Get` 留给类里直接返回成员的查询,如 `GetCurrentPlayer`。`To` 后面先写结果类型再写角色,`PlayerToOpponent` 不带重复的 `Player`;没有 `To` 承接时单个 `Opponent` 读不出是玩家还是方格,`To` 语境下反而清楚。名字要点明两端角色,与注释同一个说法。
|
|
24
24
|
- 布尔变量用 `is_` / `has_` / `can_` 开头:`is_valid`、`has_stock`;不要用 `flag`、`status`。
|
|
25
25
|
- 容器用复数:`tasks`、`orders`;单个元素用单数。
|
|
26
26
|
- 缩写按单词处理:`url`、`id`、`http`,不要写 `URL` 混进 snake_case;类型名里可以 `HttpClient`。
|
|
27
|
-
- 不用单字母名:坐标参数和局部变量用 `pos
|
|
27
|
+
- 不用单字母名:坐标参数和局部变量用 `pos`,行、列用 `row`、`col`;公开数据成员仍用完整名词(`Move::position`),避免 API 缩写;迭代器式的位置参数才写完整 `position`。取舍依据与更多词见 [naming-terms.md](glossary/naming-terms.md)。`i` / `j` 只用于没有领域名字的计数(如遍历固定长度的下标)。也不要用 `data`、`info`、`tmp` 这类不说明内容的名字。
|
|
28
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` 会检查。
|
|
29
30
|
|
|
30
31
|
## 字符集与保留标识符
|
|
31
32
|
|
|
@@ -40,19 +41,20 @@
|
|
|
40
41
|
| 角色 | 前缀 / 动词 | 例子 | 说明 |
|
|
41
42
|
| --- | --- | --- | --- |
|
|
42
43
|
| 查询 | `Get` 或访问器风格 | `GetCurrentOrder()`、`CurrentOrder()` | 只读、不改变状态;同一 API 选一种 |
|
|
43
|
-
| 判断 | `Is` / `Has` / `Can` | `IsEmpty()`、`
|
|
44
|
-
| 查找 | `Find` | `FindTask()` | 找不到是正常结果,返回 `optional
|
|
45
|
-
| 计数 | `Count` | `
|
|
46
|
-
| 尝试 | `Try` / 领域动词 | `
|
|
47
|
-
| 确定性动作 | 领域动词 | `
|
|
48
|
-
| 计算 | 动词开头 | `
|
|
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()` | 从输入算出结果,不修改外部状态 |
|
|
49
50
|
| 映射 | `<来源>To<目标>` | `OrderToReceipt()` | 名字必须含两端角色 |
|
|
50
|
-
| 读写 | `Read` / `Write` | `
|
|
51
|
+
| 读写 | `Read` / `Write` | `ReadEnvironment()`、`WriteLedger()` | 涉及 I/O 时用;不要在库核心用 |
|
|
51
52
|
| 访问器 | `Get` + PascalCase,或 snake_case | `GetHistory()`、`history()` | Google 允许两种;同一 API 只选一种,不用成员名直接当函数名 |
|
|
52
53
|
|
|
53
54
|
- 返回 `bool` 的 lambda 也用 `is_` / `has_` / `can_` 命名:`auto is_empty = [](const Order& order) { ... };`。
|
|
54
55
|
- `Find` 不把“没找到”当异常;`Get` 在前置条件不满足时抛标准异常:坐标越界用 `std::out_of_range`,调用时不变量已破坏用 `std::logic_error`;只有调用方必须把这些类型区分开时,才新增派生自它们的类型。两种构建模式的抛出条件相同。两者不要互换。
|
|
55
56
|
- 查询返回多个值时用 `struct`,不要用三个 `GetXxx()` 加输出参数。
|
|
57
|
+
- 返回集合的查询给只读视图,不把内部容器类型写进接口:`std::span<const Move> GetHistory()`,不写 `const std::vector<Move>&`。视图能被区间的用法遍历,改内部存储不影响调用方;跨度只在对象存活期间有效,写进注释。
|
|
56
58
|
|
|
57
59
|
- 禁止裸 `To()` / `Of()`:调用点看不出方向。
|
|
58
60
|
- 修改器用 `Set` + 属性名(`SetCount()` / `set_count()`)或领域动作(`CompleteTask()`);同一 API 只选一种,不要用 `SetItem` 这类暴露存储细节的名字。
|
|
@@ -60,15 +62,17 @@
|
|
|
60
62
|
|
|
61
63
|
## 谓词与查询命名
|
|
62
64
|
|
|
63
|
-
- 返回 `bool` 的查询用肯定式,读起来像一句断言:`IsEmpty()`、`
|
|
64
|
-
-
|
|
65
|
+
- 返回 `bool` 的查询用肯定式,读起来像一句断言:`IsEmpty()`、`CanPlace()`、`HasLine()`。
|
|
66
|
+
- 前缀集以 `Is` / `Has` / `Can` 为主,外加与成对规则一一对应的 `Completes`、`Would` 这类限定式;`Contains` 只保留给容器成员(`std::vector::empty` 一类已有标准名),不拿它当一般谓词。
|
|
65
67
|
- `Get` 只用于取值,`Set` 只用于设值;不要用 `CheckLine()` 当布尔 getter。
|
|
66
|
-
- `Check` / `Validate`
|
|
67
|
-
- 动作可能因为多个前置条件失败、而调用方需要区分原因时,返回 `enum class`(`
|
|
68
|
-
- 结果枚举的取值写结果类别,两个词以内:成功用 `
|
|
69
|
-
- 结果枚举的类型用「操作或对象 + Result」,读作「什么的 Result」:`Place` 的结果是 `PlaceResult
|
|
70
|
-
-
|
|
71
|
-
-
|
|
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 只选一种,改名时同步声明、定义、调用点、测试和注释。
|
|
72
76
|
- 私有谓词同样用肯定式;不要用 `flag`、`status` 或 `check` 这类不说明判据的名字。
|
|
73
77
|
|
|
74
78
|
## 类型命名的判断顺序
|
|
@@ -76,7 +80,7 @@
|
|
|
76
80
|
1. 纯数据、无 invariant、可以直接聚合初始化 → `struct`,名字是名词:`Order`, `TaskRecord`。
|
|
77
81
|
2. 有 invariant、需要构造函数维持 → `class`,名字是名词:`TaskTable`, `Warehouse`。
|
|
78
82
|
3. 封闭的状态集合 → `enum class`,名字是单数名词:`TaskState`, `OrderResult`;枚举子用 `k` 前缀。
|
|
79
|
-
- 类型名用拥有它的那一层的词汇:`
|
|
83
|
+
- 类型名用拥有它的那一层的词汇:`PlaceResult` 与 `GameResult` 都在 `Game` 层;`Board` 只做写入,不出现 `Game` 前缀,也不承担终局。反例见 `references/naming/examples/naming-lower-layer.md`。
|
|
80
84
|
4. 只有行为、没有状态,且调用方需要替换实现 → 抽象基类;只有一个实现时先用普通类。
|
|
81
85
|
5. 一组自由函数 → 命名空间 + 动词;不要为了「像 Java」造一个 `XxxUtil` 类。
|
|
82
86
|
|
|
@@ -98,6 +102,7 @@
|
|
|
98
102
|
- ISO C++ `[namespace.std]`:向 `std` 添加声明与特化的限制。https://eel.is/c++draft/namespace.std
|
|
99
103
|
- Google C++ Style Guide, Naming:https://google.github.io/styleguide/cppguide.html#Naming
|
|
100
104
|
- C++ Core Guidelines, NL: Naming and layout:https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#s-naming
|
|
105
|
+
- `square` / `SquareValue` 的取舍与词义来源见 [square.md](glossary/square.md)。
|
|
101
106
|
|
|
102
107
|
## 术语
|
|
103
108
|
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
- 每个头文件用 `#define` 守卫,符号由项目源码树全路径生成:`<PROJECT>_<PATH>_<FILE>_H_`,例如 `FOO_BAR_BAZ_H_`。不要用 `#pragma once`,也不要两者并存;模板见 `references/layout/02-file-template.md`。
|
|
16
16
|
- 头文件必须自包含:自己用到的类型、宏、标准库头都直接包含,不依赖调用方的包含顺序。
|
|
17
17
|
- `.cpp` 第一个包含自己的配对头,然后按下面的顺序包含。
|
|
18
|
-
- 包含顺序:配对头 → C 系统头 → C++ 标准库头 → 其他库头 →
|
|
18
|
+
- 包含顺序:配对头 → C 系统头 → C++ 标准库头 → 其他库头 → 本项目头。每组之间空一行,组内按字母序。clang-format 的 Google 预设自带 `IncludeCategories`(`^<.*\.h>` 第一组、其余尖括号头第二组、引号头最后)并开了 `IncludeBlocks: Regroup`,配置里不写 `SortIncludes: false` 时这份分组与排序会自动落实。clang-format 按 `IncludeIsMainRegex` 从文件名推断配对头(`board_test.cpp` 配 `board.h`);文件名不遵循 `<name>` 或 `<name>_test` 时要自己把配对头放在第一条。`#include` 之间夹独立注释或 `#if` 会切断分组,这类位置要自己保持次序。需要把第三方库单独成组(例如 `<gtest/gtest.h>` 与标准库之间空一行)时,才在工程 `.clang-format` 里追加优先于 `^<.*` 的类别,并说明理由;预设已覆盖的分组不要重复手写。`clang-tidy` 的 `llvm-include-order` 按 LLVM 分组,与这条不同,不要拿来替代 clang-format。
|
|
19
19
|
- 本项目头用引号写裸文件名,例如 `#include "logging.h"`。构建时把该组件的 `include/` 加入 include 搜索路径(`target_include_directories(<target> PUBLIC .../include)`),不要写 `"base/logging.h"` 这种从源码根起的长路径。
|
|
20
20
|
- 公共头文件直接放在组件的 `include/` 下;不要为了「整齐」再加一层命名空间目录。
|
|
21
21
|
- 跨文件共享的常量用 `inline constexpr`,放在命名空间里并带 `k` 前缀;头文件里不要定义非 inline 的普通全局变量。
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
- 用户声明的拷贝/移动/析构函数在头文件声明、在 `.cpp` 定义;只有零参数且默认实现足够时才写 `= default`。
|
|
40
40
|
- 查询成员函数标尾部 `const`;返回结果对象的 API 加 `[[nodiscard]]`,不要给所有非 `void` 函数机械添加。标注位置与 `friend` 的例外见 `references/language/value-types/07-nodiscard.md`。
|
|
41
41
|
- 所有权优先 `std::unique_ptr`;没有共享所有权的理由时不用 `std::shared_ptr`。禁止裸 `new` / `delete`。
|
|
42
|
-
- 禁止 C 风格强制转换;用 `static_cast`、`const_cast`、`reinterpret_cast
|
|
42
|
+
- 禁止 C 风格强制转换;用 `static_cast`、`const_cast`、`reinterpret_cast`,C++20 起可用 `std::bit_cast`,低标准用 `std::memcpy`;并让 `clang-tidy` 的 `google-readability-casting` 兜底。
|
|
43
43
|
- `.cpp` 里不需要外部引用的 `constexpr` 直接放具名命名空间;仅一个函数使用时才用函数内 `static constexpr`;函数和类型仍放匿名命名空间。
|
|
44
44
|
- 一个 `.cpp` 只写一个匿名命名空间,公开函数的定义放在它后面。
|
|
45
45
|
- 常量要出 `.cpp` 时按四步升:1) 其他翻译单元要的是算出来的答案,给类加查询、表留 `.cpp`;2) 要同一份坐标但仍不是公开契约,放组件 `src/` 的私有头;3) 对方必须写 `Board::kLines` 且这是类型的稳定事实,才用类内 `public static constexpr`;不依赖某个对象的公开常量则用命名空间 `inline constexpr`;4) 第二使用方复用同一套坐标时再抽共享组件。
|
|
@@ -60,10 +60,10 @@
|
|
|
60
60
|
- 半开区间写成两个「变量在左」的比较:`id >= 0 && id < kTaskCount`;不要写 `0 <= id`。
|
|
61
61
|
- 混合优先级算术加显式括号,消除 `readability-math-missing-parentheses`。
|
|
62
62
|
- 内建整数默认用 `int`;需要确定宽度时用 `<cstdint>` 的 `std::int32_t`、`std::int64_t` 等(可省略 `std::` 前缀),可能到 2^31 的值用 64 位;`std::size_t` / `std::ptrdiff_t` 只用于大小和指针差。不要用无符号类型表达「这个值不会为负」。
|
|
63
|
-
- 封闭状态码用 `enum class
|
|
63
|
+
- 封闭状态码用 `enum class`;本项目固定写 `: std::uint8_t` 并逐个写出枚举值(见 `references/language/value-types/04-enum.md`,有意偏离 Google 的默认 `int`),普通枚举用默认底层类型。枚举值总是带作用域:`OrderResult::kSuccess`。
|
|
64
64
|
- 枚举的尾随注释要么全写要么全不写;两种句式与禁止项见 `references/comments/comment-rules.md`「枚举项与 `struct` 短字段:两种句式」。
|
|
65
65
|
- 浮点不用 `==` 比较计算结果;按量纲给容差,金额用最小货币单位的整数。
|
|
66
|
-
-
|
|
66
|
+
- 数值宽度、符号、整数角色、枚举、强类型、`const` 成员和下标访问的完整规则见 `references/language/value-types/01-width.md`、`02-signed.md`、`03-integer-roles.md`、`04-enum.md`、`05-strong-type.md`、`06-const-method.md`、`09-subscript.md`;初始化、RAII、移动和模板见 `references/language/language-basics/`。
|
|
67
67
|
|
|
68
68
|
## 异常
|
|
69
69
|
|
|
@@ -33,13 +33,15 @@ const DOXY_BRIEF = /^\/\/\/\s*@brief\s+\S/
|
|
|
33
33
|
const BLOCK_FILE = /^@file\s+\S/
|
|
34
34
|
const BLOCK_BRIEF = /^@brief\s+\S/
|
|
35
35
|
const MEMBER_LIST = /提供[^。]{0,40}、[^。]{0,40}(的功能|的能力|的接口)|功能[::]/
|
|
36
|
+
const CALLER_LIST = /调用/
|
|
37
|
+
const MEMBER_REF = /`(?:Get|Set|Clear|Is|Has|Can|Completes|Would|Check|ToIndex|Place|Undo|Choose)[A-Za-z0-9_]*`/
|
|
36
38
|
const RATIONALE = /而不是|而非|代价.{0,4}忽略|只留一份|与其|比.{0,12}更|更(?:安全|快|清楚|直接|好写|难写)/
|
|
37
39
|
const FORWARD_REF = /(?:留在|归|交给|由)\s*`?[A-Z][A-Za-z0-9_]*`?/
|
|
38
40
|
const TYPE_SYSTEM = /值语义|自由拷贝/
|
|
39
41
|
const NEGATED_BOUNDARY = /只保存|不拥有|不含/
|
|
40
42
|
const NEGATIVE_SCOPE = /不(?:检查|校验|验证|负责|判断)/
|
|
41
43
|
const DOMAIN_SIZE = /\d+\s*[×xX]\s*\d+|九格|九个格子/
|
|
42
|
-
const PREDICATE_DECL = /^(?:\[\[nodiscard\]\]\s*)?(?:static\s+)?bool\s+(?:Is|Has|Can|Completes)[A-Za-z0-9_]*\s*\(|operator==/
|
|
44
|
+
const PREDICATE_DECL = /^(?:\[\[nodiscard\]\]\s*)?(?:static\s+)?bool\s+(?:Is|Has|Can|Completes|Would)[A-Za-z0-9_]*\s*\(|operator==/
|
|
43
45
|
const isPredicateFirstOk = (declaration, firstDocLine) =>
|
|
44
46
|
firstDocLine.startsWith('检查') || (declaration.includes('operator==') && firstDocLine.startsWith('相等运算符'))
|
|
45
47
|
const DECL_KEYWORDS = /^(class|struct|union|enum|namespace|using|typedef|template|#|if|else|for|while|switch|return|case|default)\b/
|
|
@@ -432,6 +434,9 @@ export function checkText(text, options = {}) {
|
|
|
432
434
|
add(start + 1, 'tag-gap', '同一分组内的 Doxygen 标签之间有空行:参数、返回值、异常、说明各成一组,组间空一行,组内连续')
|
|
433
435
|
}
|
|
434
436
|
const isType = /^(template\s*<[^>]*>\s*)?(class|struct|union|enum)\b/.test(declaration)
|
|
437
|
+
if (!isType && CALLER_LIST.test(block) && MEMBER_REF.test(block)) {
|
|
438
|
+
add(start + 1, 'caller-list', '函数注释在点名调用者:调用关系会随重构过期,只写这个函数做什么、参数、返回值与异常')
|
|
439
|
+
}
|
|
435
440
|
if (RATIONALE.test(block)) {
|
|
436
441
|
add(start + 1, 'rationale', '注释在写设计取舍:方案对比与理由移到正文或实现注释,接口注释只留契约')
|
|
437
442
|
}
|
|
@@ -492,6 +497,9 @@ export function checkText(text, options = {}) {
|
|
|
492
497
|
const declaration = lines[next].trim()
|
|
493
498
|
const block = doc.contents.join('\n')
|
|
494
499
|
const isType = /^(template\s*<[^>]*>\s*)?(class|struct|union|enum)\b/.test(declaration)
|
|
500
|
+
if (!isType && CALLER_LIST.test(block) && MEMBER_REF.test(block)) {
|
|
501
|
+
add(doc.start + 1, 'caller-list', '函数注释在点名调用者:调用关系会随重构过期,只写这个函数做什么、参数、返回值与异常')
|
|
502
|
+
}
|
|
495
503
|
if (RATIONALE.test(block)) add(doc.start + 1, 'rationale', '注释在写设计取舍:方案对比与理由移到正文或实现注释,接口注释只留契约')
|
|
496
504
|
if (isType && MEMBER_LIST.test(block)) add(doc.start + 1, 'member-list', '类型注释在罗列成员功能,应改写为概念与使用约定')
|
|
497
505
|
if (isType && FORWARD_REF.test(block)) add(doc.start + 1, 'forward-ref', '类型注释在交代别的类型负责什么:只写本类型的契约,删掉这句,不要挪到文件头')
|
|
@@ -25,6 +25,7 @@ const SKIP_DIR = new Set([
|
|
|
25
25
|
'.cache', '.tmp', 'temp', 'third_party', 'third-party', 'external', 'vendor', 'deps',
|
|
26
26
|
'cmake-build-debug', 'cmake-build-release', '.vs', '.idea',
|
|
27
27
|
])
|
|
28
|
+
const PREDICATE_NAME = /^(?:Is|Has|Can|Completes|Would)[A-Za-z0-9_]*$/
|
|
28
29
|
|
|
29
30
|
function usage(message) {
|
|
30
31
|
if (message) process.stderr.write(`错误:${message}\n`)
|
|
@@ -237,11 +238,36 @@ export function cppDefinitions(text) {
|
|
|
237
238
|
return definitions
|
|
238
239
|
}
|
|
239
240
|
|
|
241
|
+
/** 检查头文件类内谓词是否排在 Get 之后。 */
|
|
242
|
+
export function checkHeaderOrder(headerText) {
|
|
243
|
+
const classes = findClasses(headerText)
|
|
244
|
+
const issues = []
|
|
245
|
+
for (const item of classes) {
|
|
246
|
+
const members = classMembers(item.body)
|
|
247
|
+
let seenGet = false
|
|
248
|
+
for (const member of members) {
|
|
249
|
+
if (member.name.startsWith('Get')) {
|
|
250
|
+
seenGet = true
|
|
251
|
+
continue
|
|
252
|
+
}
|
|
253
|
+
if (!seenGet || !PREDICATE_NAME.test(member.name)) continue
|
|
254
|
+
const index = headerText.search(new RegExp(`\\b${member.name}\\s*\\(`))
|
|
255
|
+
const line = index === -1 ? 1 : headerText.slice(0, index).split('\n').length
|
|
256
|
+
issues.push({
|
|
257
|
+
line,
|
|
258
|
+
rule: 'header-order',
|
|
259
|
+
message: `${item.name}::${member.name} 是谓词,却排在 Get 之后;谓词 Is / Has / Can / Completes / Would 应放在 Get 之前。`,
|
|
260
|
+
})
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
return issues
|
|
264
|
+
}
|
|
265
|
+
|
|
240
266
|
/** 比较一个头文件与一个实现文件;返回乱序问题。 */
|
|
241
267
|
export function checkPair(headerText, cppText, fileName = '') {
|
|
242
268
|
const classes = findClasses(headerText)
|
|
243
269
|
const definitions = cppDefinitions(cppText)
|
|
244
|
-
const issues =
|
|
270
|
+
const issues = checkHeaderOrder(headerText)
|
|
245
271
|
for (const item of classes) {
|
|
246
272
|
const members = classMembers(item.body)
|
|
247
273
|
if (members.length === 0) continue
|
|
@@ -10,12 +10,12 @@
|
|
|
10
10
|
- 文件槽位、包含守卫与扩展名以 `references/layout/02-file-template.md` 为准;本步只决定声明写什么。
|
|
11
11
|
- 命名以 `references/naming/identifier-names.md` 为准:规则表、语义角色、谓词与查询命名都在那里;字符集、保留标识符与不向 `std` 添加声明见同文件的「字符集与保留标识符」。
|
|
12
12
|
- 声明顺序以 `references/layout/03-declaration-order.md` 为准:类型 → 静态常量 → 工厂 → 构造/赋值/析构 → 其他函数 → 数据成员。组内次序也按同一份的「其他函数的组内次序」:`operator==` 开头、`Get` 按数据成员顺序、先 `ClearSquare` 后 `Clear`;声明区空行按「空行」表,组间 1 行、数据成员间 0 行、`private:` 前 1 行、连续两空行是缺陷。数据成员按初始化依赖、逻辑分组和阅读顺序排列,不按字母序;先声明先初始化,初始化器只能安全使用已经完成初始化的成员。
|
|
13
|
-
- 公共头自包含:自己用到的类型、宏和标准库头都直接包含。验证方式:建一个只包含该头的空 `.cpp` 编译一次,不通过就补 include;五条自检见 `references/layout/02-file-template.md`
|
|
13
|
+
- 公共头自包含:自己用到的类型、宏和标准库头都直接包含。验证方式:建一个只包含该头的空 `.cpp` 编译一次,不通过就补 include;五条自检见 `references/layout/02-file-template.md` 的「自检」。
|
|
14
14
|
- 单参数构造函数写 `explicit`;查询成员函数尾部写 `const`;返回结果对象的 API 加 `[[nodiscard]]`,标注位置与 `friend` 的例外见 `references/language/value-types/07-nodiscard.md`;默认成员初始化写在声明处。默认用花括号初始化:成员声明处一律写 `{}`,只有拷贝初始化或 `auto` 推导时用 `=`,不用 `()` 初始化普通对象。特殊成员(含 `= default`)按声明顺序放在成员组最前,定义位置见 `references/layout/03-declaration-order.md`。
|
|
15
15
|
- 头文件只就地定义约 10 行以内的短函数;模板与 `constexpr` 例外,其余函数体在 `.cpp`。头文件里不写 `using namespace`、非 `inline` 普通全局变量、匿名命名空间或 `static` 定义。
|
|
16
16
|
- 失败是调用方的正常结果时,用结果类型、`bool` + 输出参数或 `std::optional` 表达;函数无法完成自己的任务、或构造函数无法建立不变量时才抛,异常类型的选择与消息见 `references/exceptions/throw-rules.md`。析构与 `swap` 标 `noexcept`,异常类型的拷贝/移动不抛;「拒绝」与「抛」并存时在公共头各写一行。
|
|
17
|
-
- 一次操作有多种必须区分的正常结果时用 `enum class`
|
|
18
|
-
- 声明注释按 `references/comments/comment-rules.md`(内容)与 `comment-style.md`(结构、版式、措辞)写:类型注释、特殊成员、函数注释、数据成员。本步只决定「写哪些事实」:结果、失败方式、检查项、参数与返回值契约;句式、标签、宽度和示例由后两份定,行宽跟 `.clang-format` 的 `ColumnLimit`(默认 80,中文按两列),枚举项与 `struct` 短字段的行尾注释用 `///<` 并与声明对齐。判不准用词时再开 `references/comments/glossary/comment-terms.md`,它只列词表,不重复句式。私有函数同样写声明契约,不能默认调用者知道前置条件;输出参数标 `[out]`,既读又写标 `[in,out]`。
|
|
17
|
+
- 一次操作有多种必须区分的正常结果时用 `enum class` 返回(如落子:成功、玩家不对、终局、空值、占用),不用 `bool` 把原因压掉;成功还要附带数据时再考虑 `std::expected` 或结果结构。纯查询谓词仍用 `bool`。枚举项逐个写值,状态与结果枚举把 0 留给正常态(空、进行中或成功),失败态按固定顺序随后;身份枚举按领域编号(如 X=1、O=2),不套用 0 = 正常态;封闭状态码的枚举默认写 `: std::uint8_t` 并逐个写值,普通枚举用默认底层类型;确需其他 ABI 宽度时写明理由;都不靠第一项默认为 0(见 `references/language/value-types/04-enum.md`);不要用 `bool` 开关参数承载两种以上行为(见 `references/language/value-types/10-float-bool.md`)。
|
|
18
|
+
- 声明注释按 `references/comments/comment-rules.md`(内容)与 `references/comments/comment-style.md`(结构、版式、措辞)写:类型注释、特殊成员、函数注释、数据成员。本步只决定「写哪些事实」:结果、失败方式、检查项、参数与返回值契约;句式、标签、宽度和示例由后两份定,行宽跟 `.clang-format` 的 `ColumnLimit`(默认 80,中文按两列),枚举项与 `struct` 短字段的行尾注释用 `///<` 并与声明对齐。判不准用词时再开 `references/comments/glossary/comment-terms.md`,它只列词表,不重复句式。私有函数同样写声明契约,不能默认调用者知道前置条件;输出参数标 `[out]`,既读又写标 `[in,out]`。
|
|
19
19
|
- 自定义类型的 `std::formatter` 特化放在类型头文件;`parse` 的默认格式限制、`format` 的复用与非法值回退见 `references/language/standard-library/06-format.md` 的「自定义类型的格式化」。
|
|
20
20
|
- 组合消息用 `std::format`;声明里只承诺 `std::format`,`std::print` 是 C++23 的程序边界设施。
|
|
21
21
|
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
|
|
30
30
|
**异常声明**:构造函数、`Get` 类查询、解析函数最容易抛;在声明注释里写「当……时抛出 `std::out_of_range`」这样的具体类型,不给每个参数错误新建类型。
|
|
31
31
|
|
|
32
|
-
**注释**:声明注释用 `///` 写中文散文,只写调用方需要知道的事实,实现细节留给 `.cpp`。文件头、标签、参数完整性与设计论证的规则见 `references/comments/comment-rules.md` 与 `comment-style.md
|
|
32
|
+
**注释**:声明注释用 `///` 写中文散文,只写调用方需要知道的事实,实现细节留给 `.cpp`。文件头、标签、参数完整性与设计论证的规则见 `references/comments/comment-rules.md` 与 `references/comments/comment-style.md`。类型注释先写一句名词短语;纯值容器到此为止,初始状态归默认构造函数,写入与清空条件归对应函数。只有类有不变量才加第二段,且不写别的类型负责什么,也不列「不检查…」这类能力边界。写完运行:
|
|
33
33
|
|
|
34
34
|
```bash
|
|
35
35
|
node "<skill-dir>/scripts/checking-comments.mjs" --root <项目根>
|