Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Google C++ style guide (googlecode.com)
39 points by degio on April 10, 2014 | hide | past | favorite | 54 comments


The C++ you end up with by following Google's style guide is more of a "C with classes" thing, they prohibit several central features of C++. That said, I understand that they mostly do this to be consistent with their older code bases.

But it's not exactly a style guide I'd follow strictly in a new project/team.


The Google style guide could help explain why the Go team thought that their language would make more of an impact in C++ land than it actually did.


Nope. Go was born because large C++ builds were just too darn slow, but that wasn't due to using a restricted subset of C++. In fact, using a subset of C++ speeds the build up.


You're right on the money. This passage is particularly damning:

> Rvalue references encourage a programming style that makes heavier use of value semantics. This style is unfamiliar to many developers, and its performance characteristics can be hard to reason about.

It's like, do you know C++ or don't you? Value semantics are C++.


They are right that it's harder to reason about value semantics. The cost of passing a pointer is always the same whereas the cost of passing a value isn't.

But I don't think that's a good enough reason not to prefer values considering the performance and memory usage drawbacks of shared_ptr. unique_ptr is often the best solution in my view.


Actually they do prohibit those features that arguably cause more damage than benefit.

c++ exceptions? in an unmanaged (runtime engine not managed by some sort of VM) language you can't really assume anything when an exception occurs, you can't assume that you will be able to allocate more memory from the heap, for instance - if the heap is corrupted then this will just not work.

RTTI ? it costs a lot, well and you can do without it; Besides requirement of RTTI (assuming that the library does RTTI) will prevent you from using third party libraries that might be of use. Resolution: skip.


It's a totally reasonable style guide to adopt with a new project / team. And in fact it's what I chose the last time I started a team doing systems programming. It's well reasoned, and hits my personal sweet spot for the power vs. complexity tradeoff.

C++ has a pretty bad reputation. Possibly deservedly. So unless everyone has already bought fully into C++, the full language is a very hard sell. But C with a few extra conveniences and safety features (strings, unique_ptr, some limited polymorphism) is a much easier one.


> Functions should start with a capital letter and have a capital letter for each new word. No underscores.

Why so? It's Java-ish. Java favors camel notation for some historic reason. C++ on the other hand favors snake notation which is clearly reflected in stdc++. Snake notation is also easier to read, especially if the name contains many words in it. Of course in the end it's a matter of preference, I just wonder why Google mandates camel notation.


This is actually the main advantage of having detailed coding style guide. Teams don't need to discuss minor details like this over and over again, and can focus on higher level issues. Style rules can be sometimes arbitrary because there is no clear best approach, but it is important that such arbitrary choices are made once, documented, and consistently enforced.


>Snake notation is also easier to read, especially if the name contains many words in it. Of course in the end it's a matter of preference.

Exactly. It's matter of preference. I can read TextLikeThis much easier than text_like_this. Granted I've been developing in C# for past few years so that's likely the reason.


I don't have a strong opinion either way (I probably like snake case more, but use camel/pascal case basically everywhere) but this doesn't work as well when acronyms are involved. e.g. is it XmlParser or XMLParser? I'd say the latter, but someone else might disagree.

I think there are more ambiguous examples, but cant think of them.


> is it XmlParser or XMLParser?

MS standards says that if shortened letter combination is longer than 2 words than it's lowered, otherwise everything is uppercase.

e.g. XmlParser, IOStream

But again, everyone can create their own standard and be happy. The only problem occurs when you work with 10 people and everyone is sticking to his own style.


Not every API is that consistent. JavaScript's `XMLHttpRequest`, for example...


It's a matter of preference, however it's not a subjective thing that reading the text without spacing between the words is slower. There are tests which measure such speed.


For those who are unaware, `clang-format` (http://clang.llvm.org/docs/ClangFormat.html) can be used to automatically convert existing C++ code to a given formatting style (Google, LLVM, a custom style, etc).

It's similar to `gofmt` in the golang world, and is a really useful tool for enforcing these kind of style guides.


No lambdas allowed? For many other things they say "use within reason", I don't see why they can't say this for lambdas. They're finally making the stl algorithms easily usable. You don't have to write Functors that are in a different place then called.

Also, the guide seems to completely miss templates and actually pretty much allow everything in C++ (except lambdas and exceptions). I don't really see the point then.


> They're finally making the stl algorithms easily usable.

Well, considering they ban use of exceptions and considering that stl does use exceptions and therefore they cannot use stl, I don't think that they really care.


I suspect they either use an STL that has a way to disable exceptions (such as STLPort) or they disable exceptions using compiler flags, or maybe both.

You may argue that this really brings into question the "S" part of "STL" but it is done in practice and I'd argue it's not even all that difficult to deal with.


According to this StackOverflow reply [1], they do use the STL, but don't catch the (very rare) exceptions.

[1] https://stackoverflow.com/a/15564254


This is an older revision of the style guide; they updated the style guide recently to allow lambdas. Other C++11 features were also under review for a while.


They don't even want you to use rvalue references.


There's a few style guides up the path for those interested: http://google-styleguide.googlecode.com/svn/trunk/


As an x googler I had my issues with it

http://games.greggman.com/game/lets-debate-coding-style/


"Use only spaces, and indent 2 spaces at a time." Of course you realize, this means war.

Much like all other style guides I've come across, there are a lot of hints that I'll find helpful and encourage at our next meetup, but honestly, the only style guide that truly matters is the one you can all agree to in your team and makes sense in your context.

And, I'm not sure if this is just those who share the same water cooler, many C++ devs seem to dislike using STL. I can't figure out why.


The big reasons for avoiding the STL are (in my experience)

1. Holdover from when STL implementations were buggy

2. Holdover from C

3. Avoiding exceptions it might throw if you compile with exceptions off

4. Many STLs are slow and unoptimized.

5. Many parts of the STL are slow, no matter which STL you use

6. Poor allocator support (better, not perfect in c++11)

7. You need guarantees that the STL doesnt provide.

there are more, but those are the big ones.


It doesn't get any worse than that guide (except maybe EC++ or the FQA). No RAII, no class invariants, no value semantics, no streams, no operators (which means no custom Iterators, EqualityComparable or even CopyAssignable types! Such a basic feature called "insidious"). Not even mentioning the FUD about newer C++ features.


I love C++, I wish there was more work available that used it. EDIT: Clearly my issue is where I lived, not the lack of available jobs apparently.


I don't really see a lot to like about C++. It's huge and fiendishly complex, keywords are overloaded to incomprehensibility, it doesn't make satisfactory guarantees about static initialization, exception safety is too hard to reason about, it's full of features maintained for backwards compatibility even when they no longer make sense, and development tools are unacceptably inferior to Java and C#.

Pros are that it performs excellently and const correctness is pretty cool.

Let's get an old fashioned debate started!


>It's huge and fiendishly complex

It's used to write software where that complexity is a minor cost in return for the performance it provides.

>keywords are overloaded to incomprehensibility

I don't know of a single keyword that is incomprehensible. Only a few keywords are overloaded but they are overloaded in contexts that make it very very clear what they're being used for.

>doesn't make satisfactory guarantees about static initialization

It makes very strong guarantees about static initialization. Static variables declared within a function are initialized when control flow passes through it for the first time, and such initialization is guaranteed to be thread safe. Global variables have three phases to their initialization, zero initialization, static initialization, and then dynamic initialization. Global variables are also initialized in the order that they are defined within a translation unit.

> exception safety is too hard to reason about

Exception safety is easier to reason about in C++ than any other language thanks to RAII.

>it's full of features maintained for backwards compatibility even when they no longer make sense

They make sense because no one wants to rewrite the billions of lines of code that their project might depend on that uses those features. Instead, it makes a lot more sense to just avoid using deprecated or backward compatible features rather than remove them from the language and break a crap load of invaluable software in the process.

>development tools are unacceptably inferior to Java and C#.

This is true.


> It's used to write software where that complexity is a minor cost in return for the performance it provides.

Other languages (C, D, Ada, Go) provide similar native performance with far less complexity.

> I don't know of a single keyword that is incomprehensible. Only a few keywords are overloaded but they are overloaded in contexts that make it very very clear what they're being used for.

static, virtual, and class are all overloaded in weird ways. But punctuation is the worst: in particular commas, colons, ampersands, and asterisks are overused to absurdity. I'd almost prefer APL-like extra symbols to disambiguate some of the insane template one-liners that I've seen.

> It makes very strong guarantees about static initialization.

Are you saying that the static initialization order fiasco doesn't exist? C# and Java do not suffer from this problem.

> Exception safety is easier to reason about in C++ than any other language thanks to RAII.

I don't even know what to say to this one. Exceptions are notoriously wonky in C++. The language makes you resort to idioms like RAII and copy-swap to use them at all, instead of providing language-level support to solve those problems.

> They make sense because no one wants to rewrite the billions of lines of code that their project might depend on that uses those features. Instead, it makes a lot more sense to just avoid using deprecated or backward compatible features rather than remove them from the language and break a crap load of invaluable software in the process.

And as a consequence the language suffers. Historical reasons are not a valid defense of the present design of the language.

I am reminded of this esr quote:

"While we do not intend to insult the designers of C++, we will not make excuses for them either. They repeatedly made design choices that were well-intentioned, understandable in context, and wrong."


Finance /HFT. In my last job I worked for a company that sells an algo trading platform written in C++.


Still the language of choice in the world of music production software. This is not a place you'll make a lot of money but the work is far more interesting than the usual CRUD slapdash you do in most dev shops.


HPC, games, portable mobile code and embedded systems.


Indeed, I don't understand that statement either.


Especially remote jobs, outside US.


Games programmer!


Yeah, this is true. But all places pretty much require you have prior experience. Not to mention most games these days are in Obj-c/Java. Plus game studios aren't exactly ubiquitous (least not where I'm from).


> Not to mention most games these days are in Obj-c/Java.

Proof? I've seen _quite_ the opposite. Sure, these things (and Flash, still) are popular among small/indie devs, but I haven't heard about a single AAA title since ~1995 that didn't used C++.

There is an insane amount of big open source projects out there that use C++. Firefox, Chromium, KDE, .... the list is much longer. Making significant contributions to any of those would most likely be considered experience.

And there are tons of C++ jobs outside the game industry. Not for small-scale web stuff of course, but for client-side stuff, and really huge backends, C++ is still very popular.


Speaking about remote C++ jobs - I sent an email to you more than two months ago, for the C++ dev position at Eyeo, which they still advertise on their jobs site. I sent again after a couple of weeks. No reply. Could you please confirm that this job is still active?


No proof really. My last job hunt ( Nov of last year ) just came up 100% not-C++, currently doing Java work. Same thing happened a few years ago...maybe I just lived in a lame city though.


db programmer


Looking at this style guide as a C++ programmer, I'd hate to work at Google.


FYI, the lint tool referenced in the guide is found here:

http://google-styleguide.googlecode.com/svn/trunk/cpplint/


It might sound like a good idea to precisely describe how code should be written, until you realize it's inefficient and useless.

Code coherence can be accomplished with more relaxed rules if you delegate more responsibility to your engineers.


> Code coherence can be accomplished with more relaxed rules if you delegate more responsibility to your engineers.

Please elaborate.


I think consensus through code review is better as compilers and languages evolve. If you write rules for everything you spend your time checking for compliance and perhaps rewriting rules instead of relying on a more organic code quality assurance.

You also want your engineers to think by themselves instead of hiding behind a rules book.


Code style doesn't dictate what your engineers build, it just makes their code easier to review and maintain. Code smells, foreign conventions, and inconsistency all make reviewing and editing code harder.


Some coding style guides amount to an obsessive compulsive disorder. They focus on trivialities, like an extra space between parameters or the placement of braces around blocks of code.


"Vigorous programming is concise. A method should contain no unnecessary statements, a class no unnecessary methods, for the same reason that a drawing should have no unnecessary lines and a machine no unnecessary parts. This requires not that the programmer make all his methods short, or that he avoid all detail and treat his classes only in design, but that he make every statement tell."

—String and Wire (Elements of Modern Programming Style)


C++ Guide: We do not use C++ exceptions.

Python Guide: Exceptions are allowed but must be used carefully.

C++ Guide: Do not use lambda expressions, std::function or std::bind.

Python Guide: Okay for one-liners.


I've encountered a lot of c++ programmers who seem to dislike a lot of c++; even refusing to use the STL. Perhaps it's because c++ and Python have different cultural backgrounds: a lot of early c++ programmers came over grudgingly from c and continued to program in a c-style.


Does Google's 80 character limit include the identification sequence?


You'd think that with their mountains of cash Google could afford to replace their engineers' 80-character monochrome terminals.


Actually you find that many engineers with 30" monitors end up using multiple side-by-side terminal windows, each set to 80 chars.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: