Appendix B: Modernizing codegit
附录B:代码现代化程序员
Ideally, we follow all rules in all code. Realistically, we have to deal with a lot of old code:github
理想状况下,咱们遵循全部代码中的全部规则。实际上,咱们必须处理许多旧代码:web
application code written before the guidelines were formulated or known编程
在制定或了解准则以前编写的应用程序代码
设计模式libraries written to older/different standards安全
按照较旧/不一样标准编写的库
微信code written under "unusual" constraints架构
在“异常”约束下编写的代码
appcode that we just haven't gotten around to modernizing
咱们尚未实现现代化的代码
If we have a million lines of new code, the idea of "just changing it all at once" is typically unrealistic. Thus, we need a way of gradually modernizing a code base.
若是咱们有一百万行新代码,那么“当即更改全部内容”的想法一般是不现实的。所以,咱们须要一种逐步现代化代码库的方法。
Upgrading older code to modern style can be a daunting task. Often, the old code is both a mess (hard to understand) and working correctly (for the current range of uses). Typically, the original programmer is not around and the test cases incomplete. The fact that the code is a mess dramatically increases the effort needed to make any change and the risk of introducing errors. Often, messy old code runs unnecessarily slowly because it requires outdated compilers and cannot take advantage of modern hardware. In many cases, automated "modernizer"-style tool support would be required for major upgrade efforts.
将较旧的代码升级为现代风格多是一项艰巨的任务。一般,旧代码虽然一片混乱(难以理解),但却能够正常工做(针对当前使用范围)。一般,最先的程序员不在身边而且测试用例不完整。代码混乱不堪的事实极大地增长了进行任何更改所需的工做量,并增长了引入错误的风险。一般,凌乱的旧代码没有任何须要性的地缓慢运行,由于它须要过期的编译器,而且没法利用现代硬件。在许多状况下,大型升级工做须要自动化的“现代化程序”式工具支持。
The purpose of modernizing code is to simplify adding new functionality, to ease maintenance, and to increase performance (throughput or latency), and to better utilize modern hardware. Making code "look pretty" or "follow modern style" are not by themselves reasons for change. There are risks implied by every change and costs (including the cost of lost opportunities) implied by having an outdated code base. The cost reductions must outweigh the risks.
代码现代化的目的是简化添加新功能的过程,简化维护并提升性能(吞吐量或延迟),以及更好地利用现代硬件。使代码“看起来很漂亮”或“遵循现代风格”自己并非更改的缘由。每一次更改都隐含着风险,而过期的代码库则隐含了成本(包括失去机会的成本)。下降的成本必须超过风险。
But how?
可是应该怎么作呢?
There is no one approach to modernizing code. How best to do it depends on the code, the pressure for updates, the backgrounds of the developers, and the available tool. Here are some (very general) ideas:
使代码现代化的方法不止一种。如何作到最好取决于代码,更新的压力,开发人员的背景以及可用的工具。如下是一些(很是笼统的)想法:
The ideal is "just upgrade everything." That gives the most benefits for the shortest total time. In most circumstances, it is also impossible.
理想是“只需升级全部内容”。这样能够在最短的总时间内得到最大的收益。在大多数状况下,这也是不可能的。
We could convert a code base module for module, but any rules that affects interfaces (especially ABIs), such as use span, cannot be done on a per-module basis.
咱们能够将代码基础模块转换为模块,可是任何影响接口(尤为是ABI)的规则(例如使用范围)都不能在每一个模块的基础上完成。
We could convert code "bottom up" starting with the rules we estimate will give the greatest benefits and/or the least trouble in a given code base.
咱们能够从估计能够带来最大收益和/或最小麻烦的规则开始,“自下而上”转换代码。
We could start by focusing on the interfaces, e.g., make sure that no resources are lost and no pointer is misused. This would be a set of changes across the whole code base, but would most likely have huge benefits. Afterwards, code hidden behind those interfaces can be gradually modernized without affecting other code.
咱们能够从专一于接口开始,例如,确保没有资源丢失而且没有滥用指针。这将是整个代码库中的一组更改,但极可能会带来巨大的好处。以后,隐藏在这些接口后面的代码能够逐步实现现代化,而不会影响其余代码。
Whichever way you choose, please note that the most advantages come with the highest conformance to the guidelines. The guidelines are not a random set of unrelated rules where you can randomly pick and choose with an expectation of success.
不管选择哪一种方式,请注意,大部分好处来自于与准则的最高度符合。准则不是随机的,不相关的规则集,您不该该期待在其中随机选择就能成功。
We would dearly love to hear about experience and about tools used. Modernization can be much faster, simpler, and safer when supported with analysis tools and even code transformation tools.
咱们很是但愿听到有关经验和所用工具的信息。当分析工具甚至代码转换工具支持时,现代化能够更快,更简单,更安全。
原文连接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#appendix-b-modernizing-code
新书介绍
《实战Python设计模式》是做者最近出版的新书,拜托多多关注!
本书利用Python 的标准GUI 工具包tkinter,经过可执行的示例对23 个设计模式逐个进行说明。这样一方面可使读者了解真实的软件开发工做中每一个设计模式的运用场景和想要解决的问题;另外一方面经过对这些问题的解决过程进行说明,让读者明白在编写代码时如何判断使用设计模式的利弊,并合理运用设计模式。
对设计模式感兴趣并且但愿随学随用的读者经过本书能够快速跨越从理解到运用的门槛;但愿学习Python GUI 编程的读者能够将本书中的示例做为设计和开发的参考;使用Python 语言进行图像分析、数据处理工做的读者能够直接以本书中的示例为基础,迅速构建本身的系统架构。
以为本文有帮助?请分享给更多人。
关注微信公众号【面向对象思考】轻松学习每一天!
面向对象开发,面向对象思考!
本文分享自微信公众号 - 面向对象思考(OOThinkingDalian)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。