When to Use This Skill
Use the Refactoring skill when you need to:- Rename a function or class across multiple files
- Extract code into a separate module
- Split a large service into smaller pieces
- Move code to a different file
- Restructure packages or modules
- Safely modify public APIs
Example Scenarios
Rename this function safely
Rename this function safely
Use
gitnexus_rename with dry_run: true to preview all edits, then apply if satisfied.Extract this into a module
Extract this into a module
Use
gitnexus_context to see all dependencies, plan the new interface, then extract and update imports.Split this service
Split this service
Use
gitnexus_context to understand callees, group by responsibility, then use impact to find all callers to update.Move this to a new file
Move this to a new file
Use
impact to find all dependents, move the code, then update import paths using rename or manually.Workflow
Follow these steps for safe refactoring:General Refactoring Principles
Update Order:- Interfaces and types first
- Implementations next
- Callers after that
- Tests last
Workflow: Rename Symbol
Renaming is the most common refactoring task. GitNexus provides automated multi-file rename with confidence scoring.Step-by-Step Guide
1. Preview the Rename (Dry Run)- graph edits: High confidence, based on knowledge graph relationships
- ast_search edits: Lower confidence, found via text search (might include string refs, comments)
detect_changes output shows affected processes—run tests for those:
Rename Checklist
-
gitnexus_rename({..., dry_run: true})to preview all edits - Review graph edits (high confidence)
- Carefully review ast_search edits (might include string refs)
- If satisfied:
gitnexus_rename({..., dry_run: false})to apply -
gitnexus_detect_changes()to verify scope - Run tests for affected processes
Workflow: Extract Module
Extracting code into a separate module requires understanding dependencies.Step-by-Step Guide
1. Get Full Context- Create new file
- Move implementation
- Export public interface
Extract Module Checklist
-
gitnexus_context({name: target})to see all dependencies -
gitnexus_impact({target, direction: "upstream"})to find external callers - Define new module interface
- Extract code and update imports
-
gitnexus_detect_changes()to verify scope - Run tests for affected processes
Workflow: Split Function/Service
Splitting a large function or service requires grouping callees by responsibility.Step-by-Step Guide
1. Understand All Callees- Validation: validateCart
- Payment: processPayment
- Notification: sendEmail
- Inventory: updateInventory
detect_changes and run tests.
Split Function Checklist
-
gitnexus_context({name: target})to understand all callees - Group callees by responsibility
-
gitnexus_impact({target, direction: "upstream"})to map callers - Create new functions/services
- Update callers
-
gitnexus_detect_changes()to verify scope - Run tests for affected processes
Tools for Refactoring
gitnexus_rename
Automated multi-file rename with confidence scoring:- graph: Found via knowledge graph relationships (high confidence)
- ast_search: Found via text search (lower confidence, might include string refs)
gitnexus_impact
Map all dependents before refactoring:gitnexus_detect_changes
Verify refactoring scope:gitnexus_cypher
Custom queries for complex refactoring:Risk Rules
Use these rules to assess refactoring risk:| Risk Factor | Mitigation Strategy |
|---|---|
| Many callers (>5) | Use gitnexus_rename for automated updates |
| Cross-area refs | Use detect_changes after to verify scope |
| String/dynamic refs | Use gitnexus_query to find them, review ast_search edits |
| External/public API | Version and deprecate properly, coordinate with consumers |
Example: Rename validateUser to authenticateUser
Here’s a complete refactoring walkthrough:Step 1: Preview the Rename
Step 2: Review ast_search Edits
Checkconfig/auth.json:
Step 3: Apply the Rename
Step 4: Verify Changes
Step 5: Run Tests
Best Practices
Always Dry Run First
Preview changes with
dry_run: true before applying any rename.Review ast_search Edits
These might include string refs in config files—verify each one.
Use detect_changes After
Verify that only expected files changed and run affected tests.
Update in Order
Interfaces → implementations → callers → tests.
Advanced Techniques
Renaming Across Multiple Symbols
If you need to rename a pattern (e.g., allvalidate* to authenticate*):
-
Find all matching symbols:
-
Rename each one with
gitnexus_rename -
Use
detect_changesto verify total scope
Handling External/Public APIs
For public APIs used by external consumers:- Don’t rename immediately—add new function alongside old
- Deprecate old function with warnings
- Update internal callers to use new function
- Remove old function in next major version
Safe Refactoring Checklist
For any refactoring task:- Understand dependencies with
contextandimpact - Plan update order (interfaces first, tests last)
- Preview changes with dry run (for renames)
- Make incremental changes
- Verify scope with
detect_changes - Run tests for affected processes
- Commit frequently with clear messages
Common Pitfalls
Not Previewing Renames
Always usedry_run: true first—you might be surprised by string references in config files.
Ignoring ast_search Edits
These are lower confidence but often valid (config files, JSON, etc.). Review each one.Forgetting Tests
Run tests for affected processes, not just the changed files. Usedetect_changes to find them.
Updating Out of Order
Update interfaces before implementations, otherwise you’ll have type errors mid-refactor.Next Steps
Explore Other Skills
Return to skills overview to see all available workflows
MCP Tools Reference
Learn about all GitNexus MCP tools in detail