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

推荐订阅源

L
LangChain Blog
C
Check Point Blog
博客园 - Franky
V
Visual Studio Blog
云风的 BLOG
云风的 BLOG
aimingoo的专栏
aimingoo的专栏
Microsoft Security Blog
Microsoft Security Blog
V2EX - 技术
V2EX - 技术
AI
AI
Hacker News - Newest:
Hacker News - Newest: "LLM"
Jina AI
Jina AI
S
Security @ Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
H
Hacker News: Front Page
H
Hackread – Cybersecurity News, Data Breaches, AI and More
O
OpenAI News
Attack and Defense Labs
Attack and Defense Labs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
爱范儿
爱范儿
H
Heimdal Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
G
Google Developers Blog
G
GRAHAM CLULEY
V
V2EX
The Register - Security
The Register - Security
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
Schneier on Security
Schneier on Security
M
MIT News - Artificial intelligence
Stack Overflow Blog
Stack Overflow Blog
Help Net Security
Help Net Security
大猫的无限游戏
大猫的无限游戏
C
CERT Recently Published Vulnerability Notes
The GitHub Blog
The GitHub Blog
V
Vulnerabilities – Threatpost
The Last Watchdog
The Last Watchdog
J
Java Code Geeks
S
Secure Thoughts
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
量子位
NISL@THU
NISL@THU
K
Kaspersky official blog
Engineering at Meta
Engineering at Meta
T
Threatpost
Recent Commits to openclaw:main
Recent Commits to openclaw:main
宝玉的分享
宝玉的分享
Security Latest
Security Latest
T
The Exploit Database - CXSecurity.com
博客园_首页
A
Arctic Wolf

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参数优化 [分享]深度工作 你需要更多的思考时间 Django项目重构小结 工作七年小结: 学习,生活及其他 [分享]bash日常: bash-utils 极客时间推广海报 2017总结: 予时光以意义 k8s APIServer源码: api注册详细细节 k8s APIServer源码: api注册主体流程 k8s APIServer源码: 服务启动 k8s APIServer源码: go-restful框架 重构 - 读书笔记(Python示例) 写给新人的沟通建议 vim 杂谈 - 关于快速编辑 vim 杂谈 - 关于移动 读书笔记-重构: 章11 处理概括关系 读书笔记-重构: 章10 简化函数调用 读书笔记-重构: 章9 简化表达式 读书笔记-重构: 章8 重新组织数据 读书笔记-重构: 章7 在对象之间搬移特性 Python 代码规范小结 [分享]关于vim ElasticSearch集群部署文档 Logstash+ElasticSearch处理mysql慢查询日志 [分享]关于代码调试DE那些事 Logstash+ElasticSearch+Kibana- 实现相对通用的数据收集分析 ELK维护的一些点(二) [分享]Python源码剖析-数据结构 一些Centos Python生产环境的部署命令 摘录<<6个月学会任何一种外语>> ELK 维护的一些点 也许是一个新的开始 一些vim的个性化配置 读书笔记-调试九法 这段时间的一些想法 Python 源码阅读 - 垃圾回收机制 我为什么要写博客 APUE笔记-第一章 UNIX基础知识 Python源码阅读-闭包的实现 Python源码阅读-内存管理机制(二) Python源码阅读-内存管理机制(一) Python-基础-数据结构小结 '活动'设计的一些trick 一些简单的Python测试题 我的tmux配置及说明【k-tmux】 Review and Restart 工作四周年小结 vim插件: surround & repeat[成对符号编辑] vim插件: gundo[时光机] vim插件: expand-region[区域选中] vim插件: quickrun[快速执行] vim插件: trailing-whitespace[行尾空格处理] vim插件: closetag[成对标签补全] vim插件: ctrlp[文件搜索] vim插件: airline[状态栏增强] vim插件: theme[主题] vim插件: tagbar[大纲式导航] vim插件: nerdcommenter[快速注释] vim插件: rainbow_parentheses[括号高亮] vim插件: syntastic[语法检查] vim插件: delimitmate[符号自动补全] vim插件: matchit[成对标签跳转] vim插件: easy-align[快速对齐] vim插件: multiple-cursors[多光标操作] vim插件: vim-signature[快速标记跳转] vim插件: easymotion[快速跳转] vim插件: vundle[管理插件] Elasticsearch几个问题的解决 分享一份 Vim 简介PPT k-vim 更新9.0版本 关于知识管理工具的思考 Logstash+ElasticSearch+Kibana处理nginx访问日志
读书笔记-重构: 章6 重新组织函数
2016-12-03 · via Posts on WKLKEN THINKING

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

你有一段代码可以被组织在一起并独立出来, 将这段代码放进一个独立函数中, 并让函数名称解释该函数的用途

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" % amount

def 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 1
def 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_price

if base_price > 1000:
    return base_price * 0.95
else:
    return base_price * 0.98

to

if base_price() > 1000:
    return base_price() * 0.95
else:
    return base_price() * 0.98

def 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 > 0

if is_macos and is_ie_browser and was_initialized() and was_resized:
    # do something

6.6 Split Temporary Variable 分解临时变量

你的程序有某个临时变量被赋值过一次, 它既不是循环变量, 也不被利用与收集计算结果. 针对每次赋值, 创造一个独立, 对应的临时变量

tmp = 2 * (height * width)
print tmp

tmp = height * width
print tmp

to

perimeter = 2 * (height * width)
print perimeter

area = height * width
print 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 以函数对象取代函数

你有一个大型函数, 其中对局部变量的使用使你无法采用Extract method. 将这个函数放进一个单独对象中, 如此一来局部变量就成了对象内的字段, 然后你可以在同一个对象中将这个大型函数分解成多个小型函数

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, "")