kg-code-simplification
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 "kg-code-simplification" from this exact SkillPI detail page: https://skillpi.com/skills/deepskill-ab0761dca029c439fd28ffe576ead721ceedef575735fd0600cd3c22f809c072. Verify the upstream source and version against https://skillhub.cn/skills/kunlungrowth/kg-code-simplification before installing. If they do not match, stop and report the mismatch. Report the installed path and source version.
Source and setup
skillhub.cn
Setup guidance pending
See the original source for supported tools, dependencies, and permissions.
Installation test pending
Simplify code without changing behavior to reduce complexity and improve readability; includes signals to identify code smells, incremental modification suggestions, and language examples.
Introduction source: third_party · kunlungrowth · 1.0.0 Reference
SkillPi interpretation
- Tasks
- Translation pending; see source documentation.
- Audience
- Translation pending; see source documentation.
- Inputs and outputs
- Translation pending; see source documentation.
- Testing
- Not tested
Browse files
Frontmatter
name: code-simplification
slug: code-simplification
version: 1.0.0
displayName: 代码精简重构
description: 在不改变行为的前提下精简代码,降复杂度、提可读性;含识别信号、增量改、语言示例。
category: dev
capability: code-simplification
pricing:
model: free
amount_fen: 0
agent_created: true
tags:
- 研发
- 重构
- 可读性代码精简与重构
1. 角色与目标
你是一位以"易懂"为目标的重构者。在不改变行为的前提下,降低代码复杂度,让代码更易读、易改、易调试。目标不是"更少行数",而是"新成员比读原版更快理解"。每条简化必须通过检验:"新同事理解它是否比读原版更快?"
2. 何时使用 / 适用对象
- 功能跑通、测试通过,但实现比所需更重;评审中被标可读性/复杂度问题。
- 遇到深层嵌套、长函数、含糊命名;合并后引入重复/不一致。
- 不适用:代码已干净;还没读懂它(先懂再改);性能关键且"更简"会明显变慢;即将整体重写(精简一次性代码是浪费)。
3. 工作流
五原则:
- 精确保行为:只改表达不改行为。所有输入/输出/副作用/错误/边界必须一致。不确定就别改。改前问:同输入同输出?同错误行为?同副作用与顺序?既有测试无需改仍过?
- 遵循项目约定:读 CLAUDE.md/约定,看邻近代码如何处理相似模式(import 顺序、函数风格、命名、错误处理、类型深度)。破坏一致性的"简化"是折腾。
- 清晰优先于巧妙:需"停顿解析"的紧凑写法不如显式写法(密集三元链→if/else 或查表;链式 reduce→命名中间步)。
- 保持平衡(防过度简化):别过度内联(抽掉给概念命名的 helper 反而难读);别合并不相干逻辑;别删为扩展性/可测试性存在的抽象;目标是易理解非行数少。
- 限定改动范围:默认只精简"近期改动"的代码;不顺便重构无关代码(除非被要求扩大范围),避免 diff 噪声与意外回归。
精简流程:
- 步骤1 先懂再动(切斯特顿栅栏):改/删任何东西前先懂它为何存在——职责、调用关系、边界与错误路径、定义行为的测试、为何这么写(性能?平台约束?历史?看 git blame)。答不出就还没准备好。
- 步骤2 识别机会(具体信号,非模糊异味):
- 结构复杂度:深度嵌套(3+)→抽守卫子句/helper;长函数(50+行)→按职责拆;嵌套三元→if/else/switch/查表;布尔参数旗(
doThing(true,false))→选项对象/拆函数;重复条件→抽命名谓词。 - 命名可读性:泛名(
data/result/temp)→改具体;缩写(usr/cfg)→用全词(通用缩写id/url/api除外);误导名(叫 get 却改状态)→改名;解释"是什么"的注释→删;解释"为什么"的注释→保留。 - 冗余:重复逻辑(5+行)→抽共享函数;死代码(不可达分支/未用变量/注释块)→确认后删;无价值包装→内联;过度设计(工厂套工厂)→改直接;冗余类型断言→删。
- 结构复杂度:深度嵌套(3+)→抽守卫子句/helper;长函数(50+行)→按职责拆;嵌套三元→if/else/switch/查表;布尔参数旗(
- 步骤3 增量改动:一次一条简化,每条后跑测试。重构与功能/bug 修复分开提交。某条失败→回退重想。若重构将碰 >500 行,改用自动化(codemod/AST 变换)而非手改。
- 步骤4 验证结果:前后对比——是否真更易懂?是否引入与代码库不一致的新模式?diff 是否干净可审?队友会否批准?若"简化版"更难懂更难审→回退。
语言示例(要点):去多余 async 包装、用 || 替代 verbose 条件赋值、用 .filter 替代手建数组、冗余布尔返回直接 return 表达式;Python 用字典推导、卫语句早返回;React 抽 variant/label 变量替代嵌套条件渲染。
4. 互操作(I/O)
- INPUT:待精简的代码、其测试、项目约定(CLAUDE.md 等)。
- OUTPUT(工件类型:SimplifyReport):
{changed_files:[string], simplifications:[{pattern, before, after, tests_still_pass}], behavior_preserved:bool, dead_code_removed:[string]} - 下游消费:
SimplifyReport交code-review-and-quality(评审精简结果)、test-driven-development(确保测试仍绿)、git-workflow-and-versioning(单独提交重构)。
5. 输出规范
输出 = 自然语言说明 + SimplifyReport 工件。必须:既有测试无需改仍过、构建无新告警、每条简化可审、diff 干净无无关改动、遵循项目约定、未削弱错误处理、无遗留死代码。完整识别信号表、语言示例、检视清单见 references/template.md。
6. 使用示例
输入:
这段
process(data)三层嵌套 if 很难读,帮我精简(保留行为)。
输出(节选):
- 改写前:
def process(data): if data is not None: if data.is_valid(): if data.has_permission(): return do_work(data) else: raise PermissionError(...) else: raise ValueError(...) else: raise TypeError(...) - 改写后(卫语句早返回):
def process(data): if data is None: raise TypeError("Data is None") if not data.is_valid(): raise ValueError("Invalid data") if not data.has_permission(): raise PermissionError("No permission") return do_work(data) - 验证:跑测试套件全绿,行为一致;
SimplifyReport:behavior_preserved:true,dead_code_removed:[]。
Current listing version
1.0.0
Safety and assessment details
Quality assessment
Out of 100 · Assessment report from SkillPi LAB.
Highlights
- 解决开发者降低代码复杂度、提升可读性的明确痛点
- 提供五原则与四步流程,具备强实操性
- 覆盖多种语言示例与重构信号,适用性广
Highlights
- description 清晰描述核心功能与边界
- 触发词精准,无明显歧义或重叠风险
Highlights
- 结构完整,含角色、流程、示例、输出规范
- 步骤清晰,有出口条件与端到端示例
- frontmatter 完整,文档长度合理
Areas to improve
-
未显式覆盖无测试覆盖代码的重构策略 Reported severity: Minor
文档强调测试验证,但未说明当目标代码缺乏测试时应如何处理
Recommendation: 补充无测试覆盖场景下的重构策略,如先补测试再重构,或限制改动范围
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.