การตั้งชื่อตัวแปรที่ดี
การตั้งชื่อตัวแปรให้ถูกต้องตามข้อกำหนดช่วยให้โค้ดอ่านง่ายขึ้น บำรุงรักษาได้สะดวก และลดโอกาสเกิดข้อผิดพลาด ลองฝึกตั้งชื่อคลาสและฟังก์ชันให้ถูกต้องตามรูปแบบที่กำหนด
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
กรณีศึกษา: การสร้างซอฟต์แวร์ด้วย Python
คำแนะนำการฝึกหัด
- เลือกชื่อที่เหมาะสมสำหรับคลาส ระหว่าง
mortgage_calculatorกับMortgageCalculator - เลือกชื่อที่เหมาะสมสำหรับฟังก์ชัน ระหว่าง
calculate_monthly_paymentกับCalculateMonthlyPayment
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Use proper naming conventions for a class name
class ____(FinancialCalculator):
def __init__(self, loan_amount, annual_interest_rate, years):
super().__init__()
self.loan_amount = loan_amount
self.monthly_interest_rate = self.monthly_interest(annual_interest_rate)
self.months = self.multiply(years, 12)
# Use proper naming conventions for a function name
def ____(self):
numerator = self.monthly_interest_rate * (1 + self.monthly_interest_rate) ** self.months
denominator = (1 + self.monthly_interest_rate) ** self.months - 1
multiplier = self.divide(numerator, denominator)
monthly_payment = round(self.multiply(self.loan_amount, multiplier), 2)
return monthly_payment