# 读书笔记-重构: 章6 重新组织函数

2016-12-05 09:41:46来源:作者:wklken's blog人点击

def print_owing(double amount): print_banner() // print details print "this is the detail: " print "amnount: %s" % amount

to

def print_details(amount): print "this is the detail: " print "amnount: %s" % amountdef print_owing(double amount): print_banner() print_details(amount) 6.2 Inline Method 内联函数

def get_rating(x): return 2 if more_than_five_late_deliveries(x) else 1def more_than_five_late_deliveries(x): return x > 5

to

def get_rating(x): return 2 if x > 5 else 1 6.3 Inline Temp 内联临时变量

base_price = order.get_base_price()return base_price > 1000

to

return order.get_base_price() > 1000 6.4 Replace Temp with Query 以查询取代临时变量

base_price = quantity * item_priceif base_price > 1000: return base_price * 0.95else: return base_price * 0.98

to

if base_price() > 1000: return base_price() * 0.95else: return base_price() * 0.98def base_price(): return quantity * itme_price 6.5 Introduce Explaining Variable 引入解释性变量

if "MAC" in platform.upper() and "IE" in browser.upper() and was_initialized() and resize > 0: #do something

to

is_macos = "MAC" in platform.upper()is_ie_browser = "IE" in browser.upper()was_resized = resize > 0if is_macos and is_ie_browser and was_initialized() and was_resized: # do something 6.6 Split Temporary Variable 分解临时变量

tmp = 2 * (height * width)print tmptmp = height * widthprint tmp

to

perimeter = 2 * (height * width)print perimeterarea = height * widthprint area 6.7 Remove Assignments to Parameters 移除对参数的赋值

def discount(int input_value): if input_value > 50: input_value -= 2

to

def discount(int input_value): result = input_value if input_value > 50: result -= 2 6.8 Replace Method with Method Object 以函数对象取代函数

class Account(object): def delta(self): # do something return x def gamma(self, input_value, quantity, year_to_date): i_value_1 = (input_value * quantity) + self.delta() i_value_2 = (input_value * year_to_date) + 100 if year_to_date - i_value_1: i_value_2 -= 20 i_value_3 = i_value_2 * 7 # and so on. return i_value_3 - 2 * i_value_1

to

class Account(object): def delta(self): # do something return x def gamma(self, input_value, quantity, year_to_date): return Gamma(self, input_value, quantity, year_to_date).compute()class Gamma(object): def __init__(self, account, input_value, quantity, year_to_date): self.account = account self.input_value = input_value self.quantity = quantity self.year_to_date = year_to_date self.i_value_1 = 0 self.i_value_2 = 0 self.i_value_3 = 0 def compute(self): self.i_value_1 = (self.input_value * self.quantity) + self.account.delta() self.i_value_2 = (self.input_value * self.year_to_date) + 100 important_thing() self.i_value_3 = self.i_value_2 * 7 # and so on. return self.i_value_3 - 2 * self.i_value_1 def important_thing(self): if self.year_to_date - self.i_value_1: self.i_value_2 -= 20 6.9 Substitute Algorithm 替换算法

def find_person(i): if i == "Don": return "D" if i == "John": return "J" if i == "Kent": return "K" return ""

to

def find_person(i): return {"Don": "D", "John": "J", "Kent": "k", }.get(i, "")