29

Right now I am using Google C++ Style Guide in my C++ code and I was pretty happy with it.

Recently I was told that this guide is very bad: it is used internally by Google (I knew that), is outdated, and promotes some very bad practices. So I want to use another coding style.

What good and fairly used C++ style guides are there? I write code for both gcc and Visual Studio, and I use a lot of the C++11 features.

What I liked very much about Google C++ Style Guide was the indentation, the whitespace and the naming conventions (specially naming all classes, types - including typedefs, type aliases and template aliases - with capital first letter).

I know any answer is subjective (I hope this is ok on this site) and I would appreciate any opinion, but I am interested which guides are used these days.

  • 1
    You can always use what ever style you like, and then reformat it to the preferred style when you have to share it. Here is a style formatter that automates this astyle.sourceforge.net – Reactgular Jan 7 '14 at 0:23
  • 7
    This question does have potential for opinion based answers. Rather than closing it for this reason, I would encourage answerers to focus on facts, such as known uses, recommendations by authorities, comparative studies and the like. – andy256 Jan 7 '14 at 0:23
  • @andy256 Couldn't have said it better myself. – bolov Jan 7 '14 at 0:28
  • 7
    Who told you Google's style was bad, and why does their opinion matter to you? – Reactgular Jan 7 '14 at 0:39
  • @MathewFoscarini there was a reasonably recent discussion here, even though it wasn't too in-depth: chat.stackoverflow.com/rooms/10/conversation/… (but then, going through it in-depth is like going through the FQA) – Cubbi Jan 7 '14 at 4:47
15

You can use the guideline from this book for general usage:

http://www.amazon.com/Coding-Standards-Rules-Guidelines-Practices/dp/0321113586

from Herb Sutter and Andrei Alexandrescu. It does not take into account C++11 though, but I think there will be a new edition.

But it will not answer about number of space instead of tabs or what kind of esoteric notation you want to enforce. But these are not the most important thing, most of the time just to have some consistency is the key.

22

The C++ Core Guidelines are a set of tried-and-true guidelines, rules, and best practices about coding in C++, you can find them here: https://github.com/isocpp/CppCoreGuidelines

They are written by, among the others, Bjarne Stroustrup and Herb Sutter.

  • 1
    Definitely better than Nikko's solution – whn Apr 4 '18 at 17:12
8

The criticisms of Google's C++ style guide (and I agree some are justified) are not about Google's naming conventions or indentation style but rather about some of their other rules and policies. Indentation / formatting and naming conventions are both something of a matter of taste and are fertile ground for endless programmer religious wars but in C++ where unlike say C# there is no universal standard the only really important guideline is consistency. For a new project, pick a naming convention and indentation style you like and use it consistently. For an existing project, stick with the convention already in use. Rule 0 in C++ Coding Standards is "Don't sweat the small stuff." where they argue that naming conventions and indentation style are just not that important as long as you're consistent.

A big productivity booster for me has been automating indentation / formatting using clang-format. Once you've settled on some indentation and formatting rules, I highly recommend setting up a customized .clang-format configuration file and then never having to worry about it again :-)

Clang-format is a standalone tool and doesn't require you to be using clang as a compiler. There's even an official plugin available for Visual Studio.

6

As @user113896 earlier wrote, Bjarne Strostrup gave us a lot of style guidance. One of his fine achievements is JSF-C++ Coding style book. Beware, it's not for regular c++, more for embedded use, but it shows how things should be done to be clear and functional. Of course - You don't have to take everything into account - its a guide, not an order-book :).

2

If you haven't read Bjarne Stroustrup's Programming and Principles I strongly recommend you to read it because you can learn a lot from the language's creator. I have watched and read a lot from the C++ author and I can say that he has this fixation for style and is always telling people to write good, beautiful code that according to him is easier to debug and read. I believe that he is the greatest tutor and the best guide to writing stylish and beautiful C++ code.

Not the answer you're looking for? Browse other questions tagged or ask your own question.