safe-refactor

Perform safe refactoring when:

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "safe-refactor" with this command: npx skills add phar-dev/phardev.dot/phar-dev-phardev-dot-safe-refactor

When to Use

Perform safe refactoring when:

  • Renaming functions, variables, or classes

  • Restructuring code for better organization

  • Optimizing performance-critical sections

  • Improving code readability and maintainability

  • Extracting methods or classes

  • Consolidating duplicate code

Critical Patterns

Safety First Approach

ALWAYS follow this sequence:

  • Analyze - Understand current code and dependencies

  • Backup - Create git branch or stash current changes

  • Incremental Changes - Make small, verifiable changes

  • Rollback Ready - Know how to revert if issues occur

Code Analysis Requirements

Before refactoring:

  • Use Grep to find all usages of the target code

  • Use Read to understand current implementation

  • Identify dependencies and side effects

  • Check for complex logic that needs careful handling

Change Management

  • Atomic Changes: Each refactoring should be a single, focused change

  • Test After Each Step: Never batch changes without validation

  • Documentation Updates: Update comments and docs as you refactor

  • Interface Preservation: Maintain public APIs unless explicitly changing them

Refactoring Types

Function/Method Refactoring

// Before function oldFunction(param1, param2) { // complex logic }

// After (safe rename) function newFunctionName(param1, param2) { // same complex logic }

Variable Renaming

// Before const usr = getUser();

// After const user = getUser();

Code Extraction

// Extract method safely function processData(data) { // Before: inline logic const result = data.map((item) => item.value * 2); return result.filter((val) => val > 10); }

// After: extracted function function calculateValues(data) { return data.map((item) => item.value * 2); }

function filterHighValues(values) { return values.filter((val) => val > 10); }

function processData(data) { const result = calculateValues(data); return filterHighValues(result); }

Validation Commands

Pre-Refactor Checks

Run tests before starting

pnpm test

Check linting

pnpm run lint

Type checking

pnpm run typecheck

Post-Refactor Validation

Run all validation steps

pnpm test && pnpm run lint && pnpm run typecheck

Check for breaking changes

git diff --name-only

Rollback Procedures

Git-Based Rollback

If on feature branch

git reset --hard HEAD~1 # Undo last commit git checkout main # Switch back git branch -D feature-branch

If working directory changes

git stash # Save changes

... test rollback ...

git stash pop # Restore if safe

Manual Rollback

  • Keep original files backed up

  • Have git history to reference

  • Know which files were changed

  • Test rollback thoroughly

Error Handling

Common Issues

Tests Failing After Refactor:

  • Check if function signatures changed

  • Verify all call sites updated

  • Look for missing dependencies

Linting Errors:

  • Fix import statements

  • Update variable references

  • Check for unused code

Type Errors:

  • Update type definitions

  • Fix interface changes

  • Verify generic type usage

Recovery Steps

  • Stop and Assess: Don't make more changes

  • Revert Changes: Use git to undo problematic changes

  • Test Revert: Ensure rollback works

  • Retry Smaller: Break down into smaller changes

  • Get Help: Ask for code review if stuck

Best Practices

  • Start Small: Refactor one function at a time

  • Use IDE Tools: Leverage rename refactoring features

  • Commit Often: Each successful change gets committed

  • Document Changes: Update comments and READMEs

  • Peer Review: Have someone verify complex refactors

  • Measure Impact: Ensure performance doesn't degrade

Integration with Tools

With Git-Master Skill

Use git-master for version control operations:

  • Creating feature branches

  • Committing atomic changes

  • Managing merge conflicts

With Testing Frameworks

Coordinate with testing tools:

  • Run unit tests for affected code

  • Update integration tests

  • Add regression tests for complex changes

This skill ensures refactoring is performed safely with minimal risk of introducing bugs or breaking functionality.

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

Coding

skill-registry

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

git-master

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

skill-creator

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

command-creator

No summary provided by upstream source.

Repository SourceNeeds Review