Links to official style guides [closed]

2019-01-21 04:41发布

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 style is 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?

9条回答
手持菜刀,她持情操
2楼-- · 2019-01-21 05:17

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.

查看更多
The star\"
3楼-- · 2019-01-21 05:22

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.
查看更多
我只想做你的唯一
4楼-- · 2019-01-21 05:24

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.

查看更多
等我变得足够好
5楼-- · 2019-01-21 05:26

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.

查看更多
姐就是有狂的资本
6楼-- · 2019-01-21 05:31

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

查看更多
别忘想泡老子
7楼-- · 2019-01-21 05:34

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.

查看更多
登录 后发表回答