Refactor code to improve structure, readability, and maintainability.
Refactors code to improve structure, readability, and maintainability using proven patterns.
/plugin marketplace add adelabdelgawad/fullstack-agents/plugin install adelabdelgawad-fullstack-agents-plugins-fullstack-agents@adelabdelgawad/fullstack-agentsRefactor code to improve structure, readability, maintainability, and adherence to best practices.
/optimize refactor [target]Before:
async def create_order(self, session: AsyncSession, data: OrderCreate):
# Validate stock
for item in data.items:
product = await self.product_repo.get(session, item.product_id)
if product.stock < item.quantity:
raise ValidationError(f"Insufficient stock for {product.name}")
# Calculate total
total = Decimal("0")
for item in data.items:
product = await self.product_repo.get(session, item.product_id)
total += product.price * item.quantity
# Create order
order = Order(user_id=data.user_id, total=total)
session.add(order)
await session.flush()
# Create order items
for item in data.items:
order_item = OrderItem(order_id=order.id, **item.dict())
session.add(order_item)
return order
After:
async def create_order(self, session: AsyncSession, data: OrderCreate):
await self._validate_stock(session, data.items)
total = await self._calculate_total(session, data.items)
order = await self._create_order_record(session, data.user_id, total)
await self._create_order_items(session, order.id, data.items)
return order
async def _validate_stock(self, session: AsyncSession, items: list[OrderItemCreate]):
for item in items:
product = await self.product_repo.get(session, item.product_id)
if product.stock < item.quantity:
raise ValidationError(f"Insufficient stock for {product.name}")
async def _calculate_total(self, session: AsyncSession, items: list[OrderItemCreate]) -> Decimal:
total = Decimal("0")
for item in items:
product = await self.product_repo.get(session, item.product_id)
total += product.price * item.quantity
return total
async def _create_order_record(self, session: AsyncSession, user_id: int, total: Decimal) -> Order:
order = Order(user_id=user_id, total=total)
session.add(order)
await session.flush()
return order
async def _create_order_items(self, session: AsyncSession, order_id: int, items: list[OrderItemCreate]):
for item in items:
order_item = OrderItem(order_id=order_id, **item.dict())
session.add(order_item)
Before:
def process_payment(payment_type: str, amount: Decimal):
if payment_type == "credit_card":
# Credit card processing
validate_card()
charge_card(amount)
elif payment_type == "paypal":
# PayPal processing
redirect_to_paypal()
confirm_payment(amount)
elif payment_type == "bank_transfer":
# Bank transfer processing
generate_reference()
await_transfer(amount)
After:
from abc import ABC, abstractmethod
class PaymentProcessor(ABC):
@abstractmethod
async def process(self, amount: Decimal) -> PaymentResult:
pass
class CreditCardProcessor(PaymentProcessor):
async def process(self, amount: Decimal) -> PaymentResult:
await self.validate_card()
return await self.charge_card(amount)
class PayPalProcessor(PaymentProcessor):
async def process(self, amount: Decimal) -> PaymentResult:
await self.redirect_to_paypal()
return await self.confirm_payment(amount)
class BankTransferProcessor(PaymentProcessor):
async def process(self, amount: Decimal) -> PaymentResult:
await self.generate_reference()
return await self.await_transfer(amount)
# Factory
def get_payment_processor(payment_type: str) -> PaymentProcessor:
processors = {
"credit_card": CreditCardProcessor,
"paypal": PayPalProcessor,
"bank_transfer": BankTransferProcessor,
}
return processors[payment_type]()
Before:
async def create_report(
start_date: date,
end_date: date,
user_id: int | None,
category_ids: list[int] | None,
status: str | None,
format: str,
include_summary: bool,
include_charts: bool,
):
...
After:
@dataclass
class ReportConfig:
start_date: date
end_date: date
user_id: int | None = None
category_ids: list[int] | None = None
status: str | None = None
format: str = "pdf"
include_summary: bool = True
include_charts: bool = False
async def create_report(config: ReportConfig):
...
Before:
if user.failed_attempts >= 5:
lock_account(user, 3600) # Lock for 1 hour
if order.total > 1000:
apply_discount(order, 0.1) # 10% discount
After:
MAX_LOGIN_ATTEMPTS = 5
ACCOUNT_LOCK_DURATION_SECONDS = 3600
BULK_ORDER_THRESHOLD = Decimal("1000")
BULK_ORDER_DISCOUNT = Decimal("0.1")
if user.failed_attempts >= MAX_LOGIN_ATTEMPTS:
lock_account(user, ACCOUNT_LOCK_DURATION_SECONDS)
if order.total > BULK_ORDER_THRESHOLD:
apply_discount(order, BULK_ORDER_DISCOUNT)
## Code Refactoring Report
**Target:** `api/services/order_service.py`
**Generated:** {timestamp}
### Summary
| Refactoring Type | Count |
|------------------|-------|
| Extract Method | 3 |
| Replace Conditional | 1 |
| Introduce Parameter Object | 1 |
| Replace Magic Numbers | 5 |
### Refactoring 1: Extract Method
**Function:** `create_order` (89 lines → 15 lines)
**Changes:**
- Extracted `_validate_stock()`
- Extracted `_calculate_total()`
- Extracted `_create_order_record()`
- Extracted `_create_order_items()`
**Benefits:**
- Each function does one thing
- Easier to test individual pieces
- More readable main function
- Reusable helper methods
### Refactoring 2: Replace Conditionals
**Location:** `api/services/payment_service.py:45-89`
**Changes:**
- Created `PaymentProcessor` abstract base class
- Created `CreditCardProcessor`, `PayPalProcessor`, `BankTransferProcessor`
- Added factory function `get_payment_processor()`
**Benefits:**
- Open/Closed Principle - add new payment types without modifying existing code
- Single Responsibility - each processor handles one payment type
- Easier testing - mock individual processors
### Refactoring 3: Introduce Constants
**File:** `core/constants.py` (new file)
```python
# Authentication
MAX_LOGIN_ATTEMPTS = 5
ACCOUNT_LOCK_DURATION_SECONDS = 3600
TOKEN_EXPIRE_MINUTES = 15
REFRESH_TOKEN_EXPIRE_DAYS = 7
# Business Rules
BULK_ORDER_THRESHOLD = Decimal("1000")
BULK_ORDER_DISCOUNT = Decimal("0.1")
FREE_SHIPPING_THRESHOLD = Decimal("50")
# Pagination
DEFAULT_PAGE_SIZE = 20
MAX_PAGE_SIZE = 100
Cyclomatic Complexity:
| Function | Before | After |
|---|---|---|
| create_order | 12 | 3 |
| process_payment | 8 | 2 |
| generate_report | 15 | 4 |
Lines of Code:
| File | Before | After |
|---|---|---|
| order_service.py | 234 | 189 |
| payment_service.py | 156 | 98 |
New testable units:
test_validate_stock()test_calculate_total()test_credit_card_processor()test_paypal_processor()| File | Changes |
|---|---|
api/services/order_service.py | Extract methods |
api/services/payment_service.py | Polymorphism |
core/constants.py | New file |
api/v1/orders.py | Use constants |
Designs feature architectures by analyzing existing codebase patterns and conventions, then providing comprehensive implementation blueprints with specific files to create/modify, component designs, data flows, and build sequences