The Philosophy of Google's C++ Style Guide - Titus Winters - CppCon 2014
The Philosophy of Google's C++ Style Guide - Titus Winters - CppCon 2014
BORING
QUESTION!
What Goes in a Style Guide?
What Goes in a Style Guide?
WRONG
QUESTION!
What is the Purpose of a Style Guide?
What is the Purpose of a Style Guide?
What’s the purpose of any rule or set of rules your organization puts
out?
What is the Purpose of a Style Guide?
What’s the purpose of any rule or set of rules your organization puts
out?
● Heavy handed throw-your-weight-around hoop jumping
What is the Purpose of a Style Guide?
What’s the purpose of any rule or set of rules your organization puts
out?
● Heavy handed throw-your-weight-around hoop jumping
● Make it harder for people to do “bad” things, encourage “good”
things
○ Clearly depends on your organization’s goals
Outline
Most projects check into the same codebase. Most engineers have
read access to most code. Most projects use the same infrastructure
(libraries, build system, etc).
Meaning?
Code is going to live a long time, and be read many times. We choose
explicitly to optimize for the reader, not the writer.
Philosophies of the
Style Guide
#1 Optimize for the Reader, not the Writer
We aren’t going to list every single thing you shouldn’t do. Rules for
dumb stuff should be handled at a higher level (“Don’t be clever”).
#3 Value the Standard, but don’t Idolize
Old Example: “No non-const references” leads to “The extra ‘&’ means
it could be mutated.”
Old Example: “No non-const references” leads to “The extra ‘&’ means
it could be mutated.”
// or new to new
TakeFoo(std::move(my_foo));
// or old to new
TakeFoo(std::make_unique<Foo>(my_foo));
#5 If something unusual is happening, leave explicit
evidence for the reader
Waivers here are probably rare, and would require a strong argument,
and probably some comments to mitigate the chance of copy and paste
re-using those patterns unsafely.
Examples include:
● Static and global variables of complex type (danger at shutdown)
● Use override or final (avoid surprise)
● Exceptions (dangerous)
#6 Avoid tricky and hard-to-maintain constructs
Most code should avoid the tricky stuff. Waivers may be granted if
justified.
Re-evaluate.
And with that . . .
Questions?