360 Secure SkillHub
360 安全技能中心

refactoring

面向现有代码的行为保持型重构指南:先固定契约并运行基线检查,再补充特征测试、诊断结构摩擦,按命名小步变换,最后审查差异并报告验证结果、遗留工作和验证缺口。

分类:开发
mingchuno
热度 0.00
最近更新 2026-10-27 11:52:25

安装

发给 Agent
帮我安装这个技能:npx secure-skills-cli add mingchuno/skills/refactoring
兼容性
通用兼容
联网要求
不联网
数据出网
数据不出本机
Metadata
name
refactoring
description
Restructure existing code in small steps that preserve observable behavior. Use when asked to refactor or clean up code, or when existing structure makes the current change awkward and a preparatory refactor would make it easy.

Refactoring

Refactoring is behavior-preserving design work in small, reversible steps. Every step leaves the code buildable, tested and reviewable; the next step is chosen only after the last one is green.

Scope

  • Requested refactor: the module, file, class, functions or codebase the user selected. If no target can be inferred, ask for it before editing.
  • Preparatory refactor, reached on your own during other work: only the structure that blocks the current change. Record further cleanup as a follow-up in your report.

Inspect callers and dependencies as needed to preserve contracts, without widening the edit scope. Feature or bug-fix behavior belongs to its own step, made only when the task asked for it.

Workflow

  1. Pin the contracts. Read the target, repository instructions, callers, tests and verification commands. List the observable behavior the target touches, using the contract checklist. Run the baseline checks. Done when every contract is listed and pre-existing failures are recorded.
  2. Build the safety net. Where a listed contract has no test that would fail if it broke, add a characterization test and watch it go red under a deliberate perturbation. See Safety net. Done when each contract is covered or recorded as a verification gap.
  3. Diagnose. Name the structural friction and the smallest move that removes it: Triage and Smells and moves. For a whole codebase, inventory the major areas and rank them by impact and risk; track completed, deferred and uninspected areas so one improved file is never reported as a refactored codebase.
  4. Transform. Apply one named move, run the affected checks, repeat. Pick moves from the catalog; for fragile or untested code use Legacy code. Commit structural changes apart from behavior changes and test updates.
  5. Stop and report. Stop at the stopping point. Review the diff against the final checklist, then report what improved, the checks run with their results, and remaining work or gaps.

When the diff starts to outgrow the behavior being changed, or the next move is a rewrite, return to step 3 and pick the next small move that recovers control. See Reassess.

Related skills

  • The red-green loop's own refactor step belongs to tdd; larger restructuring belongs here.
  • A move that crosses a module or service boundary, or changes a public API, schema or data layout, is architecture work: use production-software-architecture.
  • To measure whether complexity actually dropped, the user can invoke code-quality.