7
ตัวแปรชั่วคราวเทียบกับข้อกำหนดความยาวบรรทัด
ฉันได้อ่านRefactoringของ Martin Fowler แล้ว โดยทั่วไปถือว่ายอดเยี่ยม แต่คำแนะนำอย่างหนึ่งของ Fowler ดูเหมือนจะก่อให้เกิดปัญหาเล็กน้อย ฟาวเลอร์แนะนำให้คุณแทนที่ตัวแปรชั่วคราวด้วยเคียวรีดังนั้นแทนที่จะเป็น: double getPrice() { final int basePrice = _quantity * _itemPrice; final double discountFactor; if (basePrice > 1000) discountFactor = 0.95; else discountFactor = 0.98; return basePrice * discountFactor; } คุณดึงออกมาเป็นวิธีการช่วยเหลือ: double basePrice() { return _quantity * _itemPrice; } double getPrice() { …