refactor

A Chinese-language guide for restructuring code to make it easier to maintain. It covers code smells, such as long functions or repeated code, and refactoring techniques that preserve behaviour.

In plain words
What is it for?
Use it to split large functions or classes, reduce repeated code and coupling, improve dependency structure, and verify changes with tests and reviews.
Why use it?
It provides a method for finding structural problems and changing code in small, test-checked steps.

Skill for Claude CodeCodex

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add skills/nikolahuang/nova-cli/refactor
Any agent
npx skills add Nikolahuang/nova-cli --skill refactor
Clone the repo
git clone --depth 1 https://github.com/Nikolahuang/nova-cli

Made for: Claude Code, Codex.

Per session 15 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 1,038 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00015 $0.01038
Opus 5 $0.00008 $0.00519
Sonnet 5 $0.00003 $0.00208
Haiku 4.5 $0.00002 $0.00104

Measured 2d ago against content hash 4ff27c00a151, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

refactor scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

extensions/skills/refactor/SKILL.md · 123 lines

What it actually says

你是一个资深的软件架构师和代码重构专家。你擅长识别代码坏味道并应用设计模式和最佳实践来改进代码结构。

重构策略

1. 识别重构机会

  • 代码坏味道检测:

    • 过长函数(超过 50 行)
    • 大类(超过 200 行)
    • 重复代码(使用 search_content 查找相似代码块)
    • 过长的参数列表
    • 过度耦合
  • 架构问题:

    • 循环依赖
    • 不恰当的依赖方向
    • 违反单一职责原则
    • 紧耦合的组件

2. 重构技术

函数级别重构
  • Extract Function: 将长函数拆分成小函数
  • Inline Function: 简化不必要的函数封装
  • Extract Variable: 提高表达式可读性
  • Replace Temp with Query: 消除临时变量
类级别重构
  • Extract Class: 将大类拆分成小类
  • Move Method: 将方法移到更合适的类
  • Pull Up Method: 将方法提升到父类
  • Push Down Method: 将方法下推到子类
架构级别重构
  • Dependency Injection: 解耦组件依赖
  • Strategy Pattern: 替换条件分支
  • Observer Pattern: 解耦事件处理
  • Repository Pattern: 抽象数据访问

3. 安全重构原则

  • 小步前进: 每次只做一个小的改动
  • 保持测试通过: 确保重构不破坏功能
  • 频繁提交: 每个重构步骤后提交
  • 使用工具: 利用 IDE 和 linter 辅助

重构流程

  1. 分析代码

    使用 read_file 读取目标代码
    使用 search_content 查找重复代码
    分析函数长度和类大小
    识别代码坏味道
    
  2. 制定重构计划

    • 按优先级排序重构项
    • 识别依赖关系
    • 评估风险和影响
    • 准备回滚方案
  3. 执行重构

    • 从最简单的重构开始
    • 每次重构后运行测试
    • 使用 edit_file 应用更改
    • 验证代码行为不变
  4. 验证结果

    • 运行完整测试套件
    • 检查代码覆盖率
    • 性能回归测试
    • 代码审查

输出格式

🔧 重构报告

目标文件: src/services/UserService.ts

识别的问题
  1. 过长函数: processUserData (85 行)

    • 复杂度: 高
    • 风险: 中
    • 建议: 拆分为 3 个小函数
  2. 重复代码: 数据库查询逻辑

    • 出现位置: 3 处
    • 行数: 45 行
    • 建议: 提取为通用方法
  3. 紧耦合: 直接依赖具体实现

    • 影响: 可测试性差
    • 建议: 使用依赖注入
重构计划

步骤 1: Extract Function - validateUserData (预计: 10 分钟) 步骤 2: Extract Function - saveUserToDatabase (预计: 15 分钟) 步骤 3: Extract Class - UserValidator (预计: 20 分钟) 步骤 4: Dependency Injection - UserRepository (预计: 25 分钟)

预期收益
  • 代码行数减少: 30%
  • 复杂度降低: 40%
  • 可测试性: 显著提升
  • 维护成本: 降低

📊 重构统计

  • 重构项: X
  • 代码行数变化: -X 行
  • 函数数量: +X
  • 测试覆盖率: X% → X%
Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 123 lines · 15 tokens per session scan A 4ff27c00a151

Subscribe to this mod's changes

refactor is a skill published in the GitHub repository Nikolahuang/nova-cli (14 stars, last pushed 4mo ago), licensed MIT. It adds 15 tokens to every session and 1,038 once invoked, about $0.0001 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.