執行TDD的Refactor階段。在測試通過後,改善程式碼品質。
Refactors code to improve quality while maintaining all tests passing.
/plugin marketplace add cashwu/claude-code-tdd-marketplace/plugin install test-first-tdd@tdd-methodologies執行TDD的Refactor階段,改善程式碼品質。
【功能名】:{{feature_name}}
請確認:
在保持測試通過的前提下,改善程式碼品質。
重要原則:
// ❌ 重複的程式碼
function processA(data) {
if (!data) throw new Error('無效資料');
return data.value * 2;
}
function processB(data) {
if (!data) throw new Error('無效資料');
return data.value * 3;
}
// ✅ 提取共通邏輯
function validateData(data) {
if (!data) throw new Error('無效資料');
}
function processA(data) {
validateData(data);
return data.value * 2;
}
function processB(data) {
validateData(data);
return data.value * 3;
}
/**
* 【功能說明】:計算折扣後的價格
* 【改善說明】:提取折扣計算邏輯,提高可讀性
* 【使用情境】:結帳時計算最終價格
*
* @param {number} price - 原始價格
* @param {number} discount - 折扣百分比(0-100)
* @returns {number} - 折扣後價格
*/
function calculateDiscountedPrice(price, discount) {
// 【參數驗證】:確保價格和折扣都是有效數值
if (price < 0 || discount < 0 || discount > 100) {
throw new Error('無效的價格或折扣');
}
// 【折扣計算】:套用折扣百分比
const discountAmount = price * (discount / 100);
// 【最終價格】:原價減去折扣金額
return price - discountAmount;
}
npm test
根據 memo.md 中記錄的待改善項目:
每次只改善一個地方:
記錄重構內容
// Before
function processOrder(order) {
// 驗證訂單
if (!order.items || order.items.length === 0) {
throw new Error('訂單沒有商品');
}
// 計算總價
let total = 0;
for (const item of order.items) {
total += item.price * item.quantity;
}
return total;
}
// After
function processOrder(order) {
validateOrder(order);
return calculateTotal(order.items);
}
function validateOrder(order) {
// 【訂單驗證】:確保訂單包含商品
if (!order.items || order.items.length === 0) {
throw new Error('訂單沒有商品');
}
}
function calculateTotal(items) {
// 【總價計算】:計算所有商品的總價
let total = 0;
for (const item of items) {
total += item.price * item.quantity;
}
return total;
}
// Before
function calculateShipping(weight) {
if (weight > 1000) {
return 100;
}
return 50;
}
// After
// 【運費常數】:定義不同重量級別的運費
const SHIPPING_COST = {
STANDARD: 50,
HEAVY: 100,
};
// 【重量門檻】:超過此重量視為重貨
const HEAVY_WEIGHT_THRESHOLD = 1000;
function calculateShipping(weight) {
// 【運費計算】:根據重量決定運費
if (weight > HEAVY_WEIGHT_THRESHOLD) {
return SHIPPING_COST.HEAVY;
}
return SHIPPING_COST.STANDARD;
}
// Before
function canPurchase(user, product) {
if (user.age >= 18) {
if (user.balance >= product.price) {
if (product.stock > 0) {
return true;
}
}
}
return false;
}
// After
function canPurchase(user, product) {
// 【購買條件檢查】:驗證使用者和商品狀態
const isAdult = user.age >= 18;
const hasEnoughBalance = user.balance >= product.price;
const isInStock = product.stock > 0;
return isAdult && hasEnoughBalance && isInStock;
}
在 memo.md 中加入 Refactor 階段記錄:
## Refactor 階段(程式碼重構)
### 日期
{當前日期時間}
### 改善內容
1. {改善項目 1}
- 改善前:{描述}
- 改善後:{描述}
- 原因:{為什麼要這樣改善}
2. {改善項目 2}
...
### 測試結果
✅ 所有測試持續通過
### 程式碼品質評估
- 可讀性:{評估}
- 維護性:{評估}
- 效能:{評估}
### 下一步
進入完整性驗證階段。
完成後請確認:
禁止的行為:
允許的行為:
Refactor 階段完成後,執行:
/tf-verify
驗證開發完整性。