Skip to Content
程序员的38堂成长课
book

程序员的38堂成长课

by Pete Goodliffe
January 2022
Beginner to intermediate
314 pages
6h 55m
Chinese
Posts & Telecom Press
Content preview from 程序员的38堂成长课
走出泥潭
53
要诀
慎选战场。权衡是否应该投入时间和精力来“清理”糟糕的代码。置
之不理可能更务实。
即使现在不适合大规模地重写代码,也不要让污水放任自流。你可以通过小规模的清理来
逐步取得控制权。
7.5
 清理混乱
无论是持久战,还是闪电战,你都应当听从罗伯特
C.
马丁的建议,遵循童子军军规:总
是保证营地比你来的时候更干净。虽然今天不适合进行全面改进,但你可以让代码世界变
得稍微不那么糟糕。
要诀
遵循童子军军规。只要修改代码,就让它变得比修改之前
更好
可以简单地清理混乱:修正不一致的缩进、纠正容易让人误解的变量名、简化复杂的条件
判断,或者将长方法拆分并恰当命名。
如果你经常修改一段代码,并且每次都略有改善,那么也许不久之后它就是一段
好代码
了。
7.6
 做出改变
处理混乱代码的要诀如下。
要诀
缓慢地、谨慎地修改代码。每次改变一点点。
这是重中之重,我建议你停下来,倒回去,再读一遍。
下面是一些具体、实用的方法。
修改功能时不要调整代码布局。可以把布局整理一下再提交代码,
之后
才进行功能修改。
(最好还是保留布局,除非它已经成为某种障碍。)
一定要确保“调整”不会改变代码现有的行为。使用可靠的自动化工具,并且仔细评审
并检查你的更改,然后邀请其他人评审代码。这可以称为
重构
,即一组用于改进代码结
构的常见技术。
只有健全的单元测试才能保证重构成功。混乱的代码很可能没有进行任何测试。因此请
首先考虑编写一些测试来覆盖关键代码。
调整
API
,而不是直接修改内部逻辑。调整接口的命名、参数类型、顺序以及一致性 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

管理Kubernetes

管理Kubernetes

Brendan Burns, Craig Tracey
Python应用开发指南

Python应用开发指南

Posts & Telecom Press, Ninad Sathaye
解密金融数据

解密金融数据

Justin Pauley

Publisher Resources

ISBN: 9787115577948