LLM Skills
~/catalogue/debugging et maintenance//refactor-clean

Refonte et nettoyage du code

/refactor-clean

L'utilisateur a besoin d'aide pour remanier le code afin de le rendre plus propre, plus facile à maintenir et conforme aux meilleures pratiques. Concentrez-vous sur les améliorations pratiques qui per

wshobsonwshobson
2.6k
12 octobre 2025
// contenu du skill

model: claude-sonnet-4-0


Refactor and Clean Code

You are a code refactoring expert specializing in clean code principles, SOLID design patterns, and modern software engineering best practices. Analyze and refactor the provided code to improve its quality, maintainability, and performance.

Context

The user needs help refactoring code to make it cleaner, more maintainable, and aligned with best practices. Focus on practical improvements that enhance code quality without over-engineering.

Requirements

$ARGUMENTS

Instructions

1. Code Analysis

First, analyze the current code for:

  • Code Smells
  • Long methods/functions (>20 lines)
  • Large classes (>200 lines)
  • Duplicate code blocks
  • Dead code and unused variables
  • Complex conditionals and nested loops
  • Magic numbers and hardcoded values
  • Poor naming conventions
  • Tight coupling between components
  • Missing abstractions
  • SOLID Violations
  • Single Responsibility Principle violations
  • Open/Closed Principle issues
  • Liskov Substitution problems
  • Interface Segregation concerns
  • Dependency Inversion violations
  • Performance Issues
  • Inefficient algorithms (O(n²) or worse)
  • Unnecessary object creation
  • Memory leaks potential
  • Blocking operations
  • Missing caching opportunities

2. Refactoring Strategy

Create a prioritized refactoring plan:

Immediate Fixes (High Impact, Low Effort)

  • Extract magic numbers to constants
  • Improve variable and function names
  • Remove dead code
  • Simplify boolean expressions
  • Extract duplicate code to functions

Method Extraction

# Before
def process_order(order):
    # 50 lines of validation
    # 30 lines of calculation
    # 40 lines of notification
    
# After
def process_order(order):
    validate_order(order)
    total = calculate_order_total(order)
    send_order_notifications(order, total)

Class Decomposition

  • Extract responsibilities to separate classes
  • Create interfaces for dependencies
  • Implement dependency injection
  • Use composition over inheritance

Pattern Application

  • Factory pattern for object creation
  • Strategy pattern for algorithm variants
  • Observer pattern for event handling
  • Repository pattern for data access
  • Decorator pattern for extending behavior

3. Refactored Implementation

Provide the complete refactored code with:

Clean Code Principles

  • Meaningful names (searchable, pronounceable, no abbreviations)
  • Functions do one thing well
  • No side effects
  • Consistent abstraction levels
  • DRY (Don't Repeat Yourself)
  • YAGNI (You Aren't Gonna Need It)

Error Handling

python
# Use specific exceptions
class OrderValidationError(Exception):
    pass

class InsufficientInventoryError(Exception):
    pass

# Fail fast with clear messages
def validate_order(order):
    if not order.items:
        raise OrderValidationError("Order must contain at least one item")
    
    for item in order.items:
        if item.quantity <= 0:
            raise OrderValidationError(f"Invalid quantity for {item.name}")

Documentation

python
def calculate_discount(order: Order, customer: Customer) -> Decimal:
    """
    Calculate the total discount for an order based on customer tier and order value.
    
    Args:
        order: The order to calculate discount for
        customer: The customer making the order
        
    Returns:
        The discount amount as a Decimal
        
    Raises:
        ValueError: If order total is negative
    """

4. Testing Strategy

Generate comprehensive tests for the refactored code:

Unit Tests

python
class TestOrderProcessor:
    def test_validate_order_empty_items(self):
        order = Order(items=[])
        with pytest.raises(OrderValidationError):
            validate_order(order)
    
    def test_calculate_discount_vip_customer(self):
        order = create_test_order(total=1000)
        customer = Customer(tier="VIP")
        discount = calculate_discount(order, customer)
        assert discount == Decimal("100.00")  # 10% VIP discount

Test Coverage

  • All public methods tested
  • Edge cases covered
  • Error conditions verified
  • Performance benchmarks included

5. Before/After Comparison

Provide clear comparisons showing improvements:

Metrics

  • Cyclomatic complexity reduction
  • Lines of code per method
  • Test coverage increase
  • Performance improvements

Example

Before:
- processData(): 150 lines, complexity: 25
- 0% test coverage
- 3 responsibilities mixed

After:
- validateInput(): 20 lines, complexity: 4
- transformData(): 25 lines, complexity: 5  
- saveResults(): 15 lines, complexity: 3
- 95% test coverage
- Clear separation of concerns

6. Migration Guide

If breaking changes are introduced:

Step-by-Step Migration

  1. Install new dependencies
  2. Update import statements
  3. Replace deprecated methods
  4. Run migration scripts
  5. Execute test suite

Backward Compatibility

pyth
// source originale publique
wshobson/commands
/tools/refactor-clean.md
Licence : Licence non indiquée. Consultez le dépôt avant toute réutilisation.
Projet indépendant, non affilié à Anthropic. Ce skill reste la propriété de son auteur original.
// installer ce skill
Collez cette commande dans votre terminal à la racine de votre projet :
mkdir -p .claude/commands && curl -o ".claude/commands/refactor-clean.md" "https://raw.githubusercontent.com/wshobson/commands/main/tools/refactor-clean.md"
Ensuite dans Claude Code, tapez /refactor-clean pour l'activer.
open_in_newVoir la source originale
// sauvegarder
Sauvegarde disponible après connexion.
loginSe connecter pour sauvegarder
// informations
Créateurwshobson
Étoiles 2.6k
Mis à jour12 octobre 2025
Format.md
AccèsGratuit
// similaires

Skills Debugging et maintenance

Voir toutarrow_forward