Google C++ Style Guide Is No Good
This article is so full of misconceptions.<p>&gt; Unlike C++ Core Guidelines that try to explain how to use the language effectively, GSG is about forbidding the use of certain features.<p>They aren&#x27;t meant to accomplish the same thing. The GSG&#x27;s goal is to standardize both style and feature set across the mono repo so that thousands of engineers can effectively contribute.<p>&gt; We should not bifurcate developers into two camps as the reality exists in a continuum between these two extremes.<p>It&#x27;s a style guide, not an unwavering book of law. Library writers use some features that aren&#x27;t necessary in non-library code. In fact, some language features are written specifically to the benefit of the implementers.<p>&gt; GSG prefers #ifndef&#x2F;#define idiom over the simpler #pragma once. Yes, #pragma once is non-standard but widely supported.<p>When the difference is a 2 lines of code that you probably have tooling to generate anyways, why not default to the thing that&#x27;s standard?<p>&gt; Yes, both of these forward declarations are possible to avoid by type-punning through a void* but it is not a good pattern.<p>The point of &quot;Avoid using fwd-declarations where possible&quot; is exactly that: reason about the code and whether the declaration is necessary. It&#x27;s not &quot;dogmatically avoid forward declarations&quot;. I&#x27;m sure no-one at Google is punning through void* for those examples.<p>&gt; Marking the function “inline” lets the compiler make the decision. Not marking it inline is a sure way to prevent inlining, unless Link Time Optimizations are turned on.<p>That&#x27;s simply not true. The compiler can inline a function if it can infer that it doesn&#x27;t alter the program&#x27;s behavior. Generally, tools make good decisions. Override them when you&#x27;ve measured that something else is better.<p>&gt; Library code should always be placed in a namespace. Top level&#x2F;application code has questionable value being placed in a namespace.<p>It&#x27;s easier to be diligent about placing everything in a namespace because application code doesn&#x27;t always remain so. Especially not in a gigantic mono repo like Google&#x27;s.<p>&gt; GSG prohibits the use of static definitions and annonymous namespaces in header files. How else do we declare constants?<p>I mean, you declare your constants in the header and define them in the implementation file. First example I can find in Chromium: <a href="https:&#x2F;&#x2F;cs.chromium.org&#x2F;chromium&#x2F;src&#x2F;ios&#x2F;chrome&#x2F;browser&#x2F;pref_names.h?rcl=82d8ee1e070100260963c8d76f23dcd2884099c9&amp;l=11" rel="nofollow">https:&#x2F;&#x2F;cs.chromium.org&#x2F;chromium&#x2F;src&#x2F;ios&#x2F;chrome&#x2F;browser&#x2F;pref...</a><p>&gt; The rule basically says that global&#x2F;static variables with non-trivial constructors and destructors are not allowed. While it’s true that initialization&#x2F;destruction order of globals between translation units is not defined and can pose problems, this rule is overly prohibitve.<p>This rule isn&#x27;t overly prohibitive in the context of large applications that require clean startups&#x2F;shutdowns and contain objects that are sensitive to construction&#x2F;destruction order.<p>&gt; “Do not define implicit conversions”. I would urge the reader to consider what life would be like if std::string(const char) constructor was marked explicit (especially in the absense of user defined literals, which GSG also outlaws). Nuff said.<p>The issue with implicit conversions is that they&#x27;re not explicit to callers. Honestly, having to wrap a few const char* into std::string() calls wouldn&#x27;t be as bad as you seem to think it would be. Explicit code is easier to read and reason about.<p>&gt; “a copyable class should explicitly declare the copy operations, a move-only class should explicitly declare the move operations” – this goes against the language philosophy.<p>Again, this is about being explicit, and using safe defaults. It&#x27;s not prohibited to make things copyable and movable but defaulting to the most restrictive set up and adding things as needed ensures that developers thought about the implications of those properties.<p>&gt; In this pattern the private base cannot be replaced by a data member because the base is constructed before the members.<p>I&#x27;ve never seen code with private inheritance that made sense and couldn&#x27;t be refactored to something clearer.<p>&gt; Operator Overloading<p>See my point about implicit conversion above because it&#x27;s the same thing here. Operator Overloading tends to obfuscate code.<p>&gt; I think this rule mixes together two ideas for no good reason. C++ has a clear way to denote mutability – the const qualifier. Using pointer vs reference to signal mutability goes against the language.<p>The const qualifier isn&#x27;t visible from the call site. This rule makes it so that you can reason about what will happen to the values you&#x27;re passing to a function based on whether you pass them by pointer, or by reference&#x2F;value. It also prevents a library from changing an argument from const T&amp; to T&amp;, introduce mutations, and break callers as a result.<p>&gt; Exceptions<p>Google&#x27;s code is just setup to std::terminate instead of throwing. It&#x27;s a minor performance gain AFAIK but it also avoids littering the code with try {} catch {} blocks. It forces developers to handle errors instead of propagating them whenever possible too.<p>&gt; “Use lambda expressions where appropriate.” – probably a cheap shot, but what is the alternative? – use them where it’s not appropriate?<p>This point seems counter-productive considering the author claims the guide is written in too much of a prohibitive fashion.<p>&gt; Avoid complicated template programming.<p>TMP is hard, increases compilation times, and is difficult to reason about. The Guide recognizes its usefulness but suggests avoiding it where possible. This is similar to the library writer vs application writers argument: not everyone needs it, avoid it if possible.<p>All in all I think the author just doesn&#x27;t have the same requirements, constraints, and sheer amount of developers&#x2F;lines of code Google has. Nothing is forcing them to use the Guide. In fact, it&#x27;s public but it was written for Google, by Google. It works for Google, and it&#x27;s a great tool in that kind of organization.<p>Disclaimer: I work on Chrome, which has its own guide derived from the GSG.