標籤:
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.
//C++特性強大 -> C++難讀難維護
很多Google的開源項目把C++作為程式設計語言。每個C++程式員都知道C++有很多強大的特性,但是越強大,C++的難度也變得越大。難度就招來了Bug,同時代碼也變得難讀和難以維護。
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.
//這篇文章會告訴你哪些該做。
這篇指導的目標是,告訴你寫C++代碼時該做的和不該做的,通過這些來管控C++的難度。在保證程式員可以高效使用C++特性的同時,使得程式碼程式庫可以維護,這就是這些準則所存在的意義。
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.
//代碼風格的定義不僅僅局限在原始碼檔案的格式上。
代碼風格,或者說所謂的可讀性,是我們掌控C++代碼的約定。因為這些約定不僅僅局限在原始碼檔案的格式上,所以風格這個術語有點用詞不當。
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.
//不用隨便使用C++進階特性
這篇指導文章將說說另一個問題:對C++特性的濫用。C++擁有很多進階特性,它是個很大規模的語言。一些情況下,我們限制或者是禁止去使用某種特性。這樣做是為了代碼能夠簡單,同時避免各種錯誤和問題。這篇指導文章列出了這些特性,解釋了為什麼應該限制使用它們。
Open-source projects developed by Google conform to the requirements in this guide.
Google公司的開源項目都符合這篇導文中提出的要求。
Note that this guide is not a C++ tutorial: we assume that the reader is familiar with the language.
注意這篇文章不進行C++教學:我們假設你熟悉這門語言。
GoogleC++風格指南之通俗譯文(譯文保留著作權,勿轉載)