refactor-code-with-ai
Refactor code with AI assistance. Use to refactor code in TDD loop, and to improve code quality.
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
القائمة
Refactor code with AI assistance. Use to refactor code in TDD loop, and to improve code quality.
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
استنادا إلى تصنيف SOC المهني
Analyzes the extracted story document, capturing capability, acceptance criteria, edge cases, dependencies and open design questions. Use after extracting a story from a user request, to clarify the stories intended and likley imact. Use before designing the implementation.
Discovers the current application reality BEFORE designing or implementing. Surfaces relevant ADRs, existing code patterns, and target architecture.
Design the implementation of a new user story based on the current application reality, and analysis of the proposed user story.
Design comprehensive test cases for a given set of requirements. Use before starting TDD to plan coverage.
Drafts a high-level implementation plan based on the extracted story document
Extract a user story from a given document or text, into a json format
| name | refactor-code-with-ai |
| description | Refactor code with AI assistance. Use to refactor code in TDD loop, and to improve code quality. |
Improve code quality, remove duplication, and enhance design while keeping all tests green.
Review code for:
DUPLICATION:
- [ ] Repeated code blocks
- [ ] Similar methods that could be unified
- [ ] Copy-pasted logic
NAMING:
- [ ] Unclear variable names
- [ ] Method names that don't describe behavior
- [ ] Magic numbers without constants
STRUCTURE:
- [ ] Long methods (>20 lines)
- [ ] Deep nesting (>3 levels)
- [ ] Large classes (>200 lines)
- [ ] Missing abstractions
SOLID VIOLATIONS:
- [ ] Class doing too much (SRP)
- [ ] Rigid dependencies (DIP)
- [ ] Large interfaces (ISP)
Order by impact and safety:
HIGH PRIORITY (Safe, high impact):
1. Rename for clarity
2. Extract constants
3. Remove dead code
4. Simplify conditionals
MEDIUM PRIORITY (Moderate risk):
5. Extract methods
6. Extract classes
7. Introduce parameters
LOW PRIORITY (Higher risk, do carefully):
8. Change method signatures
9. Restructure inheritance
10. Modify public APIs
IMPORTANT: One change at a time!
1. Make a single, focused change
2. Run all tests immediately
3. Verify tests still pass
4. Commit if green
5. Repeat for next refactoring
After EVERY refactoring step:
$ dotnet test # C#
$ pytest # Python
$ npm test # TypeScript
If ANY test fails:
- STOP immediately
- Undo the change
- Understand why it broke
- Try a smaller step
Verify improvements:
- [ ] Code is more readable
- [ ] Duplication is reduced
- [ ] Names are clearer
- [ ] Structure is simpler
- [ ] All tests still pass
- [ ] No new functionality added
Choose one:
A) More refactoring needed → Repeat from Step 1
B) Code is clean enough → Continue to next test
C) All tests implemented → Feature complete
.process directory, named {skill-name}.done.json.// Before
public void ProcessOrder(Order order)
{
// Validate
if (order.Items.Count == 0) throw new Exception("Empty");
if (order.Total < 0) throw new Exception("Invalid total");
// Process
// ... more code
}
// After
public void ProcessOrder(Order order)
{
ValidateOrder(order);
// Process
}
private void ValidateOrder(Order order)
{
if (order.Items.Count == 0) throw new Exception("Empty");
if (order.Total < 0) throw new Exception("Invalid total");
}
# Before
def calculate_shipping(weight):
if weight > 50:
return weight * 2.5
return weight * 1.5
# After
MAX_STANDARD_WEIGHT = 50
HEAVY_RATE_PER_KG = 2.5
STANDARD_RATE_PER_KG = 1.5
def calculate_shipping(weight):
if weight > MAX_STANDARD_WEIGHT:
return weight * HEAVY_RATE_PER_KG
return weight * STANDARD_RATE_PER_KG
// Before
function getDiscount(customer: Customer): number {
if (customer.type === "premium") {
if (customer.years > 5) {
return 0.2;
} else {
return 0.1;
}
} else {
if (customer.years > 5) {
return 0.05;
} else {
return 0;
}
}
}
// After
function getDiscount(customer: Customer): number {
const isPremium = customer.type === "premium";
const isLoyal = customer.years > 5;
if (isPremium && isLoyal) return 0.2;
if (isPremium) return 0.1;
if (isLoyal) return 0.05;
return 0;
}
// Before: Order doing too much
public class Order
{
public List<Item> Items { get; set; }
public decimal CalculateSubtotal() { }
public decimal CalculateTax() { }
public decimal CalculateShipping() { }
public decimal CalculateTotal() { }
public void SendConfirmationEmail() { }
public void GenerateInvoicePdf() { }
}
// After: Separated concerns
public class Order
{
public List<Item> Items { get; set; }
}
public class OrderPricingService
{
public decimal CalculateSubtotal(Order order) { }
public decimal CalculateTax(Order order) { }
public decimal CalculateShipping(Order order) { }
public decimal CalculateTotal(Order order) { }
}
public class OrderNotificationService
{
public void SendConfirmationEmail(Order order) { }
public void GenerateInvoicePdf(Order order) { }
}
→ tdd-red: Start next test case from plan
→ OR complete if all tests implemented
❌ Refactoring and adding features simultaneously
❌ Making multiple changes before testing
❌ Skipping tests during refactoring
❌ Refactoring code that doesn't have tests
❌ Premature abstraction
❌ Over-engineering for future requirements
Stop when:
✓ Code clearly expresses intent
✓ No obvious duplication remains
✓ Methods are reasonably sized
✓ Names are descriptive
✓ Further changes have diminishing returns
Continue if:
✗ Code smells are obvious
✗ Duplication exists
✗ Understanding requires effort
✗ Changes in one place require changes elsewhere