C++核心準則?附錄B:代碼現(xiàn)代化

Appendix B: Modernizing code
附錄B:代碼現(xiàn)代化
Ideally, we follow all rules in all code. Realistically, we have to deal with a lot of old code:
理想情況下,我們遵循所有代碼中的所有規(guī)則。實際上,我們必須處理許多舊代碼:
application code written before the guidelines were formulated or known
在制定或了解準則之前編寫的應用程序代碼
libraries written to older/different standards
按照較舊/不同標準編寫的庫
code written under "unusual" constraints
在“異?!奔s束下編寫的代碼
code that we just haven't gotten around to modernizing
我們還沒有實現(xiàn)現(xiàn)代化的代碼
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.
如果我們有一百萬行新代碼,那么“立即更改所有內容”的想法通常是不現(xiàn)實的。因此,我們需要一種逐步現(xiàn)代化代碼庫的方法。
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.
將較舊的代碼升級為現(xiàn)代風格可能是一項艱巨的任務。通常,舊代碼雖然一片混亂(難以理解),但卻可以正常工作(針對當前使用范圍)。通常,最早的程序員不在身邊并且測試用例不完整。代碼混亂不堪的事實極大地增加了進行任何更改所需的工作量,并增加了引入錯誤的風險。通常,凌亂的舊代碼沒有任何必要性的地緩慢運行,因為它需要過時的編譯器,并且無法利用現(xiàn)代硬件。在許多情況下,大型升級工作需要自動化的“現(xiàn)代化程序”式工具支持。
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.
代碼現(xiàn)代化的目的是簡化添加新功能的過程,簡化維護并提高性能(吞吐量或延遲),以及更好地利用現(xiàn)代硬件。使代碼“看起來很漂亮”或“遵循現(xiàn)代風格”本身并不是更改的原因。每一次更改都隱含著風險,而過時的代碼庫則隱含了成本(包括失去機會的成本)。降低的成本必須超過風險。
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:
使代碼現(xiàn)代化的方法不止一種。如何做到最好取決于代碼,更新的壓力,開發(fā)人員的背景以及可用的工具。以下是一些(非?;\統(tǒng)的)想法:
The ideal is "just upgrade everything." That gives the most benefits for the shortest total time. In most circumstances, it is also impossible.
理想是“只需升級所有內容”。這樣可以在最短的總時間內獲得最大的收益。在大多數(shù)情況下,這也是不可能的。
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)的規(guī)則(例如使用范圍)都不能在每個模塊的基礎上完成。
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.
我們可以從估計可以帶來最大收益和/或最小麻煩的規(guī)則開始,“自下而上”轉換代碼。
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.
我們可以從專注于接口開始,例如,確保沒有資源丟失并且沒有濫用指針。這將是整個代碼庫中的一組更改,但很可能會帶來巨大的好處。之后,隱藏在這些接口后面的代碼可以逐步實現(xiàn)現(xiàn)代化,而不會影響其他代碼。
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.
無論選擇哪種方式,請注意,大部分好處來自于與準則的最高度符合。準則不是隨機的,不相關的規(guī)則集,您不應該期待在其中隨機選擇就能成功。
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.
我們非常希望聽到有關經驗和所用工具的信息。當分析工具甚至代碼轉換工具支持時,現(xiàn)代化可以更快,更簡單,更安全。
原文鏈接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#appendix-b-modernizing-code
新書介紹
《實戰(zhàn)Python設計模式》是作者最近出版的新書,拜托多多關注!

本書利用Python 的標準GUI 工具包tkinter,通過可執(zhí)行的示例對23 個設計模式逐個進行說明。這樣一方面可以使讀者了解真實的軟件開發(fā)工作中每個設計模式的運用場景和想要解決的問題;另一方面通過對這些問題的解決過程進行說明,讓讀者明白在編寫代碼時如何判斷使用設計模式的利弊,并合理運用設計模式。
對設計模式感興趣而且希望隨學隨用的讀者通過本書可以快速跨越從理解到運用的門檻;希望學習Python GUI 編程的讀者可以將本書中的示例作為設計和開發(fā)的參考;使用Python 語言進行圖像分析、數(shù)據(jù)處理工作的讀者可以直接以本書中的示例為基礎,迅速構建自己的系統(tǒng)架構。
覺得本文有幫助?請分享給更多人。
關注微信公眾號【面向對象思考】輕松學習每一天!
面向對象開發(fā),面向對象思考!
