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万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章