@chengzhao-dev/dsh-cpp-workflow 0.6.19 → 0.6.20
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 +6 -6
- package/README.md +6 -6
- package/lib/client.js +1 -1
- package/lib/comment-policy.js +1 -1
- package/lib/index.js +1 -1
- package/package.json +3 -3
- package/skills/building-cpp/SKILL.md +26 -13
- package/skills/building-cpp/project/vscode/01-extensions.md +22 -0
- package/skills/building-cpp/project/vscode/02-settings.md +33 -0
- package/skills/building-cpp/references/build-script.md +6 -6
- package/skills/building-cpp/references/environment.md +1 -1
- package/skills/building-cpp/steps/01-build.md +6 -6
- package/skills/building-cpp/steps/02-tooling.md +16 -8
- package/skills/building-cpp/steps/03-check.md +6 -6
- package/skills/developing-cpp/SKILL.md +27 -11
- package/skills/developing-cpp/scripts/checking-comments.mjs +3 -3
- package/skills/developing-cpp/{references/change-model.md → source/01-editing/01-edit-operations.md} +3 -3
- package/skills/developing-cpp/{references/priority.md → source/01-editing/02-rule-priority.md} +1 -1
- package/skills/developing-cpp/{references/google-cpp-style.md → source/02-style/01-google-cpp-style.md} +9 -9
- package/skills/developing-cpp/{references/project-design.md → source/03-design/01-project-design.md} +4 -4
- package/skills/developing-cpp/source/03-design/examples/behavior-sentence.md +14 -0
- package/skills/developing-cpp/source/03-design/examples/coordinate-table.md +31 -0
- package/skills/developing-cpp/source/03-design/examples/header-place.md +26 -0
- package/skills/developing-cpp/source/03-design/examples/library-boundary.md +30 -0
- package/skills/developing-cpp/source/03-design/examples/library-split.md +7 -0
- package/skills/developing-cpp/source/03-design/examples/move-result.md +39 -0
- package/skills/developing-cpp/source/03-design/examples/preview-read.md +40 -0
- package/skills/developing-cpp/{references/layout.md → source/04-layout/01-source-layout.md} +4 -4
- package/skills/developing-cpp/{references/file-template.md → source/04-layout/02-file-template.md} +2 -2
- package/skills/developing-cpp/{references/declaration-order.md → source/04-layout/03-declaration-order.md} +4 -4
- package/skills/developing-cpp/source/05-language/01-language-basics/01-objects.md +9 -0
- package/skills/developing-cpp/source/05-language/01-language-basics/02-initialization.md +19 -0
- package/skills/developing-cpp/source/05-language/01-language-basics/03-deduction.md +6 -0
- package/skills/developing-cpp/source/05-language/01-language-basics/04-const.md +15 -0
- package/skills/developing-cpp/source/05-language/01-language-basics/05-expressions.md +8 -0
- package/skills/developing-cpp/source/05-language/01-language-basics/06-functions.md +9 -0
- package/skills/developing-cpp/source/05-language/01-language-basics/07-classes.md +9 -0
- package/skills/developing-cpp/source/05-language/01-language-basics/08-raii.md +9 -0
- package/skills/developing-cpp/source/05-language/01-language-basics/09-templates.md +20 -0
- package/skills/developing-cpp/source/05-language/01-language-basics/10-checklist.md +9 -0
- package/skills/developing-cpp/source/05-language/02-value-types/01-width.md +11 -0
- package/skills/developing-cpp/source/05-language/02-value-types/02-signed.md +20 -0
- package/skills/developing-cpp/source/05-language/02-value-types/03-integer-roles.md +23 -0
- package/skills/developing-cpp/source/05-language/02-value-types/04-enum.md +17 -0
- package/skills/developing-cpp/source/05-language/02-value-types/05-strong-type.md +17 -0
- package/skills/developing-cpp/source/05-language/02-value-types/06-const-method.md +17 -0
- package/skills/developing-cpp/source/05-language/02-value-types/07-nodiscard.md +9 -0
- package/skills/developing-cpp/source/05-language/02-value-types/08-comparison.md +4 -0
- package/skills/developing-cpp/source/05-language/02-value-types/09-subscript.md +14 -0
- package/skills/developing-cpp/source/05-language/02-value-types/10-float-bool.md +7 -0
- package/skills/developing-cpp/source/05-language/02-value-types/11-checklist.md +8 -0
- package/skills/developing-cpp/source/05-language/03-standard-library/01-containers.md +23 -0
- package/skills/developing-cpp/source/05-language/03-standard-library/02-access.md +8 -0
- package/skills/developing-cpp/source/05-language/03-standard-library/03-iteration.md +12 -0
- package/skills/developing-cpp/source/05-language/03-standard-library/04-algorithms.md +20 -0
- package/skills/developing-cpp/source/05-language/03-standard-library/05-string.md +7 -0
- package/skills/developing-cpp/source/05-language/03-standard-library/06-format.md +44 -0
- package/skills/developing-cpp/source/05-language/03-standard-library/07-time.md +3 -0
- package/skills/developing-cpp/source/05-language/03-standard-library/08-random.md +3 -0
- package/skills/developing-cpp/source/05-language/03-standard-library/09-files.md +4 -0
- package/skills/developing-cpp/source/05-language/03-standard-library/10-checklist.md +9 -0
- package/skills/developing-cpp/source/05-language/04-performance/01-measure.md +15 -0
- package/skills/developing-cpp/source/05-language/04-performance/02-complexity.md +7 -0
- package/skills/developing-cpp/source/05-language/04-performance/03-memory.md +8 -0
- package/skills/developing-cpp/source/05-language/04-performance/04-undefined-behavior.md +20 -0
- package/skills/developing-cpp/source/05-language/04-performance/05-pitfalls.md +11 -0
- package/skills/developing-cpp/source/05-language/04-performance/06-false-optimizations.md +6 -0
- package/skills/developing-cpp/source/05-language/04-performance/07-checklist.md +8 -0
- package/skills/developing-cpp/{references/glossary.md → source/05-language/glossary/language-terms.md} +2 -2
- package/skills/developing-cpp/{references/naming.md → source/06-naming/01-identifier-names.md} +8 -6
- package/skills/developing-cpp/source/06-naming/examples/naming-accessor.md +17 -0
- package/skills/developing-cpp/source/06-naming/examples/naming-basic.md +3 -0
- package/skills/developing-cpp/source/06-naming/examples/naming-current-prefix.md +19 -0
- package/skills/developing-cpp/source/06-naming/examples/naming-lookup.md +18 -0
- package/skills/developing-cpp/source/06-naming/examples/naming-lower-layer.md +39 -0
- package/skills/developing-cpp/source/06-naming/examples/naming-multiple-return.md +20 -0
- package/skills/developing-cpp/source/06-naming/examples/naming-player-mark.md +17 -0
- package/skills/developing-cpp/source/06-naming/examples/naming-predicate.md +18 -0
- package/skills/developing-cpp/source/06-naming/examples/naming-rename.md +5 -0
- package/skills/developing-cpp/source/06-naming/examples/naming-responsibility.md +19 -0
- package/skills/developing-cpp/source/06-naming/examples/naming-role.md +21 -0
- package/skills/developing-cpp/source/06-naming/examples/naming-state-change.md +57 -0
- package/skills/developing-cpp/{references/comments.md → source/07-comments/01-comment-rules.md} +40 -24
- package/skills/developing-cpp/source/07-comments/examples/declaration-class.md +44 -0
- package/skills/developing-cpp/source/07-comments/examples/declaration-constructor.md +22 -0
- package/skills/developing-cpp/source/07-comments/examples/declaration-enum.md +24 -0
- package/skills/developing-cpp/source/07-comments/examples/declaration-file.md +66 -0
- package/skills/developing-cpp/source/07-comments/examples/declaration-function.md +100 -0
- package/skills/developing-cpp/source/07-comments/examples/declaration-member.md +25 -0
- package/skills/developing-cpp/source/07-comments/examples/declaration-predicate.md +13 -0
- package/skills/developing-cpp/source/07-comments/examples/declaration-struct.md +18 -0
- package/skills/developing-cpp/source/07-comments/examples/implementation-body.md +42 -0
- package/skills/developing-cpp/source/07-comments/examples/implementation-todo.md +16 -0
- package/skills/developing-cpp/{references → source/07-comments/glossary}/comment-terms.md +17 -17
- package/skills/developing-cpp/{references/exceptions.md → source/08-exceptions/01-throw-rules.md} +13 -11
- package/skills/developing-cpp/source/08-exceptions/examples/throw-boundary.md +28 -0
- package/skills/developing-cpp/source/08-exceptions/examples/throw-build-mode.md +38 -0
- package/skills/developing-cpp/source/08-exceptions/examples/throw-catch.md +21 -0
- package/skills/developing-cpp/source/08-exceptions/examples/throw-destructor.md +21 -0
- package/skills/developing-cpp/source/08-exceptions/examples/throw-format.md +13 -0
- package/skills/developing-cpp/source/08-exceptions/examples/throw-message.md +17 -0
- package/skills/developing-cpp/source/08-exceptions/examples/throw-when.md +27 -0
- package/skills/developing-cpp/steps/01-behavior.md +5 -5
- package/skills/developing-cpp/steps/02-boundary.md +8 -8
- package/skills/developing-cpp/steps/03-declaration.md +11 -11
- package/skills/developing-cpp/steps/04-implementation.md +14 -14
- package/skills/engineering/SKILL.md +84 -0
- package/skills/{configuring-cmake/references/cmake.md → engineering/cmake/01-targets/01-rules.md} +3 -3
- package/skills/engineering/cmake/01-targets/examples/glob.md +22 -0
- package/skills/engineering/cmake/01-targets/examples/rpath.md +24 -0
- package/skills/{configuring-cmake/examples/cmake.md → engineering/cmake/01-targets/examples/target.md} +1 -69
- package/skills/{configuring-cmake/references/modules.md → engineering/cmake/02-modules/01-functions.md} +2 -2
- package/skills/{configuring-cmake/references/cxx-standard.md → engineering/cmake/03-standard/01-cxx-standard.md} +1 -1
- package/skills/engineering/cmake/03-standard/examples/standard.md +19 -0
- package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/README.md +6 -6
- package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++20/README.md +1 -1
- package/skills/{building-cpp/references/linking.md → engineering/cmake/linking/01-libraries.md} +1 -48
- package/skills/engineering/cmake/linking/02-rpath.md +17 -0
- package/skills/engineering/cmake/linking/03-undefined-reference.md +14 -0
- package/skills/engineering/cmake/linking/04-inspect.md +15 -0
- package/skills/{configuring-cmake → engineering/cmake}/steps/01-targets.md +3 -3
- package/skills/{configuring-cmake → engineering/cmake}/steps/02-modules.md +2 -2
- package/skills/{configuring-cmake → engineering/cmake}/steps/03-check.md +2 -2
- package/skills/{building-cpp/references/testing.md → engineering/cmake/testing/ctest.md} +3 -3
- package/skills/engineering/formatting/02-clang-format.md +14 -0
- package/skills/engineering/formatting/03-clang-tidy.md +51 -0
- package/skills/engineering/formatting/04-clangd.md +83 -0
- package/skills/engineering/formatting/examples/clang-format.md +17 -0
- package/skills/engineering/formatting/examples/clang-tidy.md +64 -0
- package/skills/{building-cpp/examples/tooling.md → engineering/formatting/examples/clangd.md} +1 -80
- package/templates/cpp-workflow-project/SKILL.md +1 -1
- package/skills/building-cpp/references/static-analysis.md +0 -148
- package/skills/configuring-cmake/SKILL.md +0 -68
- package/skills/developing-cpp/examples/comments-declaration.md +0 -313
- package/skills/developing-cpp/examples/comments-implementation.md +0 -66
- package/skills/developing-cpp/examples/exceptions.md +0 -173
- package/skills/developing-cpp/examples/naming.md +0 -168
- package/skills/developing-cpp/examples/structure.md +0 -194
- package/skills/developing-cpp/references/language.md +0 -123
- package/skills/developing-cpp/references/performance.md +0 -83
- package/skills/developing-cpp/references/standard-library.md +0 -138
- package/skills/developing-cpp/references/types.md +0 -154
- /package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++11/README.md +0 -0
- /package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++11/cmake-snippet.cmake +0 -0
- /package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++14/README.md +0 -0
- /package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++14/cmake-snippet.cmake +0 -0
- /package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++17/README.md +0 -0
- /package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++17/cmake-snippet.cmake +0 -0
- /package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++20/cmake-snippet.cmake +0 -0
- /package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++23/README.md +0 -0
- /package/skills/{building-cpp/references → engineering/cmake/03-standard}/standards/c++23/cmake-snippet.cmake +0 -0
- /package/skills/{configuring-cmake/assets/cmake → engineering/cmake/assets}/CompileSettings.cmake +0 -0
- /package/skills/{configuring-cmake/assets/cmake → engineering/cmake/assets}/LinkSettings.cmake +0 -0
- /package/skills/{building-cpp/references/encoding.md → engineering/formatting/01-encoding.md} +0 -0
- /package/skills/{building-cpp → engineering/formatting}/scripts/checking-encoding.mjs +0 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# 项目对整型角色的约定
|
|
2
|
+
|
|
3
|
+
| 角色 | 类型 | 理由 |
|
|
4
|
+
| --- | --- | --- |
|
|
5
|
+
| 不能为负的尺寸、容器长度、`std::array` 长度 | `std::size_t` | 与 STL 和 NTTP 一致 |
|
|
6
|
+
| 坐标等允许表示范围外取值的下标 | `int` | 能表达负值;越界判断交给持有尺寸的类型 |
|
|
7
|
+
| 没有上面两种理由的整数 | `int` | 宽度足够,写起来最短 |
|
|
8
|
+
| 取值很少的 `enum class` 的底层类型 | `std::uint8_t` | 只表达取值集合和布局,不参与算术 |
|
|
9
|
+
|
|
10
|
+
- 协议、文件格式与跨平台布局仍用 `<cstdint>` 的定宽整数(如 `std::int32_t`);工程内普通代码不必写定宽类型。
|
|
11
|
+
- 不用裸 `int` 承担跨平台协议或布局;它的宽度不可预测。
|
|
12
|
+
- 不用 `std::uint8_t` / `std::uint16_t` 当业务编号;负值无法表达,整数提升还会制造意外。
|
|
13
|
+
- 有符号乘法进入容器长度前先显式拓宽,否则会触发 `bugprone-implicit-widening-of-multiplication-result`:
|
|
14
|
+
|
|
15
|
+
```cpp
|
|
16
|
+
inline constexpr int kBatchSize = 3;
|
|
17
|
+
inline constexpr int kItemsPerBatch = 3;
|
|
18
|
+
inline constexpr std::size_t kItemCount =
|
|
19
|
+
static_cast<std::size_t>(kBatchSize) * static_cast<std::size_t>(kItemsPerBatch);
|
|
20
|
+
std::array<TaskState, kItemCount> items_{};
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
- 下标换算在同一函数里先 `static_cast<std::size_t>` 各操作数再乘加;不要为此关闭检查,也不要改业务编号的类型。
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# 枚举类
|
|
2
|
+
|
|
3
|
+
`enum class` 定义封闭取值集合:
|
|
4
|
+
|
|
5
|
+
- 枚举值只在类型作用域内可见:`TaskState::kPending`,不写 `kPending`。
|
|
6
|
+
- 与整数之间不发生隐式转换;需要编号时写 `static_cast<int>(state)`,只用于输出或既有接口。
|
|
7
|
+
- 同类型值可以比较和赋值;不能参与算术。
|
|
8
|
+
- 需要固定布局时写底层类型。编号要在头文件里直接读到,就逐个写出来,不靠第一项默认为 0:`enum class TaskState : std::uint8_t { kPending = 0, kActive = 1, kDone = 2 };`
|
|
9
|
+
|
|
10
|
+
约定:
|
|
11
|
+
|
|
12
|
+
- 新代码用 `enum class`;unscoped `enum` 只用于兼容 C 接口,不作为默认写法。
|
|
13
|
+
- 封闭状态码默认写 `: std::uint8_t`;普通枚举用默认底层类型。
|
|
14
|
+
- 枚举值的行尾注释要么全写要么全不写;两种句式与禁止项见 `source/07-comments/01-comment-rules.md`「枚举项与 `struct` 短字段:两种句式」。
|
|
15
|
+
- 需要“编号”与“名字”两套表示时,写显式映射函数,例如 `TaskStateToName` / `TaskStateFromName`,不要在枚举和整数之间来回隐式转换。
|
|
16
|
+
- 编号按“失败优先”排:空状态或前置条件失败为 0,业务失败随后,成功放在最后。读取端因此可以先判 0,再决定是否继续。
|
|
17
|
+
- 编号只用于输出和既有接口;要从编号找回名字,仍走显式映射函数,不拿编号做算术。
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# 强类型包装
|
|
2
|
+
|
|
3
|
+
同一底层类型承载不同领域含义时,轻量包装能把“不该混用”变成编译错误:
|
|
4
|
+
|
|
5
|
+
```cpp
|
|
6
|
+
struct Meters {
|
|
7
|
+
double value{0.0};
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
struct Seconds {
|
|
11
|
+
double value{0.0};
|
|
12
|
+
};
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
- 包装是聚合,成员公开,仍可构造不合理值;需要不变量时改用 `class` 并在构造函数检查。
|
|
16
|
+
- 取值封闭的领域值用 `enum class`,不要为了“强类型”再包一层。
|
|
17
|
+
- 包装的代价是取值要写 `.value`;只在确实存在混用风险时使用,不为每个 `int` 造类型。
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# 成员函数尾部的 `const`
|
|
2
|
+
|
|
3
|
+
```cpp
|
|
4
|
+
class TaskBoard {
|
|
5
|
+
public:
|
|
6
|
+
[[nodiscard]] TaskState GetTaskState(TaskId id) const;
|
|
7
|
+
void SetTaskState(TaskId id, TaskState state);
|
|
8
|
+
|
|
9
|
+
private:
|
|
10
|
+
std::array<TaskState, kTaskCount> tasks_{};
|
|
11
|
+
};
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
- 尾部 `const` 承诺不修改实例自身;与参数上的 `const`、返回类型上的 `const`、局部变量的 `const` 各管一段。
|
|
15
|
+
- 查询型成员一律标尾部 `const`;修改型成员不标,误标会被编译器拒绝。
|
|
16
|
+
- 返回成员容器的只读视图时,返回 `const` 引用或按值返回;不要返回可变内部引用。
|
|
17
|
+
- `mutable` 只用于缓存、互斥量等“逻辑 const”场景,并在注释里写理由;不要用它绕过正常的 const 设计。
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# `[[nodiscard]]`
|
|
2
|
+
|
|
3
|
+
- 只给“结果携带信息、忽略会出错”的函数:查询型 `const` 成员与返回结果对象的 API。
|
|
4
|
+
- 不给所有非 `void` 函数机械添加;标注的价值来自稀缺。
|
|
5
|
+
- 写法位置:`[[nodiscard]]` 在返回类型前面,然后才是 `static` 或 `constexpr`;参数列表之后才是 `const`、`override`、`noexcept`。
|
|
6
|
+
- 类内 `friend` 不加 `[[nodiscard]]`:Clang 拒绝 `[[nodiscard]] friend`、`friend [[nodiscard]]` 和参数列表后的 `[[nodiscard]]`。不要再猜下一种写法。
|
|
7
|
+
- 运算符不加 `[[nodiscard]]`,成员与 `friend` 都一样。比较的参数不具名:成员写 `bool operator==(const T&) const = default`,`friend` 写 `friend bool operator==(const T&, const T&);`;注释不给这个不具名参数写 `@param`。
|
|
8
|
+
- 不要用 `static_cast<void>` 丢弃 `[[nodiscard]]` 的结果:先接住返回值并分支,把失败分支写出来;不能处理的失败说明接口或合同有问题,不是转换能解决的。测试里验证抛异常时,用 `[[maybe_unused]]` 的局部变量接住,不要用 `static_cast<void>` 跳过约定。
|
|
9
|
+
- 测试里验证“必须处理结果”时,同时验证忽略能被编译器发现。
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# 下标与 `at`
|
|
2
|
+
|
|
3
|
+
同一容器上,`operator[]` 与 `at` 的失败方式不同:
|
|
4
|
+
|
|
5
|
+
| 族 | 容器 | `[]` 越界 | `at` 越界 |
|
|
6
|
+
| --- | --- | --- | --- |
|
|
7
|
+
| 按下标 | `array`、`vector`、`deque`、`string`、`string_view` | 未定义行为 | 抛 `std::out_of_range` |
|
|
8
|
+
| 按键 | `map`、`unordered_map` | 缺键则插入默认值 | 缺键则抛 `std::out_of_range` |
|
|
9
|
+
| 无下标 | `set`、`unordered_set`、`multimap` | 无 `operator[]` / `at` | — |
|
|
10
|
+
|
|
11
|
+
- 默认用 `at()`,越界时抛出 `std::out_of_range`,而不是未定义行为;只有调用点已经证明下标合法、并且这条路径对性能敏感时,才用 `[]`,同时保留证明依据。坐标类型不拥有容器区间;区间判断属于持有尺寸的类型(如 `Board::IsInRange`)。`std::span` 在 C++20 没有 `at`,`span::at` 到 C++26 才进入标准。
|
|
12
|
+
- 已经证明非负的坐标在 `[]` 前收成 `std::size_t`:写 `static_cast<std::size_t>(index)`,不要先转成 `int`;默认仍用 `at()`,只有选择 `[]` 时才做这一步。
|
|
13
|
+
- `map::operator[]` 会插入,不是“读取可能缺失的键”;只读查询用 `find` / `contains` / `at`。
|
|
14
|
+
- 契约失败的消息用 `std::format`,见 `source/08-exceptions/01-throw-rules.md`。
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# 浮点与布尔
|
|
2
|
+
|
|
3
|
+
- 二进制浮点通常不能精确表示十进制小数,`0.1 + 0.2` 不要直接与 `0.3` 用 `==` 比较。
|
|
4
|
+
- 比较计算结果时使用与量纲、误差预算匹配的容差;单独处理 `NaN` 与无穷大。
|
|
5
|
+
- 金额存最小货币单位的整数(如“分”);折扣率、比例、物理量可用 `double`,但要写清精度边界。
|
|
6
|
+
- 整数 `0` 转 `false`,非 `0` 转 `true`;流默认输出 `0` / `1`,需要可读文本时先写 `std::boolalpha`。
|
|
7
|
+
- 不要把 `bool` 当成承载多个状态的整数;互斥状态用 `enum class`。
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# 容器选择
|
|
2
|
+
|
|
3
|
+
标准库不是“能用就行”的工具箱。先确定访问模式、所有权和失败方式,再选容器和算法。
|
|
4
|
+
|
|
5
|
+
| 需求 | 默认选择 | 说明 |
|
|
6
|
+
| --- | --- | --- |
|
|
7
|
+
| 固定长度、编译期已知 | `std::array<T, N>` | 不堆分配,可当聚合成员;长度是类型的一部分 |
|
|
8
|
+
| 可变长度、连续存储 | `std::vector<T>` | 默认容器;随机访问、尾部追加、缓存友好 |
|
|
9
|
+
| 两端增删 | `std::deque<T>` | 分段连续;中间插入仍然昂贵 |
|
|
10
|
+
| 文本 | `std::string` | 拥有字符;修改后 `c_str()` / `data()` 可能失效 |
|
|
11
|
+
| 只读文本视图 | `std::string_view` | 不拥有字符;不能比原字符串活得更久 |
|
|
12
|
+
| 有序键值 | `std::map<K, V>` | 按 key 有序;查找 O(log n);节点独立 |
|
|
13
|
+
| 无序键值 | `std::unordered_map<K, V>` | 平均 O(1),最坏 O(n);迭代顺序不稳定 |
|
|
14
|
+
| 去重集合 | `std::set` / `std::unordered_set` | 只存 key;不要用 `map<K, bool>` 代替 |
|
|
15
|
+
| 连续只读视图 | `std::span<T>` | 不拥有元素;C++20 没有 `at` |
|
|
16
|
+
|
|
17
|
+
选择顺序:
|
|
18
|
+
|
|
19
|
+
1. 先问是否需要稳定顺序、是否需要按 key 查找、是否需要连续内存。
|
|
20
|
+
2. 默认 `vector`;只有剖析或明确复杂度要求时才换。
|
|
21
|
+
3. 需要“可能没有”的返回值时用 `std::optional<T>`,不要用特殊值(`-1`、空字符串)表示缺失。
|
|
22
|
+
4. 需要“成功值或错误原因”时用 `std::expected<T, E>`(C++23)——C++20 项目改用项目自己的结果类型或 `std::variant`,不要假称标准库已有。
|
|
23
|
+
5. 需要多态值集合时用 `std::variant` 或 `std::unique_ptr<Base>`;不要用 `void*`。
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# 访问与边界
|
|
2
|
+
|
|
3
|
+
- `operator[]` 不检查边界,越界是未定义行为;`at` 检查并抛 `std::out_of_range`。规则与家族差异见 `source/05-language/02-value-types/09-subscript.md`。
|
|
4
|
+
- `vector::front()` / `back()` / `data()` 在空容器上未定义;先 `empty()`。
|
|
5
|
+
- `map::operator[]` 缺键会插入默认值;只读查询用 `find`、`contains`(C++20)或 `at`。
|
|
6
|
+
- `string_view` 不保证以 `\0` 结尾;传给需要 C 字符串的 API 前先转成 `std::string`。
|
|
7
|
+
- `span` 不拥有元素,也不延长生命周期;不要从临时容器构造后保存。
|
|
8
|
+
- 迭代器区间是半开的 `[first, last)`;`end()` 不指向元素,不能解引用。
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# 迭代与失效
|
|
2
|
+
|
|
3
|
+
- 范围 `for` 默认按值复制元素;大对象写 `const auto&`,需要修改写 `auto&`。
|
|
4
|
+
- 在范围 `for` 里修改容器大小会使迭代器失效;先收集要改的 key,再统一修改。
|
|
5
|
+
- `vector` 扩容会使所有指针、引用、迭代器失效;`reserve` 只能减少扩容次数,不能保证永不失效。
|
|
6
|
+
- `map` / `unordered_map` 插入不会使其他元素的引用失效;`unordered_map` 的 rehash 会使迭代器失效。
|
|
7
|
+
- 删除元素用 `erase-remove` 惯用法或 C++20 `std::erase` / `std::erase_if`;不要在遍历中直接 `erase` 当前迭代器以外的元素。
|
|
8
|
+
|
|
9
|
+
```cpp
|
|
10
|
+
std::vector<int> values{1, 2, 3, 4, 5};
|
|
11
|
+
std::erase_if(values, [](int value) { return value % 2 == 0; });
|
|
12
|
+
```
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# 算法优先于手写循环
|
|
2
|
+
|
|
3
|
+
能用标准算法表达意图时,不写下标循环:
|
|
4
|
+
|
|
5
|
+
| 意图 | 算法 |
|
|
6
|
+
| --- | --- |
|
|
7
|
+
| 查找 | `std::find` / `std::find_if` |
|
|
8
|
+
| 计数 | `std::count` / `std::count_if` |
|
|
9
|
+
| 任一/全部 | `std::any_of` / `std::all_of` / `std::none_of` |
|
|
10
|
+
| 变换 | `std::transform` |
|
|
11
|
+
| 排序 | `std::sort` / `std::stable_sort` |
|
|
12
|
+
| 最值 | `std::min_element` / `std::max_element` |
|
|
13
|
+
| 累加 | `std::accumulate`(注意初始值类型) |
|
|
14
|
+
| 二分查找 | `std::lower_bound` / `std::upper_bound`(要求已排序) |
|
|
15
|
+
|
|
16
|
+
- 算法接收迭代器区间,不接收容器;调用时写 `values.begin(), values.end()` 或 C++20 `std::ranges::sort(values)`。
|
|
17
|
+
- 嵌套数组整体赋同一个值时用 `std::ranges::fill` 与 `std::views::join`;逐格做不同处理时仍用 `row` / `col` 循环。
|
|
18
|
+
- 谓词只做判断,不改元素;需要修改时用 `transform`。
|
|
19
|
+
- 捕获列表只捕获需要的变量;默认 `[&]` 容易悬空,默认 `[=]` 容易复制大对象。
|
|
20
|
+
- 不在比较函数里写有副作用的逻辑;排序要求严格弱序,违反会导致未定义行为。
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# `std::string` 与 `std::string_view`
|
|
2
|
+
|
|
3
|
+
- 函数只读文本时参数用 `std::string_view`;需要保存或修改时用 `std::string`。
|
|
4
|
+
- `string_view` 不拥有内存;不要返回指向局部 `std::string` 的 `string_view`,也不要把临时 `std::string` 转成 `string_view` 后保存。
|
|
5
|
+
- 拼接少量文本用 `std::string` 的 `+=` 或 `std::format`;大量拼接先 `reserve`。
|
|
6
|
+
- 查找子串用 `find`,找不到返回 `npos`;不要用 `-1` 比较。
|
|
7
|
+
- 数值与文本互转用 `std::to_string` / `std::from_chars` / `std::format`;`std::stoi` 会抛异常且不报告消耗长度。
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# `std::format` 与输出
|
|
2
|
+
|
|
3
|
+
- 组合消息优先 `std::format`;它返回 `std::string`,不直接写流。
|
|
4
|
+
- 输出仍走 `std::cout` / `std::cerr` / 项目传入的 `std::ostream&`;C++23 且库支持时程序边界可用 `std::print`,C++20 及更早没有。
|
|
5
|
+
- 格式串里的 `{}` 按顺序取参数;需要位置时写 `{0}`、`{1}`。
|
|
6
|
+
- 自定义类型要格式化时,特化 `std::formatter<T>`;写法与边界见下面的「自定义类型的格式化」。
|
|
7
|
+
- 浮点默认格式可能与预期不同;需要固定精度时写 `{:.2f}`,需要科学计数时写 `{:e}`。
|
|
8
|
+
- 异常消息、日志、用户界面文本都通过 `std::format` 组合,不手工拼接数字和字符串。异常消息的组织方式见 `source/08-exceptions/01-throw-rules.md` 的「消息」节;面向用户的中文只留在程序入口的前缀里。
|
|
9
|
+
|
|
10
|
+
## 自定义类型的格式化
|
|
11
|
+
|
|
12
|
+
- 在类型所在的头文件里特化 `std::formatter<T>`,与类型声明放在一起;输出属于类型合同,不散落在调用点的 `operator<<` 或手工拼接里。
|
|
13
|
+
- `parse` 只接受默认格式:读到 `}` 或格式串结束时返回迭代器;其他说明符抛 `std::format_error`,消息写清只支持默认格式。需要字段对齐、宽度等语法时再实现,不预埋。
|
|
14
|
+
- `format` 用 `std::format_to(ctx.out(), ...)` 输出;内部复用已有类型的 formatter,不重复拼接坐标或枚举文本。`Move` 只组合 `Player` 与 `Position`,不重新拼 `(row, col)`。
|
|
15
|
+
- 枚举的非法值要有稳定回退,例如 `Player(9)`;不要输出空串,也不要让调用方从越界里猜原因。
|
|
16
|
+
- 输出形态短且稳定,例如 `X(1, 2)`;异常消息与测试都用 `std::format("{}", value)` 直接断言。
|
|
17
|
+
|
|
18
|
+
```cpp
|
|
19
|
+
template <>
|
|
20
|
+
struct std::formatter<tictactoe::Player> {
|
|
21
|
+
/// 只接受默认格式;其他说明符抛出 std::format_error。
|
|
22
|
+
auto parse(std::format_parse_context& ctx) {
|
|
23
|
+
if (ctx.begin() != ctx.end() && *ctx.begin() != '}') {
|
|
24
|
+
throw std::format_error("Player only supports the default format");
|
|
25
|
+
}
|
|
26
|
+
return ctx.begin();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/// 把玩家格式化后写入输出上下文。
|
|
30
|
+
auto format(tictactoe::Player player, std::format_context& ctx) const {
|
|
31
|
+
switch (player) {
|
|
32
|
+
case tictactoe::Player::kX:
|
|
33
|
+
return std::format_to(ctx.out(), "X");
|
|
34
|
+
case tictactoe::Player::kO:
|
|
35
|
+
return std::format_to(ctx.out(), "O");
|
|
36
|
+
}
|
|
37
|
+
return std::format_to(ctx.out(), "Player({})", static_cast<int>(player));
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
`parse` 直接返回 `ctx.begin()` 只适用于「只接受默认格式」;要自定义拒绝消息时先检查第一个字符是不是 `}`。`format` 的返回类型由 `std::format_to` 推导,不写死 `char*`。
|
|
43
|
+
|
|
44
|
+
依据:[cppreference: std::formatter](https://en.cppreference.com/w/cpp/utility/format/formatter) 与 [P3070R2: Formatting enums](https://open-std.org/JTC1/SC22/WG21/docs/papers/2025/p3070r2.html)。
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# 检查清单
|
|
2
|
+
|
|
3
|
+
- [ ] 容器选择有明确理由,默认是 `vector`。
|
|
4
|
+
- [ ] 访问前有边界证明,或使用 `at`。
|
|
5
|
+
- [ ] 没有保存 `string_view` / `span` 指向的临时对象。
|
|
6
|
+
- [ ] 修改容器大小前处理了迭代器失效。
|
|
7
|
+
- [ ] 能用算法表达时没有手写下标循环。
|
|
8
|
+
- [ ] 组合消息用 `std::format` 或流输出;`std::print` 只出现在 C++23 的程序边界,库代码没有。
|
|
9
|
+
- [ ] 自定义类型的 formatter 只接受默认格式,非法枚举有稳定回退,输出可直接用 `std::format` 断言。
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# 测量顺序
|
|
2
|
+
|
|
3
|
+
先证明慢在哪里,再改代码。没有测量的“优化”通常只是把复杂度搬了个位置。
|
|
4
|
+
|
|
5
|
+
本组条文只管性能判断与测量;语言规则见 `source/05-language/01-language-basics/01-objects.md`,容器与算法选择见 `source/05-language/03-standard-library/01-containers.md`,静态检查开关见 `engineering` 的 `formatting/03-clang-tidy.md`。
|
|
6
|
+
|
|
7
|
+
1. 写清可观察的性能目标:延迟、吞吐、内存上限,或“输入规模翻倍时耗时如何增长”。
|
|
8
|
+
2. 用真实规模的输入测量;玩具数据上的常数差异没有意义。
|
|
9
|
+
3. 先看算法复杂度,再看内存访问,再看常数因子。
|
|
10
|
+
4. 用 profiler 找热点;不要凭代码长短猜。
|
|
11
|
+
5. 改一处,复测同一指标;记录测量条件和结果。
|
|
12
|
+
|
|
13
|
+
- 发布构建(`-O2` / `-O3`)测性能,调试构建测正确性。
|
|
14
|
+
- 微基准要防止编译器把结果优化掉;使用输入、累加结果并输出。
|
|
15
|
+
- 单次测量受缓存、频率、调度影响;取多次的中位数或分位数,不只报最好的一次。
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# 复杂度与数据结构
|
|
2
|
+
|
|
3
|
+
- 先问操作的渐进复杂度:`vector` 尾部追加摊还 O(1),中间插入 O(n);`map` 查找 O(log n);`unordered_map` 平均 O(1)、最坏 O(n)。
|
|
4
|
+
- 嵌套循环里做线性查找通常是把 O(n) 变成 O(n²);先建索引或排序。
|
|
5
|
+
- 频繁按 key 查找时,`unordered_map` 通常比 `map` 快,但迭代顺序不稳定;需要有序输出时不要选它。
|
|
6
|
+
- 小规模数据(几十个元素)上,连续 `vector` 的线性扫描常比哈希表更快;不要机械套用复杂度。
|
|
7
|
+
- 递归深度可能随输入增长时,改成迭代或显式栈;栈溢出不是可捕获的异常。
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# 内存与缓存
|
|
2
|
+
|
|
3
|
+
- 连续存储对缓存友好;`vector<struct>` 比 `vector<unique_ptr<struct>>` 更容易预取。
|
|
4
|
+
- 只遍历部分字段时,AoS(数组的结构)可能浪费带宽;数据量大且按列访问时考虑 SoA(结构的数组),但先用 profiler 证明。
|
|
5
|
+
- 小对象按值传递通常比堆分配快;不要为了“避免拷贝”给所有参数加指针。
|
|
6
|
+
- `reserve` 减少 `vector` 扩容和搬移;只有已知上界或能准确估算时才用。
|
|
7
|
+
- 移动大对象比拷贝便宜,但移动不是免费;不要把 `std::move` 当成优化开关。
|
|
8
|
+
- 返回局部对象直接 `return value;`,依赖 RVO / NRVO;`return std::move(value);` 会阻止优化。
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# 未定义行为与安全
|
|
2
|
+
|
|
3
|
+
以下行为没有标准结果,优化后可能表现为“有时对”:
|
|
4
|
+
|
|
5
|
+
- 数组/容器越界访问(`operator[]`、指针算术)。
|
|
6
|
+
- 解引用空指针或已释放对象(use-after-free、double free)。
|
|
7
|
+
- 读取未初始化变量。
|
|
8
|
+
- 有符号整数溢出。
|
|
9
|
+
- 数据竞争:多个线程无同步地读写同一对象。
|
|
10
|
+
- 用 `reinterpret_cast` 把对象当无关类型访问,违反严格别名规则。
|
|
11
|
+
- 对象生存期结束后继续使用指针或引用。
|
|
12
|
+
|
|
13
|
+
工具:
|
|
14
|
+
|
|
15
|
+
- AddressSanitizer(ASan)查越界、use-after-free、double free。
|
|
16
|
+
- UndefinedBehaviorSanitizer(UBSan)查有符号溢出、空指针、对齐等问题。
|
|
17
|
+
- ThreadSanitizer(TSan)查数据竞争。
|
|
18
|
+
- `-Wall -Wextra -Wpedantic` 打开常用警告;不要靠关闭警告“修复”问题。
|
|
19
|
+
|
|
20
|
+
ASan/UBSan 有运行期开销,只在测试和排查时开;发布构建仍要保留编译器警告。
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# 常见性能陷阱
|
|
2
|
+
|
|
3
|
+
| 现象 | 常见原因 | 先做什么 |
|
|
4
|
+
| --- | --- | --- |
|
|
5
|
+
| 循环里反复分配 | 每次迭代构造容器/字符串 | 移到循环外,或 `reserve` |
|
|
6
|
+
| 大量拷贝 | 范围 `for` 按值、参数按值传大对象 | 改 `const&`,确认所有权 |
|
|
7
|
+
| 哈希表慢 | 哈希冲突、频繁 rehash | 预留桶数,检查哈希质量 |
|
|
8
|
+
| 字符串拼接慢 | 反复 `+` 产生临时对象 | `reserve` + `+=`,或 `std::format` 一次成型 |
|
|
9
|
+
| 多线程没有变快 | 锁竞争、伪共享 | 缩小临界区,按缓存行对齐 |
|
|
10
|
+
| 递归慢/栈溢出 | 重复子问题、深度大 | 记忆化或改迭代 |
|
|
11
|
+
| 输出慢 | 每行 flush、频繁 `std::endl` | 用 `'\n'`,需要时再 flush |
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# 术语
|
|
2
2
|
|
|
3
|
-
代码标识符用英文;注释和说明用中文时,同一个概念只用一种译法。下表是本项目的默认译法,项目技能可以增补,但不要改已有词的含义。标识符怎么拼(大小写、前缀、语义角色)见 `
|
|
3
|
+
代码标识符用英文;注释和说明用中文时,同一个概念只用一种译法。下表是本项目的默认译法,项目技能可以增补,但不要改已有词的含义。标识符怎么拼(大小写、前缀、语义角色)见 `source/06-naming/01-identifier-names.md`;本表只管术语译法与近义词辨析。
|
|
4
4
|
|
|
5
5
|
| 英文 | 中文 | 说明 |
|
|
6
6
|
| --- | --- | --- |
|
|
@@ -84,5 +84,5 @@
|
|
|
84
84
|
- **声明 vs 定义**:头文件里多是声明;函数体、全局变量初始化是定义。
|
|
85
85
|
- **参数 vs 实参**:声明里叫参数(parameter),调用点叫实参(argument);中文口语都叫参数时,写文档要区分。
|
|
86
86
|
- **接口 vs 实现**:公共头是接口,`.cpp` 是实现;不要用「接口」指整个类。
|
|
87
|
-
- **异常 vs 错误**:异常是语言机制,错误是结果;是否用异常表达错误由接口合同和 invariant 决定,不能把两者当同义词。规则见 `
|
|
87
|
+
- **异常 vs 错误**:异常是语言机制,错误是结果;是否用异常表达错误由接口合同和 invariant 决定,不能把两者当同义词。规则见 `source/08-exceptions/01-throw-rules.md`。
|
|
88
88
|
- **格式化 vs 排版**:`std::format` 组合消息;clang-format 排版源码。不要混用「格式化」指代两者。
|
package/skills/developing-cpp/{references/naming.md → source/06-naming/01-identifier-names.md}
RENAMED
|
@@ -17,11 +17,12 @@
|
|
|
17
17
|
|
|
18
18
|
- 文件名用 snake_case:`order_result.h`、`task_table.cpp`;测试文件 `task_table_test.cpp`。
|
|
19
19
|
- 查询名与返回类型一致:返回标记时写 `GetCurrentMark`,返回玩家时写 `GetCurrentPlayer`;不写项目里没有对应类型的 `GetPlayer`。
|
|
20
|
-
- 玩家互换用 `GetOpponent`,不写 `OtherPlayer
|
|
20
|
+
- 玩家互换用 `GetOpponent`,不写 `OtherPlayer`;玩家换成格子状态用 `PlayerToCellState`,不写 `CellStateOf`。名字要点明两端角色。
|
|
21
21
|
- 布尔变量用 `is_` / `has_` / `can_` 开头:`is_valid`、`has_stock`;不要用 `flag`、`status`。
|
|
22
22
|
- 容器用复数:`tasks`、`orders`;单个元素用单数。
|
|
23
23
|
- 缩写按单词处理:`url`、`id`、`http`,不要写 `URL` 混进 snake_case;类型名里可以 `HttpClient`。
|
|
24
|
-
-
|
|
24
|
+
- 不用单字母名:坐标参数和局部变量用 `pos`(`position` 的常见缩写),行、列用 `row`、`col`;公开数据成员仍用完整名词(`Move::position`),避免 API 缩写。`i` / `j` 只用于没有领域名字的计数(如遍历固定长度的下标)。也不要用 `data`、`info`、`tmp` 这类不说明内容的名字。
|
|
25
|
+
- 参数或局部变量的类型已在签名里写明时,用主流缩写而不是重复类型名:`Position pos`,不写 `Position position`;缩写只在含义无歧义时使用。
|
|
25
26
|
|
|
26
27
|
## 语义角色
|
|
27
28
|
|
|
@@ -52,9 +53,9 @@
|
|
|
52
53
|
- 前缀集就是这四类:`Is` / `Has` / `Can`,外加与成对规则一一对应的 `Completes`、`Would` 这类限定式;`Contains` 只保留给容器成员(`std::vector::empty` 一类已有标准名),不拿它当一般谓词。
|
|
53
54
|
- `Get` 只用于取值,`Set` 只用于设值;不要用 `CheckLine()` 当布尔 getter。
|
|
54
55
|
- `Check` / `Validate` 留给会报告错误、产生诊断或可能失败的操作;它们不返回纯 `bool` 查询。
|
|
55
|
-
- 动作可能因为多个前置条件失败、而调用方需要区分原因时,返回 `enum class`(`Cell::
|
|
56
|
-
- 结果枚举的取值用同一种句式:主体 + 状态,每个取值两个词以内(`
|
|
57
|
-
- 假设性查询在名字里写明条件:`PreviewWin(
|
|
56
|
+
- 动作可能因为多个前置条件失败、而调用方需要区分原因时,返回 `enum class`(`Cell::PlaceResult`、`WinPreviewResult`),不要用 `bool` 加输出参数,也不要拆成一串 `IsXxx` 判断再重复检查。
|
|
57
|
+
- 结果枚举的取值用同一种句式:主体 + 状态,每个取值两个词以内(`kRejected`、`kOccupied`、`kUpdated`);失败值写原因,成功值写结果,不堆叠 `Rejected` 这类重复修饰。完整案例(`PlaceResult`、`Place`、`Clear` 与 `kRejected` 的改名过程)见 `source/06-naming/examples/naming-state-change.md`。
|
|
58
|
+
- 假设性查询在名字里写明条件:`PreviewWin(pos, cell)` 读作「这一次落子会成线吗」,返回 `WinPreviewResult` 区分「不能落子」与「能落但不赢」;只有成功与不报错的纯判断才用 `bool`。同一 API 只选一种,改名时同步声明、定义、调用点、测试和注释。
|
|
58
59
|
- 私有谓词同样用肯定式;不要用 `flag`、`status` 或 `check` 这类不说明判据的名字。
|
|
59
60
|
|
|
60
61
|
## 类型命名的判断顺序
|
|
@@ -62,6 +63,7 @@
|
|
|
62
63
|
1. 纯数据、无 invariant、可以直接聚合初始化 → `struct`,名字是名词:`Order`, `TaskRecord`。
|
|
63
64
|
2. 有 invariant、需要构造函数维持 → `class`,名字是名词:`TaskTable`, `Warehouse`。
|
|
64
65
|
3. 封闭的状态集合 → `enum class`,名字是单数名词:`TaskState`, `OrderResult`;枚举子用 `k` 前缀。
|
|
66
|
+
- 类型名用拥有它的那一层的词汇:`Board` 的返回值是 `BoardState`,不写 `GameResult`;`Game` 组合 `Board`,`Board` 不应出现 `Game` 前缀。反例见 `source/06-naming/examples/naming-lower-layer.md`。
|
|
65
67
|
4. 只有行为、没有状态,且调用方需要替换实现 → 抽象基类;只有一个实现时先用普通类。
|
|
66
68
|
5. 一组自由函数 → 命名空间 + 动词;不要为了「像 Java」造一个 `XxxUtil` 类。
|
|
67
69
|
|
|
@@ -73,4 +75,4 @@
|
|
|
73
75
|
|
|
74
76
|
## 术语
|
|
75
77
|
|
|
76
|
-
中文术语与英文标识符的对应见 `
|
|
78
|
+
中文术语与英文标识符的对应见 `source/05-language/glossary/language-terms.md`。代码标识符用英文;注释可以用中文,但同一个概念只用一种译法。
|
|
@@ -0,0 +1,17 @@
|
|
|
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
|
+
查询统一 `Get` + PascalCase;修改用领域动词。只有真正需要替换值时,才考虑 `Set` 形式,并说明为什么不是动作。
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# 正反例:查找与读取
|
|
2
|
+
|
|
3
|
+
**差**:
|
|
4
|
+
|
|
5
|
+
```cpp
|
|
6
|
+
[[nodiscard]] Task GetTask(std::string_view name) const; // 可能找不到时返回什么?
|
|
7
|
+
[[nodiscard]] bool FindTask(std::string_view name) const; // 返回 bool 却叫 Find
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
**好**:
|
|
11
|
+
|
|
12
|
+
```cpp
|
|
13
|
+
[[nodiscard]] const Task& GetTask(std::string_view name) const; // 找不到抛 TaskNotFoundError
|
|
14
|
+
[[nodiscard]] const Task* FindTask(std::string_view name) const; // 找不到返回 nullptr
|
|
15
|
+
[[nodiscard]] bool HasTask(std::string_view name) const; // 只回答存在性
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
`Get` 表示结果一定存在(否则抛出声明过的领域异常),`Find` 表示可能找不到,`Has` / `Contains` 只回答是否存在。不要用 `Find` 返回 `bool`。
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# 正反例:下层类型不要借用上层词汇
|
|
2
|
+
|
|
3
|
+
**差**:`Board` 定义并返回 `GameResult`。
|
|
4
|
+
|
|
5
|
+
```cpp
|
|
6
|
+
// board.h:棋盘定义了对局结果
|
|
7
|
+
enum class GameResult : std::uint8_t {
|
|
8
|
+
kInProgress = 0,
|
|
9
|
+
kXWins = 1,
|
|
10
|
+
kOWins = 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
|
+
enum class BoardState : std::uint8_t {
|
|
27
|
+
kInProgress = 0,
|
|
28
|
+
kXWins = 1,
|
|
29
|
+
kOWins = 2,
|
|
30
|
+
kDraw = 3,
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
class Board {
|
|
34
|
+
public:
|
|
35
|
+
[[nodiscard]] BoardState GetState() const;
|
|
36
|
+
};
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
`Board` 的返回值用 `BoardState`;`Game` 需要「对局结果」时在 `Game` 层组合或另起名字,不把 `Game` 的词汇塞进 `Board`。`GameResult`、`GameState`、`PlayerMove` 这类「上层前缀 + 下层类型」都按反例处理。
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# 正反例:多返回值
|
|
2
|
+
|
|
3
|
+
**差**:
|
|
4
|
+
|
|
5
|
+
```cpp
|
|
6
|
+
bool Parse(std::string_view text, int* out_value); // 出参 + 返回 bool
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
**好**:
|
|
10
|
+
|
|
11
|
+
```cpp
|
|
12
|
+
struct ParseResult {
|
|
13
|
+
bool ok{false};
|
|
14
|
+
int value{0};
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
[[nodiscard]] ParseResult TryParse(std::string_view text);
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
需要同时表达成功与结果时,用带字段的结构体或 `std::optional`,不要用输出参数加布尔返回。
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# 正反例:玩家与标记
|
|
2
|
+
|
|
3
|
+
**差**:
|
|
4
|
+
|
|
5
|
+
```cpp
|
|
6
|
+
Player OtherPlayer(Player player); // 另一端是谁不明显
|
|
7
|
+
CellState CellStateOf(Player player); // 从什么换到什么没写全
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
**好**:
|
|
11
|
+
|
|
12
|
+
```cpp
|
|
13
|
+
[[nodiscard]] constexpr Player GetOpponent(Player player) noexcept;
|
|
14
|
+
[[nodiscard]] constexpr CellState PlayerToCellState(Player player) noexcept;
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
玩家互换用 `GetOpponent`;玩家换成格子状态用 `PlayerToCellState`。查询名与返回类型一致:返回 `Player` 写 `GetCurrentPlayer`,返回 `CellState` 写 `GetCurrentMark`。
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# 正反例:谓词与 Lambda
|
|
2
|
+
|
|
3
|
+
**差**:
|
|
4
|
+
|
|
5
|
+
```cpp
|
|
6
|
+
bool valid(int value);
|
|
7
|
+
auto check = [](const Task& task) { return task.done; };
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
**好**:
|
|
11
|
+
|
|
12
|
+
```cpp
|
|
13
|
+
[[nodiscard]] bool IsValid(int value);
|
|
14
|
+
auto is_done = [](const Task& task) { return task.done; };
|
|
15
|
+
auto has_owner = [](const Task& task) { return !task.owner.empty(); };
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
返回 `bool` 的自由函数用 `Is` / `Has` / `Can` / `Should` 开头;Lambda 用 snake_case 的 `is_` / `has_` / `can_`,读起来像条件。
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# 正反例:名字与职责
|
|
2
|
+
|
|
3
|
+
**差**:
|
|
4
|
+
|
|
5
|
+
```cpp
|
|
6
|
+
class DataManager {}; // 管什么?
|
|
7
|
+
class StringHelper {}; // 为什么是类?
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
**好**:
|
|
11
|
+
|
|
12
|
+
```cpp
|
|
13
|
+
class Warehouse {}; // 持有库存,维持不变量
|
|
14
|
+
namespace string_utils { // 一组自由函数
|
|
15
|
+
[[nodiscard]] std::string Trim(std::string_view text);
|
|
16
|
+
}
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
避免 `Manager`、`Helper`、`Util`;名字要能回答「它负责什么」。一组无状态函数用命名空间,不要造类。
|