bp-coding-best-practices
Installation
Review the source and dependencies with a prompt. A package is available to download.
Install with an AI assistant
Copy the prompt so your assistant can review the pinned revision and dependencies first.
View promptHide prompt
Check whether SkillPI Skill Finder is installed for your current agent. If it is missing, install and enable it using the official instructions at https://github.com/Findskills/skillpi. Then use Skill Finder to locate and install "bp-coding-best-practices" from this exact SkillPI detail page: https://skillpi.com/skills/deepskill-7c700860525c5cba7ab1e87c450c66d46a67ff5ad597478e4be88f76e6e161a5. Verify the upstream source and version against https://github.com/davidYichengWei/agentic-engineering-framework before installing. If they do not match, stop and report the mismatch. Report the installed path and source version.
Source and setup
github.com
Setup guidance pending
See the original source for supported tools, dependencies, and permissions.
Installation test pending
Provide general coding best-practice guidance for writing or reviewing code. Covers readability, naming conventions, function design, control-flow optimization, resource safety management, and commenting conventions.
Introduction source: third_party · davidYichengWei · 0.0.1 Reference
SkillPi interpretation
- Tasks
- Translation pending; see source documentation.
- Testing
- Not tested
Browse files
Frontmatter
name: bp-coding-best-practices
description: 通用编码最佳实践。在编写或 review 代码时使用。涵盖可读性、命名、函数设计、控制流、资源安全、注释规范。通用编码最佳实践
设计原则(SOLID、设计模式):参见 bp-component-design Skill
特定语言/模块规范:参见相应的 standards skills
命名
| 原则 | 说明 |
|---|---|
| 自解释 | retryCount 而非 n |
| 无魔法数字 | const int SECONDS_IN_DAY = 86400; |
| 布尔命名 | isValid, hasAccess(问题形式) |
| 作用域匹配 | 小作用域可短(i),大作用域要描述性 |
函数设计
| 原则 | 说明 |
|---|---|
| 单一职责 | 一个函数做一件事;名字需要 "And" 说明做太多了 |
| 参数精简 | 超过 3-4 个参数 → 考虑结构体封装 |
| const 正确 | 不修改的参数标 const,防止意外修改 |
控制流
Guard Clause:失败情况先处理并返回,主逻辑保持左对齐
Early Return:显式采用 early return 编程范式,尽量将可 early return 的检查前置。
// ❌ 深层嵌套
if (order != nullptr) {
if (order->isValid()) {
if (order->hasItems()) {
// main logic
}
}
}
// ✅ Guard Clause
if (order == nullptr) return;
if (!order->isValid()) return;
if (!order->hasItems()) return;
// main logic (not nested)
资源安全
| 原则 | 说明 |
|---|---|
| RAII | 资源生命周期绑定对象生命周期,避免手动清理分散在多条路径 |
| 所有权显式 | 区分 owner 与 borrower,避免隐式转移所有权 |
| 窄作用域 | 变量声明靠近首次使用,减少悬空与误用概率 |
跨语言场景统一要求:新增分支/返回路径时,必须检查资源契约是否闭环(释放类资源 + 触发类资源)。
新增返回路径的契约检查
当新增 return、early exit 或新分支时,必须逐一检查函数入口处获取的所有"契约性资源"。
契约性资源:函数持有但不拥有、需要在特定时机交还/触发的资源:
- Closure/Callback(需要 Run)
- 锁(需要 Unlock)
- 引用计数(需要 Release)
- 事务上下文(需要 Commit/Rollback/清理)
- 幂等标记/Nonce(需要 Complete)
- 注册到外部管理器的对象(需要 Remove/Unregister)
检查方法:
- 识别:在函数开头找所有"获取但需要交还"的东西
- 对照:找一个功能相似的现有返回路径,逐行对比它处理了哪些资源
- 分类:这个新路径是成功、失败、还是新的第三种状态?现有 ownership 注释是否覆盖?
| ❌ 反例 | ✅ 正例 |
|---|---|
| 新分支只清理了数据结构,忘了 callback 的执行契约 | 对照已有的 early return 路径,发现它调用了 callback->Run(),新路径也需要 |
| 假设"返回成功后调用方会处理 closure" | 检查调用方逻辑,确认 closure 执行责任的真实归属 |
| 只关注"要释放什么",忽略"要触发什么" | 同时检查释放类资源(锁、内存)和触发类资源(回调、事件) |
注释
| 场景 | 做法 |
|---|---|
| 何时写 | 仅当意图不明显时;复杂算法;公共 API |
| 写什么 | Why(为什么这样做),不是 What(做了什么) |
| TODO | 包含上下文和负责人 |
// ❌ 复述代码
// Increment i by 1
++i;
// ✅ 解释意图
// Skip index 0 because it is the sentinel slot.
for (size_t i = 1; i < slots.size(); ++i) { ... }
可观测性(日志)
| 场景 | 做法 |
|---|---|
| 关键分支覆盖 | 至少覆盖无数据快速返回、异常状态转换、错误返回三个分支 |
| 级别选择 | DEBUG 记录成功路径和排障上下文,WARN 记录异常但可恢复路径,ERROR 记录失败路径 |
| 上下文信息 | 日志中携带最小必要上下文(如 request_id、key、error_code),避免无上下文日志 |
进阶
- 可读性详细规则:reference/readability.md
- 安全性详细规则:reference/safety.md
Current listing version
0.0.1
Safety and assessment details
Quality assessment
Out of 100 · Assessment report from SkillPi LAB.
Highlights
- 提供全面的编码最佳实践指南,涵盖命名、函数设计、控制流优化、资源安全管理等多个关键维度
- 适用于广泛的开发者群体,在代码编写与审查场景中具有高频实用价值
Areas to improve
-
description写错位置 Reported severity: Minor
顶层 frontmatter 中 description 字段为空,实际描述内容位于 skill_body 内部
Recommendation: 将 description 内容移至顶层 frontmatter 的 description 字段,确保触发机制能正确读取
Highlights
- 触发词基本覆盖合理,明确指出在编写或审查代码时使用,边界较为清晰
Areas to improve
-
缺少When to Use章节 Reported severity: Minor
文档未包含明确的 When to Use 使用说明章节
Recommendation: 增加 When to Use 章节,明确说明何时以及如何使用本 Skill 的最佳实践
-
缺少端到端示例 Reported severity: Minor
虽然有代码片段示例,但缺乏完整的端到端使用场景示例
Recommendation: 补充一个完整的端到端示例,展示如何在实际项目中应用这些最佳实践
Highlights
- 整体结构清晰,分章节组织良好,配有表格说明和代码对比示例,便于理解与执行
Areas to improve
-
引用文件缺失 Reported severity: Major
SKILL.md 中引用了 reference/readability.md 和 reference/safety.md,但未在 ZIP 包中提供这两个文件
Recommendation: 请确保所有被引用的文件都包含在 Skill 包中,或移除无法提供的引用链接
Highlights
- 作为静态知识文档,无动态执行风险,内容基于通用编程原则,来源可靠且实用性强
The arithmetic mean of the first four original scores, rounded to a whole number.
Static text checks
100 / 100 · skillpi-20261123-no-r3-v1
No current rules matched the text.
This score reflects rule matches in the text and is provided for reference before use. It excludes attachments and runtime behavior.
Permissions and checks before use
Automatically summarized from this main document. Matches may be examples or defensive advice; they are not actual permission declarations. Attachments, external dependencies and runtime behavior are not covered.
These common operations were not identified in the main document. This does not mean no permissions or risks exist.
Security rating and access scope
safe · Static evidence review; not a runtime safety guarantee.
Before use, confirm and authorize actual file access, external services and network destinations in your host. This review grants no runtime permissions.
User discussion
No verified reviews yet.