參數列應避免重複和保持簡短,若有資訊能自己取得,且不新增依賴關係,就不用當成引數傳入。

Replace Parameter with Query

讓函式具備引用透明性很重要(同參數會得到同行為),
這類函式易理解與測試,所以別用會變的全域變數來取代引入參數。

# Before
class Employee:
    def __init__(self, name, department):
        self.name = name
        self.department = department

    def calculate_bonus(self, performance_rating):
        if self.department == "Sales":
            if performance_rating > 3:
                return 1000
            return 500
        else:
            if performance_rating > 3:
                return 500
            return 200

employee = Employee("John Doe", "Sales")
bonus = employee.calculate_bonus(4)

# After
class Employee:
    def __init__(self, name, department, performance_rating):
        self.name = name
        self.department = department
        self.performance_rating = performance_rating

    def calculate_bonus(self):
        if self.department == "Sales":
            if self.performance_rating > 3:
                return 1000
            return 500
        else:
            if self.performance_rating > 3:
                return 500
            return 200

employee = Employee("John Doe", "Sales", 4)
bonus = employee.calculate_bonus()