修改代码的艺术 work effectively with legacy code

可以说,大部分的程序员每天最多的工作就是修改遗留代码。

 

缺少测试的代码,就是遗留代码。缺少测试的代码非常糟糕。没有测试,我们无法快速且有保证地修改代码。

 

修改机制

修改代码的四大原因:

  1. 增加特性
  2. 修正缺陷
  3. 改善设计
  4. 优化资源

这四个修改代码的原因会对遗留代码带来以下影响

 

增加特性

修正缺陷

改善设计

优化资源

功能

-

变化

-

-

新功能

变化

-

-

-

结构

变化

变化

变化

-

资源

-

-

-

变化

 

 

为了降低修改遗留代码的风险,我们需要思考以下几个问题:

  1. 我们要做什么修改?
  2. 我们怎么知道已经正确做出了修改?
  3. 我们怎么知道修改对其他部分没有造成破坏?

 

 

做回归测试可以保证修改代码的正确性,但是一个大型项目,做一次回归测试会花费相当多的时间。如果你要改动一个大型的函数,其中包含了大量复杂的逻辑。那么,我们应该分析,思考,并且与更懂这段代码的人讨论,然后做出修改。

 

为了保证我们的修改没有引起其他部分的破环,必须要做测试。但是每一次回归测试都要耗费大量时间,这无疑降低了开发的效率。更好地一种测试方式是,单元测试。

 

单元测试的特性:

  1. 运行很快
  2. 能够帮助定位问题

 

遗留代码修改方法

  1. 确定变更点
  2. 找到测试点
  3. 打破依赖关系
  4. 编写测试
  5. 做出修改并重构

 

 

下次学习

遗留代码中的重要背景知识:感知/分离/接缝

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Re-Engineering Legacy Software by Chris Birchall English | 31 Jan. 2016 | ISBN: 1617292508 | 380 Pages | MOBI/EPUB/PDF (True) | 13.62 MB Most developers inherit projects built on an existing codebase that reflect design patterns, usage assumptions, infrastructure, and tooling from another time and another team. (And the docs are complete rubbish.) Fortunately, there are techniques that can breathe new life into legacy projects, allowing them to be maintained, improved, and scaled without having to constantly fight their current limitations. Stronger applications can be developed by injecting new architectural patterns, modern techniques for performance analysis, updated tests and build tools, and a healthy dose of cultural wisdom—creating a better work experience for all involved. Re-Engineering Legacy Software is an experience-driven guide to revitalizing inherited projects. It covers refactoring, quality metrics, toolchain and workflow, continuous integration, infrastructure automation, and organizational culture. On the technical side, readers will learn techniques for introducing dependency injection for code modularity, quantitatively measuring quality, and automating infrastructure. On the strategic side, they'll develop practical processes for solid decision making. Core topics include deciphering and modularizing awkward code structures, effectively integrating and automating tests, replacing an outdated build system, and infrastructure automation using tools like Vagrant and Ansible.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值