Google C++ Style Guide:Background

   鑑於哥比較垂涎谷歌的C++編寫風格,用哥這弱弱的英語水平,試着對谷歌的C++編寫風格進行翻譯下,僅供學習與參考,歡迎指出翻譯不當之處,請匆轉載。

  

Background

C++ is the main development language used by many of Google's open-source projects. As every C++ programmer knows, the language has many powerful features, but this power brings with it complexity, which in turn can make code more bug-prone and harder to read and maintain.

The goal of this guide is to manage this complexity by describing in detail the dos and don'ts of writing C++ code. These rules exist to keep the code base manageable while still allowing coders to use C++ language features productively.

Style, also known as readability, is what we call the conventions that govern our C++ code. The term Style is a bit of a misnomer, since these conventions cover far more than just source file formatting.

One way in which we keep the code base manageable is by enforcing consistency. It is very important that any programmer be able to look at another's code and quickly understand it. Maintaining a uniform style and following conventions means that we can more easily use "pattern-matching" to infer what various symbols are and what invariants are true about them. Creating common, required idioms and patterns makes code much easier to understand. In some cases there might be good arguments for changing certain style rules, but we nonetheless keep things as they are in order to preserve consistency.

Another issue this guide addresses is that of C++ feature bloat. C++ is a huge language with many advanced features. In some cases we constrain, or even ban, use of certain features. We do this to keep code simple and to avoid the various common errors and problems that these features can cause. This guide lists these features and explains why their use is restricted.

Open-source projects developed by Google conform to the requirements in this guide.

Note that this guide is not a C++ tutorial: we assume that the reader is familiar with the language.

 

 

背景:C++開發語言在谷歌的許多開源項目當中擔任着主要角色。正如每個C++程序員所知,C++語言有着許多強勁功能,然而正是這種強大所帶來的紛繁複雜,又反而使得代碼容易出錯、艱澀難懂又不好維護。

這篇指南旨在通過細緻講述C++編寫時的注意事項以駕馭這些細節。在保持代碼基本可維護性的同時,這些規則又會使得程序員能高效地使用C++。

風格,也熟知爲可讀性,就是所謂的代碼寫法。風格這詞本身就有點讓人誤解,因爲這些條例遠不止就是將代碼格式化而已。

加強代碼的一致性是我們常用來保持代碼基本可維護性的一種辦法。很重要的一點就是讓所有的程序員都能快速讀懂並理解別人的代碼。保持一致的風格,遵循相同的規則意味着我們將可以更輕易地通過形式的對比,就知曉不同符號的涵義並找到對應的常量。創建一種共用的、必要的模式與慣用寫法將會使得代碼更通俗易懂。有時也許對某些固定風格的修改會引發激烈的爭議,但出於維持一致性的目的,我們仍將堅持不改變。

本文解決的另一個問題就是C++代碼的膨脹性。C++是一門擁有許多先進優勢的龐大語言。在某些情況下,我們會限制甚至禁止一些特定的格式。這麼做一來保持了代碼的簡潔,又避免了由語言特徵所引發的諸多常見問題與錯誤。文中列舉了些格式並解釋了我們要限制這些做法的原因。

谷歌的開源項目也遵循着本文的規定。

請注意本文並不是一篇C++教學輔導文章:我們認爲閱讀者已經熟悉這門語言。

發佈了49 篇原創文章 · 獲贊 131 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章