Documentation/translations/zh_TW/process/4.Coding.rst
Source file repositories/reference/linux-study-clean/Documentation/translations/zh_TW/process/4.Coding.rst
File Facts
- System
- Linux kernel
- Corpus path
Documentation/translations/zh_TW/process/4.Coding.rst- Extension
.rst- Size
- 18332 bytes
- Lines
- 298
- Domain
- Support Tooling And Documentation
- Bucket
- Documentation
- Inferred role
- Support Tooling And Documentation: documentation
- Status
- atlas-only
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
Dependency Surface
- No C-style include directives detected by the generator.
Detected Declarations
- No top-level syscall, struct, function, initcall, or export declaration detected by the generator.
Annotated Snippet
.. SPDX-License-Identifier: GPL-2.0
.. include:: ../disclaimer-zh_TW.rst
:Original: :ref:`Documentation/process/4.Coding.rst <development_coding>`
:Translator:
時奎亮 Alex Shi <alex.shi@linux.alibaba.com>
:校譯:
吳想成 Wu XiangCheng <bobwxc@email.cn>
胡皓文 Hu Haowen <2023002089@link.tyut.edu.cn>
.. _tw_development_coding:
使代碼正確
======================
雖然一個堅實的、面向社區的設計過程有很多值得說道的,但是任何內核開發項目工作
的證明都反映在代碼中。它是將由其他開發人員檢查併合並(或不合並)到主線樹中
的代碼。所以這段代碼的質量決定了項目的最終成功。
本節將檢查編碼過程。我們將從內核開發人員常犯的幾種錯誤開始。然後重點將轉移
到正確的做法和相關有用的工具上。
陷阱
----
代碼風格
********
內核長期以來都有其標準的代碼風格,如
:ref:`Documentation/translations/zh_CN/process/coding-style.rst <tw_codingstyle>`
中所述。在多數時候,該文檔中描述的準則至多被認爲是建議性的。因此,內核中存在
大量不符合代碼風格準則的代碼。這種代碼的存在會給內核開發人員帶來兩方面的危害。
首先,相信內核代碼標準並不重要,也不強制執行。但事實上,如果沒有按照標準
編寫代碼,那麼新代碼將很難加入到內核中;許多開發人員甚至會在審查代碼之前要求
對代碼進行重新格式化。一個像內核這麼大的代碼庫需要一些統一格式的代碼,以使
開發人員能夠快速理解其中的任何部分。所以再也經不起奇怪格式的代碼的折騰了。
內核的代碼風格偶爾會與僱主的強制風格發生衝突。在這種情況下,必須在代碼合併
之前遵從內核代碼風格。將代碼放入內核意味着以多種方式放棄一定程度的控制權——
包括控制代碼樣式。
另一個危害是認爲已經在內核中的代碼迫切需要修復代碼樣式。開發者可能會開始編寫
重新格式化補丁,作爲熟悉開發過程的一種方式,或者作爲將其名字寫入內核變更日誌
的一種方式,或者兩者兼而有之。但是純代碼風格的修復被開發社區視爲噪音,它們往
往受到冷遇。因此,最好避免編寫這種類型的補丁。在由於其他原因處理一段代碼的
同時順帶修復其樣式是很自然的,但是不應該僅爲了更改代碼樣式而更改之。
代碼風格文檔也不應該被視爲絕對不可違反的規則。如果有一個足夠的理由反對這種
樣式(例如爲了80列限制拆分行會導致可讀性大大降低),那麼就這樣做吧。
注意您還可以使用 ``clang-format`` 工具來幫助您處理這些規則,快速自動重新格式
化部分代碼,和審閱完整的文件以發現代碼樣式錯誤、拼寫錯誤和可能的改進。它還
可以方便地排序 ``#includes`` 、對齊變量/宏、重排文本和其他類似任務。有關詳細
信息,請參閱文檔 :ref:`Documentation/dev-tools/clang-format.rst <clangformat>`
抽象層
******
計算機科學教授教學生以靈活性和信息隱藏的名義廣泛使用抽象層。當然,內核廣泛
地使用了抽象;任何涉及數百萬行代碼的項目都必須做到這一點以存續下來。但經驗
表明,過度或過早的抽象可能和過早的優化一樣有害。抽象應用在適當層級,
不要過度。
簡單點,先考慮一個調用時始終只有一個參數且總爲零的函數。我們可以保留這個參數,
Annotation
- Atlas domain: Support Tooling And Documentation / Documentation.
- Implementation status: atlas-only.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.