惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The GitHub Blog
The GitHub Blog
A
About on SuperTechFans
The Cloudflare Blog
G
Google Developers Blog
博客园_首页
Martin Fowler
Martin Fowler
Apple Machine Learning Research
Apple Machine Learning Research
L
LangChain Blog
D
Docker
C
Check Point Blog
T
Tailwind CSS Blog
博客园 - 司徒正美
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hugging Face - Blog
Hugging Face - Blog
Microsoft Security Blog
Microsoft Security Blog
V
V2EX
博客园 - 叶小钗
T
The Blog of Author Tim Ferriss
酷 壳 – CoolShell
酷 壳 – CoolShell
IT之家
IT之家
M
MIT News - Artificial intelligence
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 【当耐特】
GbyAI
GbyAI

Posts on WKLKEN THINKING

apisix 中的 lrucache apisix 中的服务发现机制 apisix 中的负载均衡 apisix etcd机制 聊聊框架 关于 k8s 的 zero downtime deployment 一些建议 apisix 遇到的一些问题 关于在除夕前一天换了一个洗衣机的故事 Django DRF 性能优化 DRF 的一些实践 Part1: Serializer DRF继承关系图 Better Code: 关于接口的灵活性 新的仓库: wklken/naming 缓存使用的一些经验 Better Code: 抽象: 可扩展性与可维护性的抉择 Better Code: 异常时, 该提示用户哪些信息? Better Code: 更好的异常日志打印 Go: some libs Go: go-redis/cache升级的坑 Go: logrus性能提升 Go: gin validation 远程办公的一点总结 Go: 开发过程中的一些bug 项目管理实践: 风险驱动开发 Go: 一种error wrap调用链处理方式 漫谈技术选型 Go: 基于 apitest 做handler层单元测试 Go: go-sql-driver interpolateparams参数优化 [分享]深度工作 你需要更多的思考时间
读书笔记-重构: 章9 简化表达式
2016-12-04 · via Posts on WKLKEN THINKING

重构的读书笔记, 简单转成python版本的code, 供参考

9.1 Decompose Conditional 分解条件表达式

你有一个复杂的条件语句(if-then-else). 从if, the, else三个段落中分别提炼出独立函数

if date < SUMMER_START) or date > SUMMER_END:
    charge = quantity * _winter_rate + _winter_servioce_charge
else:
    charge = quantity * _summer_rate

to

if not_summber(date):
    charge = winter_charge(quantity)
else:
    charge = summber_charge(quantity)

9.2 Consolidate Cnditional Expression 合并条件表达式

你有一系列条件测试, 都得到相同结果. 将这些测试合并成一个条件表达式, 并将这个条件表达式提炼成为一个独立函数

if _seniority < 2:
    return 0
if _months_disabled > 10:
    return 0
if _is_part_time:
    return 0

to

if is_not_eligible_for_disability:
    return 0

9.3 Consolidate Dumplicate Conditional Fragments 合并重复的条件判断

在条件表达式的每个分支上有着相同的一段代码. 将这段重复代码搬移到条件表达式之外

if is_special:
    total = price * 0.95
    send()
else:
    total = price * 0.98
    send()

to

if is_special:
    total = price * 0.95
else:
    total = price * 0.98
send()

9.4 Remove Control Flag 移除控制标记

在一系列布尔表达式中, 某个变量带有"控制标记"(control flag)的作用. 以break语句或return取代控制标记

found = False
for i in range(5):
    print i
    if i == 3:
        found = True

return found

to

for i in range(5):
    print i
    if i == 3:
        return True

9.5 Replace Nested Conditional with Guard Clauses 以守卫语句取代嵌套条件表达式

函数中的条件逻辑使人难以看清正常的执行路径. 使用守卫语句表现所有特殊情况

在Python中相当有用

if _is_dead:
    result = dead_amount()
else:
    if _is_separated:
        result = separated_amount()
    else:
        if _is_retired:
            result = retired_amount()
        else:
            result = normal_payamount()
return result

to

if _is_dead:
    return dead_amount()

if _is_separated:
    return separated_amount()

if _is_retired:
    return retired_amount()

return normal_payamount()

9.6 Replace Conditional with Polymorphism 以多态取代条件表达式

你手上有多个条件表达式, 它根据对象类型的不同而选择不同的行为. 将这个条件表达式的每个分支放进一个子类内的覆写函数中, 然后将原始函数声明为抽象函数

if area == 'EUROPEAN':
    return get_base_speed()
elif area == 'AFRICAN':
    return get_base_speed() - get_load_factor() * _number_of_coconuts
else:
    return 0 if _is_nailed else get_base_speed(_voltage)
class Bird(object):
    def get_base_speed():
        pass

class European(Bird):
    def get_base_speed():
        pass

class African(Bird):
    def get_base_speed():
        pass

9.7 Introduce Null Object 引入Null对象

你需要再三检查某对象是否为null. 将null值替换为null对象

多态的根本好处是你不必再向对象询问: 你是什么类型, 然后根据类型调用其行为.

if customer is None:
    plan = billing_plan.basic()
else:
    plan = customer.get_plan()

if customer is None:
    customer_name = 'occupant'
else:
    customer_name = customer.get_name()

to

class Customer(object):
    pass

class NullCustomer(Customer):
    def get_plan(self):
        return billing_plan.basic()
    def get_name(self):
        return 'occupant'

9.8 Introduce Assertion 引入断言

某一段代码需要对程序状态做出某种假设. 以断言明确表示这种假设

# note: limit must greater 100 here
do something

to

assert limit > 100
do something