Description
Proactively detect and perform code refactoring to maintain DDD architecture and code quality.
Trigger Conditions
- "Refactor this code", "refactor"
- "This file is too long"
- "Modularize", "split"
- Proactive trigger: when code exceeding the threshold is detected
Core Principles
📜 Based on Article 7.3 of the Constitution, the "Proactive Refactoring Principle"
重構不是改天換地,而是持續的小步快跑
每次提交都應該比上次更乾淨
Threshold Settings
📏 Length Thresholds
| Type | Warning | Mandatory Refactoring |
|---|---|---|
| File | > 200 lines | > 400 lines |
| Class | > 150 lines | > 300 lines |
| Function | > 30 lines | > 50 lines |
| Number of files in directory | > 10 | > 15 |
🔄 Complexity Thresholds
| Metric | Warning | Mandatory Refactoring |
|---|---|---|
| Cyclomatic complexity | > 10 | > 15 |
| Nesting depth | > 3 levels | > 4 levels |
| Number of parameters | > 4 | > 6 |
| Number of dependencies | > 5 | > 8 |
Refactoring Pattern Library
1️⃣ Extract Method
Trigger condition: function too long, duplicated logic
# Before
def process_order(order):
# 驗證訂單 (10 行)
if not order.items:
raise ValueError("Empty order")
if order.total < 0:
raise ValueError("Invalid total")
# ... 更多驗證
# 計算價格 (15 行)
subtotal = sum(item.price * item.qty for item in order.items)
tax = subtotal * 0.05
total = subtotal + tax
# ... 更多計算
# 儲存訂單 (10 行)
# ...
# After
def process_order(order):
self._validate_order(order)
total = self._calculate_total(order)
self._save_order(order, total)
def _validate_order(self, order):
"""驗證訂單有效性"""
if not order.items:
raise ValueError("Empty order")
# ...
def _calculate_total(self, order) -> Decimal:
"""計算訂單總金額(含稅)"""
subtotal = sum(item.price * item.qty for item in order.items)
return subtotal * Decimal("1.05")
2️⃣ Extract Class
Trigger condition: class has too many responsibilities, exceeds 150 lines
# Before: User 類別包含太多職責
class User:
def __init__(self, name, email, ...):
self.name = name
self.email = email
self.address_line1 = ...
self.address_line2 = ...
self.city = ...
self.postal_code = ...
def validate_email(self): ...
def format_address(self): ...
def calculate_shipping(self): ...
# After: 提取 Address 值物件
@dataclass(frozen=True)
class Address:
"""地址值物件"""
line1: str
line2: str | None
city: str
postal_code: str
def format(self) -> str:
return f"{self.line1}\n{self.city} {self.postal_code}"
class User:
def __init__(self, name: str, email: Email, address: Address):
self.name = name
self.email = email
self.address = address
3️⃣ Replace Conditional with Polymorphism
Trigger condition: large amounts of if-elif-else or switch
# Before: 條件地獄
def calculate_shipping(order):
if order.shipping_type == "standard":
return order.weight * 10
elif order.shipping_type == "express":
return order.weight * 25 + 50
elif order.shipping_type == "overnight":
return order.weight * 50 + 100
elif order.shipping_type == "international":
# 複雜計算...
pass
# After: 策略模式
class ShippingStrategy(ABC):
@abstractmethod
def calculate(self, order) -> Decimal: ...
class StandardShipping(ShippingStrategy):
def calculate(self, order) -> Decimal:
return order.weight * 10
class ExpressShipping(ShippingStrategy):
def calculate(self, order) -> Decimal:
return order.weight * 25 + 50
# 使用
shipping_strategies = {
"standard": StandardShipping(),
"express": ExpressShipping(),
# ...
}
cost = shipping_strategies[order.shipping_type].calculate(order)
4️⃣ Introduce Parameter Object
Trigger condition: more than 4 parameters
# Before: 參數過多
def create_user(
name: str,
email: str,
phone: str,
address_line1: str,
address_line2: str,
city: str,
postal_code: str,
country: str,
):
...
# After: 使用參數物件
@dataclass
class CreateUserCommand:
name: str
email: str
phone: str
address: Address
def create_user(command: CreateUserCommand):
...
5️⃣ Split Module
Trigger condition: directory has more than 10 files
# Before
src/Domain/
├── User.py
├── Order.py
├── Product.py
├── Payment.py
├── Shipping.py
├── Review.py
├── Coupon.py
├── Notification.py
├── ... # 太多了!
# After: 按子領域拆分
src/Domain/
├── Identity/
│ └── User.py
├── Ordering/
│ ├── Order.py
│ └── Payment.py
├── Catalog/
│ ├── Product.py
│ └── Review.py
├── Promotion/
│ └── Coupon.py
└── Communication/
└── Notification.py
DDD Architecture Guardian
During refactoring, you must check whether DDD principles are violated:
❌ Common Violations
# 違規 1: Domain 層依賴 Infrastructure
# Domain/Services/OrderService.py
from infrastructure.database import db # ❌ 禁止!
# 違規 2: Presentation 直接存取 Domain
# Presentation/API/routes.py
from domain.repositories import UserRepository # ❌ 應透過 Application
# 違規 3: Entity 包含持久化邏輯
class User:
def save(self): # ❌ 應在 Repository
db.session.add(self)
✅ Correct Dependency Direction
Presentation → Application → Domain
↓
Infrastructure
Refactoring Process
1️⃣ Detection Phase
🔍 偵測到重構需求:
- 檔案:`src/domain/services/order_service.py`
- 問題:檔案長度 342 行(超過 200 行警告閾值)
- 複雜度:圈複雜度 12(超過 10 警告閾值)
2️⃣ Analysis Phase
📊 分析結果:
- `process_order()` 函數 85 行,建議拆分
- 發現 3 處重複邏輯,建議提取
- 識別出 2 個隱藏的 Value Object
3️⃣ Planning Phase
📋 重構計畫:
1. 提取 `OrderValidator` 類別
2. 提取 `PricingCalculator` 服務
3. 建立 `OrderStatus` Value Object
4. 更新測試確保覆蓋
4️⃣ Execution Phase
🔧 執行重構:
- [x] 建立 `OrderValidator` 類別
- [x] 遷移驗證邏輯
- [x] 更新測試
- [x] 確認測試通過
- [ ] 提取 `PricingCalculator`
- [ ] ...
5️⃣ Verification Phase
✅ 重構完成:
- 測試:全部通過(42/42)
- 覆蓋率:85%(+3%)
- 複雜度:8(-4)
- 架構:符合 DDD ✓
Proactive Suggestion Template
When refactoring is detected as needed, the AI should proactively suggest:
💡 **重構建議**
偵測到 `order_service.py` 已達 250 行,建議進行模組化:
### 建議拆分方案
| 新檔案 | 內容 | 行數 |
|--------|------|------|
| `order_validator.py` | 訂單驗證邏輯 | ~50 行 |
| `pricing_calculator.py` | 價格計算邏輯 | ~60 行 |
| `order_service.py` | 服務編排 | ~80 行 |
### 預期效益
- ✅ 單一職責原則
- ✅ 更易測試
- ✅ 降低認知負荷
是否要我執行這個重構?
Integration with Other Skills
| Skill | Integration Method |
|---|---|
code-reviewer |
Trigger refactoring suggestions during review |
test-generator |
Generate tests before refactoring |
ddd-architect |
Ensure refactoring conforms to DDD |
memory-updater |
Record refactoring decisions |