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源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。