
Google C++ Style Guide - olalonde
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml
======
winestock
This thread is about C++. Therefore, part of this thread must be dedicated to
rants about C++. This may be an Internet law.

Thus, here's the C++ Frequently Questioned Answers List:

<http://yosefk.com/c++fqa/fqa.html>

------
joedavis512
I laughed when I saw this: "If your function crashes upon error, you should
append OrDie to the function name.", and then as an example: "OpenFileOrDie".
Lol.

~~~
xentronium
I thought it was an old perl convention.

    
    
        do_something or die "Somebody set up us the bomb"

~~~
mahmud
What if that was an actual semantic transformation in the language? Naming
something XorDie would execute the body of X in a context within which all
errors are caught, and if any is found, the call returns NIL or similar?

Easily doable with handler-case, unwind-protect and reader macros, but ugly as
sin.

~~~
rudiger
We do the same thing with getX and setX; it would be interesting to see more
"meaningful" function names in a language.

------
yuvadam
Oh, C++. I do not like that language.

I have always felt that C++ is a language too complicated in a time where
keeping it simple is key. The fact that it needs a style guide _that long_
says it all.

~~~
steve918
People love to hate on C++, but if your going to, at least hate on things that
are actually broken.

For instance every non-trivial language with more than a couple of programmers
working with it will have some style (maybe less formal) style guide. Python
is often hailed as 'executable sudo-code' yet the community has a style guide,
pep8. (Interestingly Google has their own style guide for Python.)

There are plenty of valid reasons to hate on C++, but being a cowboy coder
isn't really a valid one.

~~~
kinetik
Pseudo.

------
adbge
Browsing through the rest of the linked SVN, Google seems to have an Emacs
mode to help encourage adherence to the style guide:

[http://google-styleguide.googlecode.com/svn/trunk/google-
c-s...](http://google-styleguide.googlecode.com/svn/trunk/google-c-style.el)

------
grimlck
'Each line of text in your code should be at most 80 characters long. ' We
recognize that this rule is controversial, but so much existing code already
adheres to it, and we feel that consistency is important.

That's another funny one...

~~~
daniel02216
'Existing code' refers to Google's codebase, not necessarily the rest of the
world. Then they can do things like have their version control diff apps like
Mondrian/Rietveld have fixed-width UIs that fit on one page, when they know
that everything will fit within 80 chars.

~~~
nostrademons
Also, many Google developers do a big chunk of their development through SSH
windows on a laptop working from home. Laptops have limited screen real estate
anyway, and it's really handy to be able to use the default PuTTY terminal
size and not have to screw around with resizing it.

~~~
vilya
It also makes life easier if you like to have several code windows open side
by side while you work. I got introduced to this approach a few years ago by a
colleague and I've loved it ever since (I actually use vertical splits in Vim
FWIW; same difference...).

------
eiji
Do they enforce the style guide on VCS checkin? Or is anyone of you enforced
to do so?

(FYI: I'm talking about VCS-guards, who prevent checkin's, if they do not
comply with a style guide. Maybe someone can run a poll on that topic.)

~~~
WildUtah
No, Google requires frequent code review. Coders told me when I visited that
code which violates standards or raises any kind of compiler warning needs to
be discussed with your team before it's committed and will be identified on
review. (or nightly build)

Automated rules built into VCS seemed to be the kind of thing nobody there
would tolerate.

------
_delirium
Huh, I don't think I've seen this constant naming convention before:

    
    
       const int kDaysInAWeek = 7;

~~~
jparise
We used that C++ constant naming convention at EA, too. I like it because it
nicely differentiates C++ typed constants from preprocessor constants and
macros (which we SPELLED_THUSLY).

~~~
_delirium
Yeah, it appears it's actually quite common, and I must be one of the last
people to be surprised by it. =] I've never been part of a major multi-person
C++ project, though, so my stylistic conventions are a mixture of C
conventions and the conventions used in the STL. I tend to SPELL_THUSLY even
C++ typed constants, though I have also seen them PascalCased (but then,
unless context makes it sufficiently clear, they can look like class names).

------
atuladhar
Does Google have style guides for other languages? Are any of them public?

~~~
DTrejo
Javascript: [http://google-
styleguide.googlecode.com/svn/trunk/javascript...](http://google-
styleguide.googlecode.com/svn/trunk/javascriptguide.xml)

Full list: <http://google-styleguide.googlecode.com/svn/trunk/>

------
georgieporgie
I'm curious why everyone who writes style guides seems to hate C++ exceptions.

~~~
bodyfour
One thing that I dislike is that it tries to be overly generic, i.e.

    
    
       try { throw 5; } catch (const int& val) { printf("%d\n", val); }
       try { throw "hello_world"; } catch (const char *&val) { puts(val); }
    

I think it would have been much better (and simpler for implementers!) if they
had made std::exception a more fundamental part of the language and required
that throw be used on a descendant of it. As it stands if you want to be sure
to catch any failure from some external code you end up doing:

    
    
      try {
        something();
      } catch (const std::exception& ex) {
        fprintf(stderr, "ERROR: %s\n", ex.what());
      } catch (...) {
        fputs("UNKNOWN ERROR\n", stderr);
      }
    

My personal opinion is that it's easier to deal with C++ exceptions (warts and
all) than to try to avoid them. Just about any method that mutates a STL type
can have a heap allocation fail and the only reasonable way to report that is
via "throw std::bad_alloc();". Just make sure that your code always follows
the principals of RAII ( <http://en.wikipedia.org/wiki/RAII> ) and you'll be
fine.

~~~
joe_the_user
Yes, throwing an arbitrary object as an exception is a humorously ridiculous
approach.

It's as if every time a machine broke down in a factory, the workmen would
have to wheel the machine into the manager's office, shove it on his desk and
shout, "there, there's your problem!"... and if the manager didn't grab the
machine, it would fly through his back window...

