Safe refactoring with test guardrails

VerifiedSafe

Safely restructures code by running tests before and after each change to ensure no behavioral changes. Ideal for /refactor commands or any code cleanup where preserving existing behavior is critical.

Sby Skills Guide Bot
DevelopmentIntermediate
1706/2/2026
Claude Code
#refactoring#testing#code-quality#safe-refactoring

Recommended for

Our review

Safe refactoring with test guardrails: runs tests before and after changes to ensure behavior is preserved.

Strengths

  • Provides a safety net via test suite
  • Incremental process with validation after each step
  • Shows a diff for review
  • Does not alter existing behavior

Limitations

  • Requires an existing reliable test suite
  • Can be slow if tests are long
  • Does not address design issues that require behavior changes
When to use it

Use this skill when you need to restructure existing code without changing its behavior and have a quality test suite in place.

When not to use it

Do not use it for adding features, fixing bugs, or when tests are missing or failing.

Security analysis

Safe
Quality score92/100

The skill uses standard development tools (Bash, Read, Write, Edit, Glob, Grep) for safe refactoring with test guardrails. It does not instruct destructive actions, exfiltration, or obfuscated payloads.

No concerns found

Examples

Refactor a function
/refactor extract helper functions from the processOrder method to improve readability
Clean up code
refactor this file to reduce duplication and simplify conditionals
Rename for clarity
clean up this code by renaming variables and functions to be more descriptive

name: refactor description: Safe refactoring with test guardrails. Use when the user says "/refactor", "refactor this", "clean up this code", or wants to restructure code without changing behavior. Runs tests before and after to ensure safety. allowed-tools:

  • Bash
  • Read
  • Write
  • Edit
  • Glob
  • Grep

Refactor

安全重构:测试基线 → 重构 → 验证绿灯 → 展示 diff。

Workflow

1. Understand Scope

Clarify with the user:

  • What to refactor (file, function, module)
  • What kind of refactoring (extract, rename, simplify, reorganize)
  • What NOT to change

2. Baseline Tests

Run the test suite BEFORE any changes:

[test command]

All tests must pass. If they don't, fix tests first (or ask user) before refactoring.

Record the baseline: N tests, all passing.

3. Read the Code

Read the target code thoroughly. Understand:

  • Current structure and responsibilities
  • Dependencies (who calls this, what does it call)
  • Edge cases handled

4. Plan the Refactoring

Describe what you'll change and why. Common refactoring types:

| Type | When | |------|------| | Extract function | 函数太长或有重复逻辑 | | Rename | 命名不清晰 | | Simplify conditionals | 嵌套过深或逻辑复杂 | | Move/reorganize | 职责错位 | | Remove dead code | 未使用的代码 | | Reduce duplication | DRY 原则 |

5. Execute

Apply changes incrementally:

  • One refactoring step at a time
  • Run tests after each step
  • If tests break, revert and retry differently

6. Verify

Run the full test suite:

[test command]

All baseline tests must still pass. Show the output.

7. Show Diff

git diff

Present the diff so the user can review the changes.

Rules

  • 不改变行为——所有现有测试必须通过
  • 不添加功能——重构和 feature 分开做
  • 不一次改太多——小步快跑,每步验证
  • 如果没有测试覆盖,先建议添加测试再重构
Related skills