每一位程序員都應該學習的優秀代碼

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有時,開發人員編寫的代碼對硬件的利用能達到讓人驚歎的地步,並給整個世界留下深刻的印象。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當我閒暇時,我會瀏覽 Github 的代碼庫。使用新的計算機軟件、工具或實用程序時,我也喜歡琢磨下它的運行機制。通常情況,我會研究它的代碼庫,以瞭解其內部模塊和外部依賴是如何有機結合來完成工作。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"幾年前,我查看 Python 語言代碼時,突然發現了一些 C 語言編寫的源文件,並檢查了 GNU C 編譯器集合的實現。我發現這個先進的 C 語言編譯器就是用 C 語言編寫的。學習事物的內部結構並深入瞭解,可以顛覆我們的認知並學到豐富的知識。過去的開發人員通過自己的辛勤工作,爲現代開發人員創造了一個太平的世界。作爲現代開發人員,我們應該感謝他們的出色工作。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當我瀏覽 Github 代碼庫時,我注意到各地的開發人員完成了以下的傑作。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"阿波羅 11 號導航計算機(AGC)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"阿波羅 11 號使人類第一次成功地登上了月球。只有 4KB 物理內存的阿波羅導航計算機(AGC)完成了對航天器的控制。AGC 的軟件以 AGC 彙編語言編寫,並存儲在稱爲磁心線儲存器(ropememory)的特殊只讀存儲器中。之前有人將源代碼的掃描副本上傳到了 Internet。然後,這幾個模塊被轉換爲文本文件,並上傳到了 Github 代碼庫。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/wechat\/images\/64\/644a5e11518ab35e3d014601e89246f0.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":"center","origin":null},"content":[{"type":"text","text":"AGC 登月模塊中的代碼片段,作者截圖"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"阿波羅(Apollo)工程團隊付出了巨大的努力,編寫大量的彙編代碼來取得如此驚人的成就。在 1960 年代,編程可比現在困難多了。因爲在那時,編程語言多爲低級語言,需要直接操作硬件。所以,程序員必須編寫精心優化的代碼來提高硬件使用效率。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"雷神之錘 III 競技場"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"雷聲之錘 III——競技場是由 id Software 開發的第一人稱射擊遊戲。它開發於上世紀 90 年代,當時由於 id Tech3 等遊戲引擎的技術的出現,3D 遊戲行業開始興起。不像現在,那時候硬件資源非常有限。因此,遊戲開發人員必須編寫精心優化的代碼以實現圖形元素的渲染。許多遊戲編程都需要實現矢量歸一化,這提出了對平方根倒數運算的需求。如前所述,開發人員必須爲這些與圖形計算相關的工作選擇最有效的算法。因此,雷聲之錘 III——競技場開發團隊使用一種非常聰明的方法進行平方根倒數運算,比如下面這樣使用位級計算。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/wechat\/images\/b3\/b3f32e69c940a2546d16226c7b77337f.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":"center","origin":null},"content":[{"type":"text","text":"雷神之錘 III 競技場中的平方根倒數快速算法,作者截圖"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這段代碼的優化水平以及在 90 年代就能開發出如此出色的計算機遊戲這一事實,真的很讓人驚訝。如今,遊戲開發通常不需要處理這一級別的計算,因爲物理函數已經由遊戲引擎實現。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"GNU 編譯器代碼集"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"C 語言是高級編程語言的鼻祖,因爲它既非常靠近硬件又能提供良好的可讀性。作爲 C 語言的編譯器,GNU 是自引導的。換句話說,它是應用編譯器引導概念的 C 語言程序。Github 上的 GNU 編譯器代碼庫中的這個文件是我見過的最長的 C 語言源文件(可能有比這更長的 C 語言源文件,但是我沒見過)。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/wechat\/images\/e2\/e2a67bf82dc11215e84c2bcf06ba4390.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":"center","origin":null},"content":[{"type":"text","text":"GNU C 編譯器中的解析器的源文件有 2 萬多行代碼,作者截圖"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Chromium"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"流行的 Web 瀏覽器(例如 Google Chrome,Microsoft Edge 和 Opera)很多是基於 Chromium 開源項目,該項目有兩個主要依賴:1)Blink 渲染引擎。它是 WebCore 庫的一個分支,該庫由 Webkit 團隊基於 KHTML\/KJS 進行開發;2)v8 JavaScript 引擎。該引擎由 Chromium 團隊開發。毫無疑問,Chromium 代碼庫非常大,並且依賴很多第三方模塊,例如 gRPC 和 Skia。但是,Chromium 團隊以一種非常優雅的結構來組織所有組件。他們非常明智地將用戶界面和內部函數分開,以提高整個工程的可維護性。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/wechat\/images\/5b\/5b55218c6aa111cb0182e43cf2171cf0.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":"center","origin":null},"content":[{"type":"text","text":"劃分良好的 ChromiumUI 組件抽象層的目錄結構,作者截圖"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"此外,此代碼庫還包含 Chromium Android 和 iOS 手機端程序的源代碼。大規模的跨平臺應用程序目錄結構的劃分可能會有些複雜。但是,Chromium 對 Linux,Windows 和 Mac 不同平臺代碼目錄結構的劃分做的非常棒。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Gitk"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如今,基本上每一個軟件開發團隊都會使用 Git 來進行代碼歷史和版本的管理。Git 最初由 Linux 內核的創始人 LinusTorvalds 開發。確實,Gitk 的源文件的 Git 代碼中一個神奇的模塊。Gitk 是一個 GUI 應用程序,可以讓我們以可視化方式瀏覽歷史提交記錄。我們可以使用以下的 CLI 命令來比較兩次提交之間的差異。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"null"},"content":[{"type":"text","text":"$ git diff \n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"但是,藉助 Gitk 的可視化展示方式,我們可以更快速地明白不同提交之間的差異。Gitk 的整個源代碼只有一個文件。它是使用 Tk UI 工具包(Tcl 的擴展程序)以 Tcl 腳本語言編寫的。它僅僅通過一個源文件就實現了整個 GUI 應用程序,還選擇了一種動態編程語言來大大加快 GUI 應用程序的開發速度。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/wechat\/images\/60\/60ce9e46adf871302f1ce6ac681ff03d.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Gitk 由約 1 萬 2 千行代碼的單個源文件來實現,作者截圖"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"原文鏈接:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https:\/\/medium.com\/swlh\/impressive-sources-codes-that-every-developer-should-see-b68028b36da5","title":"","type":null},"content":[{"type":"text","text":"https:\/\/medium.com\/swlh\/impressive-sources-codes-that-every-developer-should-see-b68028b36da5"}]}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章