AgentSkillsCN

refactor-code

用于优化代码结构、提升可读性与性能的工作流。适用于在已有测试的保障下,安全地重构代码并验证行为一致性时使用。

SKILL.md
--- frontmatter
name: refactor-code
description: Workflow for improving code structure, readability, and performance. Use when you need to refactor code safely with behavior verification via existing tests.

Refactor Code Workflow

This skill ensures you refactor code safely by verifying behavior before and after changes.

Workflow Steps

1. Pre-Check (Safety Net)

  • Verify Current State: Run existing tests for the target module. They must pass before you touch anything.
  • Backup: If no tests exist, strongly consider creating a "Snapshot Test" or backup file first.

2. Strategy

  • Identify Goal: What are you improving? (Readability, DRY, Performance, Security).
  • Plan: Decide which classes/methods will be extracted, renamed, or simplified.

3. Execution (Refactoring)

  • Apply Changes: Perform the refactoring steps (Extract Method, Rename Variable, Introduce Value Object, etc.).
  • Keep Logic: Do NOT change the business logic or external behavior.

4. Verification

  • Run Tests: Run the tests again. They MUST still pass.
  • Lint: Ensure the new code follows project style guidelines.