views:

234

answers:

8

C++ has several types of styles: MFC, Boost, Google, etc. I would like to examine these styles and determine which one is best for my projects, but I want to read from the official style guidebook. Does anyone have an official guide that they typically use?

Here are two that I found. I bet there are more:

Note: This is NOT a discussion about which styleis best..only a call for official style guides that people currently use. Please refrain from bashing other style guides that you don't like.

Side question: Is there a good tool that can examine source code and tell if it matches a given style guide?

+3  A: 

Not a Coding Guideline per se, but I find this mighty useful: Bjarne Stroustrup's C++ Style and Technique FAQ

dirkgently
+1  A: 

I use my own style, which I have written up here. Whether or not you are interested in it as a style, if you're looking at styles in general you may find my discussion on the motivation for it to be useful.

Phil Nash
A: 

To the side question: I don't personally know of any tools that analyze the style in use, but there are tools that reformat source to a given style guide. One that comes to mind is Artistic Style.

greyfade
+5  A: 

There's no such thing as an "official" style guide - the C++ standard is entirely silent on style. One book on the subject by two highly knowledgable C++ guys is C++ Coding Standards by Sutter & Alexandrescu.

anon
A: 

C++ doesn't have and doesn't need an official style. Many organisations impose style guides on their contributors to try to maintain some kind of corporate look and feel; some of these contain snippets of good advice, but many just force you to add strange decorations that seemed like a good idea to someone writing a completely different language in the 1980s.

The only really useful advice you'll find amongst the waffle is:

  • Define a consistent way to distinguish types, objects, and some kinds of function (such as accessors and factories), so you'll know to write (for example) Thing thing = GetThing(); without looking the names up.
  • Don't start names with underscores. This is forbidden in some circumstances, and it's simpler and more readable to not do it at all than to worry about exactly when you can.
  • Spare a thought for the poor chap (perhaps you) who has to read and maintain the code in a few years' time.
  • Keep it simple.
  • Use your brain.
Mike Seymour
+1  A: 

I have also written some tips for good coding in c++: http://www.ivanism.com/Articles/CodingStandards.html

The post starts with:

The goal of coding standards are to increase the business value of the code. The most obvious (and indeed most important) way to do this is to make the code robust and low defect. Equally important, but more subtle goals include reducing coder friction and maintainability. As such, standards should be kept minimal -- simple enough to actually follow, and important enough to remember.

These standards should be used when building new source files. When an existing file needs to be changed, that is an appropriate time to bring it up to standard. However, it's never a good time to edit a file merely to bring it up to standard. If it ain't broke, don't "fix it" and remember to always "Keep it Working".

You'll notice that I don't touch on the classic "Religious" points:

 tabs vs. spaces
 indentation style
 curly brace style
 etc...

Consistency within a file is important and improves readability. But allowing coders to express themselves is also important. So, if you edit a file, either conform to the religion of that file, or convert the whole file to a new, consistent format. If you convert the whole file, you are effectively taking ownership of it, so be prepared to be the go-to person, or leave it as is.

gnavi
A: 

As for the side question, what you need is a static analysis tool. An expensive and huge tool is Klocwork. I've used it at a couple of shops and it can be set up to issue warnings against style issues. I don't recommend it for single users; it is more for a corporate environment. Although they may have stripped down versions for individuals.

Remember to Google for static analysis tools.

Thomas Matthews
+2  A: 

Another style guide are the The JSF air vehicle C++ coding standards.

Ozan