C++核心准则附录B:代码现代化
Appendix B: Modernizing code
附录B:代码现代化
Ideally, we follow all rules in all code. Realistically, we have to deal with a lot of old code:
理想情况下,我们遵循所有代码中的所有规则。实际上,我们必须处理许多旧代码:
application code written before the guidelines were formulated or known
在制定或了解准则之前编写的应用程序代码
libraries written to older/different standards
按照较旧/不同标准编写的库
code written under "unusual" constraints
在“异常”约束下编写的代码
code 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 语言进行图像分析、数据处理工作的读者可以直接以本书中的示例为基础,迅速构建自己的系统架构。
觉得本文有帮助?请分享给更多人。
关注微信公众号【面向对象思考】轻松学习每一天!
面向对象开发,面向对象思考!