This document is based on the work of
B. Weinberger, C. Silverstein,
G. Eitzmann, M. Mentovai
and T.Landray
at http://google-styleguide.googlecode.com,
C++ Google Style guide, Revision 3.245
under the CC-By 3.0 License
Each style point has a summary for which additional information is available by toggling the accompanying arrow button that looks this way:
. You may toggle all summaries with the big arrow button:The goal of this guide is to provide a number of rules that keep the code base manageable by enforcing consistency. It is very important that any programmer can look at another programmer's code and understand it quickly. Maintaining a uniform style and following conventions means that "pattern-matching" can be more easily used to identify different symbols and invariants.
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. Nonetheless, for reasons of consistency the rules are left unchanged.
Note that this guide is not a C++ tutorial: we assume that the reader is familiar with the language.
The most important consistency rules are those that govern naming. The style of a name immediately informs us what sort of thing the named entity is: a type, a variable, a function, a macro, etc., without requiring us to search for the declaration of that entity. The pattern-matching engine in our brains relies a great deal on these naming rules.
Naming rules are pretty arbitrary, but we feel that consistency is more important than individual preferences in this area, so regardless of whether you find them sensible or not, the rules are the rules.
o2
, and hyphenated : o2-tpc-reco-workflow
O2
.
MyClass
, MyEnum
.
myLocalVariable
.
m
.s
.m
prefix for struct members.g
.constexpr
variables are capitalized.const
.myFunction()
.
get
and set
:
getMyMemberVariable()
, setMyMemberVariable()
.is
or has
. my_namespace
.
MyEnumType
, MyEnumValue
.
MY_PROJECT_PKG1_MY_MACRO
.
Coding style and formatting are pretty arbitrary. However, a good project is much easier to follow if everyone uses the same style. Individuals may not agree with every aspect of the formatting rules, and some of the rules may be hard to get used to. Even so, it is important that all project contributors follow the style rules so that they can all read and understand everyone's code easily.
The coding conventions described above have to be followed. However, like all good rules, these sometimes have exceptions.
Use common sense and BE CONSISTENT.
When editing code, take a few minutes to look at the code and determine its style.
The point about having style guidelines is to have a common vocabulary of coding so people can concentrate on what the programmer is saying, rather than on how he/she is saying it. Global style rules are presented here so people know the vocabulary. However, local style is also important. If the code added to a file looks drastically different from the existing code around it, the discontinuity throws readers out of their rhythm when they go to read it. Try to avoid this.
OK, enough writing about writing code; the code itself is much more interesting. Have fun!
[1] Herb Sutter on software, hardware, and concurrency blog [http://herbsutter.com/2013/05/09/gotw-1-solution]