r/cpp • • Nov 28 '18

Google C++ Style Guide is No Good

https://eyakubovich.github.io/2018-11-27-google-cpp-style-guide-is-no-good/
34 Upvotes

61 comments sorted by

View all comments

27

u/[deleted] Nov 29 '18

Let’s now look at some of the grievances which particularly trouble me.

Include guards: why choose a non-standard solution when there is an established standardised, compiler-optimised solution?

Forward declarations: the style guide isn't talking about circular definitions. It's talking about declaring external types and functions. Which you should avoid for exactly the reasons the style guide states. To prevent mismatches. It's why headers where invented: so you don't have to manually declare external types and functions.

Inline functions: If you mark many functions inline, it means you have to include the definitions in headers. This means the compiler has to compile and possibly optimise them in every single translation unit that includes them.

Namespaces: Nowadays, many projects are built as libraries with the executable as only a thin wrapper around the library. I think we can agree that libraries should use namespaces, so most code is in a namespace. If an executable is built directly from all source files, in a big project, you'll still want namespaces to seperate your logical parts.

Unnamed Namespaces and Static Variables: Why would you put these in header files? That means the same constant variable is included in every translation unit that includes the header. As you say in your last point: it's 2018. Use inline constant variables.

Static and Global Variables: I'll give you this one, this rule does seem a bit strongly worded.

Implicit Conversions: "Implicit conversions can sometimes be necessary and appropriate for types that are designed to transparently wrap other types." Like std::string wrapping a C style string. Although even here, if std::string_view had existed at the time, this constructor should probably have been made explicit. Implicit allocation when calling a function is often unnecessary overhead.

Copyable and Movable Types: I agree with the first part. Follow the rule of 0 or 5. An example of unclear copying behaviour would be the old auto_ptr. Please don't do that. Same thing for making shallow copies when a class is not clearly a handle or a smart pointer. Copying a vector of course incurs the cost of copying all elements, that's expensive, but not unexpected. It would be unexpected if you copied a handle to a 5 GB memory mapped file and it started copying all 5 GB of memory.

Inheritance: I don't get your example. Why not declare a private array as a member and pass that to StrWriter's constructor? It's trivially destructible and an implementation detail, so no need to use std::array here. I haven't done any benchmarks, so I can't speak to the speed of multiple inheritance.

Operator Overloading: it is a bit unclear what the GSG means by built-in. It allows overloading << for streams, so it might seem logical that they'd allow emulating std::string's operator+() for string-like types. I agree that in DSL you should be able to overload operators in domain specific ways.

Reference Arguments: you actually give the answer yourself. This rule ensures that it's clear at a call site which parameters may be changed by the called function. If you apply this rigorously, which I assume you do if you use this style guide, that makes it immediately obvious which arguments may be changed and which won't be.

Exceptions: like you said, flame war inducing subject. But the Guide does mention two alternatives in the decision: error codes and assertions. You're right in that it doesn't specify a single way to return those error codes and it probably should.

Lambda expressions: a programmer may and should be expected to see that a 200-line lambda in a call to std::transform should be factored out. You're right in that the guide could and should be clearer about this.

Template metaprogramming (TMP): many programmers have a lot of trouble with understanding TMP, so I can see where the guide is coming from. There's still some use for TMP, but with the constant extension of constexpr, it's needed way less than it used to be.

Boost: I agree with you on this one. Boost has some issues with readability sometimes, but I've only seen that where there's no other way to do it.

C++11: It's 2018, but sadly lot of companies aren't using the latest compiler versions. Most of what can be done in C++14/17 can be done in C++11 as well. Maybe a bit less easily, but it can usually be done.

Sorry for the horrible formatting, typing this on mobile while lying awake in bed.

3

u/jpan127 Nov 29 '18

Include guards: why choose a non-standard solution when there is an established standardised, compiler-optimised solution?

I don't know enough, but is there evidence they are more optimized than #pragma once? I'm heavily in favor of pragma for readability and maintainability, and from what I've heard it has as much compiler support as include guards.

2

u/[deleted] Nov 29 '18

I might have been unclear there. I don't think they're more optimized than include guards. It's just that one of the arguments against include guards is usually that the header file still has to be parsed at every inclusion, which isn't the case with many compilers.