Links to official style guides [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
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:
http://google-styleguide.googlecode.com/svn/trunk/cppguide.html
http://www.boost.org/development/requirements.html
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?

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

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.

Other answers state that there is no official style guide, which was true at the time.
But in 2015, Bjarne Stroustrup announced the C++ Core Guidelines, an open source project to build authoritative guidelines for modern C++ code, led by Stroustrup himself and published by the Standard C++ Foundation:
http://github.com/isocpp/CppCoreGuidelines
In relevance to this question, the Core Guidelines also link to other guidelines, with added comments. About the often recommended Google C++ Style Guide, they say:
Geared toward C++03 and (also) older code bases. Google experts are now actively collaborating here on helping to improve these Guidelines, and hopefully to merge efforts so these can be a modern common set they could also recommend.

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.

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.

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

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.

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.

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.

Related

Allow ONLY c++ 11 coding features and style [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Is there a way to enforce the use of modern c++ 11 features from the compiler?
We have to start a new project and stay in C++ will benefit us from the use of the our existing tools and libraries, but what is currently hurting us to the death is the abuse of C style coding, raw pointers, C style vectors and all the dangerous C features that are so error prone or produce errors that are really hard to track, you know what i mean.
I read a note about what is bad in C++ today (c++11 and beyond) is the c++ past and the abuse of low level/old features when is not necessary.
I really would like to enforce from the compiler or with other tool the use of c++ 11 features like smart pointers, containers, reference parameters, c++ standard library and not C libraries, etc.
Thank you very much.
EDIT: I thought it is not necessary to clarify the use of code review. etc. I just ask if there is or not a tool/mechanism that filters creating features, or enforce modern safer features/coding practices. All the lose ends we will manage or figure out later. Thank you very much!
You might want to take a look at clang-modernize, it can help with some of what you're looking for. In addition running static analysis to help catch bad practices could help identify areas of your codebase in particular need of some attention / modernization. There are a number of such tools associated with clang and MSVC also has its own static analysis if you're working with Visual Studio.
The only way to ensure c++11 features like auto variable declarations, using lambda definitions, range based for loops, etc. are used preferred to older techniques are human based code reviews.
Usage of deprecated stuff, like usage of std::auto_ptr may be sorted out by warnings already.
But enforcing new way styles might become hard, without having a 2nd pair of experienced eyes looking at the code.
I could think of instrumenting an appropriate static code analysis tool, to detect old style patterns and propose the c++11 standard way to do it, but that's a way too broad topic, and depends on the static analysis tool used.
At the risk of getting downvoted to oblvion.
So you or your "expert" on C++ programming, want to ban certain practices.
Because you know they are bad.
One such practice is using raw pointers. Shame that there are C++ programmers who not only use pointers very often, but think they are a good thing thing to use. Guys like Herb Sutter, who in a recent talk ( CppCon I think ) said exactly that.
Another practice you mention is using C style vectors. Not a problem since C does not have vectors.
Those are really the only two that you mention. But you do talk about banning features that cause errors. That can easily be achieved. Instead of issuing your programmers computers. Issue them etch-a sketch's.
Other then that, I would say that your company should hire people who are real experts, not some guy who read a Herb Schildt book.
PS: I was trying to remember what your post smelled like, now I remember. It smells like one of those deign doc review, where one guy spend half the time arguing about spelling errors in the design docs.

Your thoughts on "Large Scale C++ Software Design" [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
Reading the reviews at Amazon and ACCU suggests that John Lakos' book, Large-Scale C++ Software Design may be the Rosetta Stone for modularization.
At the same time, the book seems to be really rare: not many have ever read it, and no pirate electronic copies are floating around.
So, what do you think?
I've read it, and consider it a very useful book on some practical issues with large C++ projects. If you have already read a lot about C++, and know a bit about physical design and its implications, you may not find that much which is terribly "new" in this book.
On the other hand, if your build takes 4 hours, and you don't know how to whittle it down, get a copy, read it, and take it all in.
You'll start writing physically better code quite quickly.
[Edit]
If you want to start somewhere, and can't immediately get a hold of the book, I found the Games From Within series on physical structure useful even after reading Large Scale C++ design.
Interestingly, "More C++ Gems" contains a shortened (to 88(!) pages) version of Lakos' book, which can also be browsed (fully, I believe, as it belongs to the first half of the book) online at Google books.
So, enjoy everyone interested:)
Lakos used to work for Mentor Graphics, who makes EDA software. He was involved in building EDA software in C++, where they wanted to use C++ efficiently ('no more than 5% overhead over C code') and sort out how to build software on early workstations that really would take a long time to compile a large C++ application.
The book is quite a good read - it discusses a wide variety of topics, and Lakos really did know his stuff. He really comes from a background of having to get efficient code out of a C++ compiler, as well as how to set up a C++ program so it is maintainable and compiles and links reasonably quickly.
I think that Lakos has a lot of insight, and I would recommend that you read it. However, it is 'trad' C++ from a time before features like templates were widely available. My copy is not for sale ;^)
I thought the book was a good read back in the late 1990s. It was out of date back then, now about as relevant as telephone book from the 1950s.
I worked with Lakos for several years where he tried to implement these ideas. I also had my modern C++ project I built of about 2000 source files-- large scale enough, and I've built a few more since. Build times are easily reduced by parallel distributed builds, using programs like icecream. Every complier will cache opened headers-- only by doing something really outrageous any modern build tool like scons will scale to thousands of translation units without doing anything really special, with build times, including tests, takes a couple minutes from clean.
Limiting your libraries interface to a few "vocabulary types" (not the same sence as the OO concept, he means just use Int, float, string, char, and a couple of other I don't recall) is extremely poor advice to scale anything. Your build is going to be looking for a type mismatch, you don't want this at runtime just to make it easier to write a make file.
And that's largely the gist of the book-- how do I write C++ in order that it works with tools from 1993? Additionally, the Mentor Graphic project the book describes was a disaster from all accounts. Even the book itself alludes to that.
Large Scale C++ is delivered in source code form-- these are the "physical dependencies," not link libraries (and the book never mentions other more important dependencies like databases, sockets, processor architecture, etc.).
The book is full of ideas that sound good, but there are much better ways to scale up in practice. If you want to study large C++ libraries, take a look at Boost or Xerces and see what they did. They really are implementing large projects, not writing about them.
It's a little out of date (in fact it was out of date when it was written). If I recall correctly, a lot of it was about reducing dependencies which you would probably do now with templates.
Although that's probably one of the lessons to learn on large scale projects, you aren't usually using cutting-edge features and tools.
Is “Large-Scale C++ Software Design” by John Lakos a mind-provoking (but usually overlooked) gem?
Yes.
It is an excellent book and an important one from a historical point of view.
Note that to implement the practices described in the book, you need considerable discipline and agreement within the team. Here are a few things to note:
Lakos has precise definitions for what he calls "components" and "packages". These are key for large scale design. However, they require adherence to conventions for how source and header files are named and the sequence in which they are included. It is a shame that most people (including those who quote Lakos) rarely follow these conventions.
The book is all about C++ but the concepts are more widely applicable. However, because C++ is such a complex language, a large part of the book is teaching you how to use C++ effectively. If you can get past that, you can actually find it useful even if you use other languages.
Some of the prescriptions such as on the use of "name spaces" maybe considered controversial now. Many believe that name spaces should be used in a limited way in C++.
Yes, in some ways, the book is a little dated and some of it is C++ specific. Nonetheless, it offers a lot of useful advice about dealing with complexity and coupling in large programs, and most of his advice applies to any object-oriented language. I also found it very readable.
If you can track down a copy, I'm sure you'll find it worth reading. It's in my top ten list of programming books.
I read it a long time ago, but I remember getting quite a lot from it; though as MadKeithV points out, that may just have been that I knew less then ;)
Certainly from a "how do I reduce how long this takes to build" perspective it's has lots of useful stuff, especially around reducing compile time dependencies, though possibly some of it is no longer relevant, or even possible, given how much templates are used these days.
And no, you can't have my copy either :)

Practical and advanced C++ usage

I've studied C++ as a college course. And I have been working on it for last three years. So I kinda have a fairly good idea what is it about. But I believe to know a language is quite different from using it to full potential. My current job doesn't allow me to explore much.
I have seen you guys suggest studying a open source project and perhaps contributing to one too. So my question is, can you suggest one(of both?) to start with that is simple and doesn't overwhelm a starter.
I was in the same situation 12 years ago when I got my first programming job out of college. I didn't do any open source but I managed to gain a lot of practical and advanced C++ knowledge by reading books (the dead tree kind).
In particular, there was an excellent series by Scott Meyers which I feel helped the most in turning me from newbie to professional:
Effective C++: 55 Specific Ways to Improve Your Programs and Designs
More Effective C++: 35 New Ways to Improve Your Programs and Designs
Effective STL: 50 Specific Ways to Improve Your Use of the Standard Template Library
The topics in these books range from beginner to advanced. It took me about 2 years working in C++ to understand every chapter in these books, so don't be disheartened if it goes over your head at some point... just try reading it again later :)
If you're willing to climb the very steep learning curve, I would think that you could do worse than studying parts of the boost libraries. I don't think there is another similar body of C++ code out there that is pushing the boundaries of what can be done in C++.
re project selection: you'll want to hack on something that has some relevance to you, otherwise it's just unpaid work. i might suggest eg. Pure, but will you find enough interest?
re advanced code: see Boost
Obviously learning something like this does not happen overnight; You can study existing open-source projects like other posters suggested. However, I have one more suggestion: I suggest joining one of the big IRC c++ channels (for example #c++ on efnet or freenode). Of course most of the questions / discussions going there are basic; however quite often interesting discussions and questions come up. Over the several years I've learned quite a bit just by participating in channel discussions.
My main advice would be that you do not need to stick to C++ to learn technique.. Study Erlang, Lua, and Haskell (lots of others). You will learn so much from these language that you can apply in C++.
Google protocol buffers is a very well written C++ application that sticks to the features of C++ that are proven in the industry -- i.e., they don't go over the top with features that cause headaches for using the code.
In terms of learning to use C++ to its full potential, well the full potential is often hidden. There are very few exceptional C++ programmers, even in the open-source community. So I suggest you read some books. Like the books from Scot Myers and Herb Sutter (all 6 of em).
Boost is an amazing library and has a lot of good code, ranging from simple techniques to very advanced techniques. It definitely touches all of the bleeding edge of C++ idioms. The problem with boost is, it doesn't work well with modern IDE features (code-completion being the main problem), and it has a LOT of symbols :D. It is the place to learn about generic programming and template meta-programming, the former represents the most advanced and immediately-usable idioms, and the latter you can skip for the immediate future.
Apart from the Scott Meyers books DSO has already suggested: Participate on SO and try to answer other people's C++ questions. If you don't know the answer, wait a little until some good answers are posted. You will probably get some idea where the gaps in your knowledge are and you could do some more reading to fill those afterwards.

Where can I find good C++ source code? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I am learning C++ as a first language. I feel like I am about to hit a ceiling on my learning (I am not learning through a class) if I don't start looking at actual code soon. Here are my two main questions:
Where can I find source code
What is a good litmus test on code's quality (I've obviously never developed in a work environment)
I hope this is relevant to SO, but I can see the need to close this. Thanks for the help.
Related:
Examples of "modern C++" in action?
I would recommend Boost. Using Boost will simplify your program design. Reading Boost source code can show you how to use C++ to solve some challenging problems in a concise way.
This add on library is itself written in C++, in a peer-reviewed fashion, and has a high standard of quality.
I think your two best bets for finding C++ code are to go to the popuplar open source repositories.
CodePlex: http://codeplex.com
Google Code: http://code.google.com
SourceForge: http://sourceforge.net/
These all have high quality C++ projects you can take a look at. I don't think there's a great metric for judging quality on a large scale. I would start with the more popular projects which may be more likely to have quality code.
The List:
SourceForge: http://sourceforge.net/
Boost: http://www.boost.org/
CodePlex: http://www.codeplex.com/
Google Code: http://code.google.com/
Google Code University: http://code.google.com/edu/
koders.com: http://www.koders.com/
The net is chock-full of open-source C++ code. Why not pick a few such projects, and, even better, start helping out of them? No better way of learning than by doing!
I would recommend getting a good book, which will be packed full of source code examples!
C++ in a Nutshell
You can also search open source code at www.koders.com
I think you got some good answers already, I would like to add this suggestions for picking a project from one of the open source project repositories: Pick a widely used but preferably smaller project that has been around for a while and targets a domain that you are specifically interested in. That way you will be able to get a better idea of production ready code and be able to learn something about that domain.
I found the source code and documentation of POCO are quite readable, and
unlike some other open source projects that focus on handling one specific problem, say GUI or Logging, this library focuses on developing a complete application, thus covering a quite broad area(file system, text processing, networking, logging etc ).
it uses modern C++ idioms. So by reading the implementation you can learn modern C++ skills as well.
I would recommend OpenSG
It is an interesting topic, it uses concurrency modeling, networking, includes links to scientific papers, is well documented, uses real c++ not c with objects stuff and almost all subparadigms and doesn't overuse them, is easily accessible AND who would have guessed... I am a fan of it ;)
OpenSG - Home
C++ is a great language, but kind of heavy as a first language. Try python.
1) Where can I find source code
Reading code is harder than writing it. This is especially true of large, complex languages like C++. Without already knowing the intricacies of the language, you don't stand much chance of getting knowledge from the complex code others write in production. You're going to have to learn the very smallest parts first, on your own by writing it. As you learn c++, you will also learn programming.
2) What is a good litmus test on code's quality
There isn't one. That's not going to be an easy thing to learn, either. It comes from experience. But really, the way you know the good code from the bad, is that after you've had some time to familiarize yourself with the layout of a project, you can understand what any given piece does, after you look at it. Readable code has quality, whereas confusing code falls short.
Looking at other peoples code is a hard way to learn the basics. Find a tutorial on the net and get your feet wet that way. I'm sure there are many, fine printed books on the subject as well.
As you go, and get stuck or confused or lost, post questions here.
Code Project is the best place for source code.

Existing Standard Style and Coding standard documents [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
The following have been proposed for an upcoming C++ project.
C++ Coding Standards, by Sutter and Alexandrescu
JSF Air Vehicle C++ coding standards
The Elements of C++ Style
Effective C++ 3rd Edition, by Scott Meyers
Are there other choices? Or is the list above what be should used on a C++ project?
Some related links
Do you think a software company should impose developers a coding-style?
https://stackoverflow.com/questions/66268/what-is-the-best-cc-coding-style-closed
C++ Coding Standards: 101 Rules, Guidelines, and Best Practices (C++ In-Depth Series)
by Herb Sutter and, Andrei Alexandrescu.
I really think it does not matter which one you adopt, as long as everyone goes along with it. Sometimes that can be hard as it seems that some styles don't agree with peoples tases. I.e. it comes down to arguing about whether prefixing all member variable with m_ is pretty or not.
I have been using and modifying the Geosoft standards for a while, these are for C++. There are some other at the what-is-your-favorite-coding-guidelines-checklist thread
Hmm, strange question. Just choose standard which most of the team members are familiar with. Make some kind of poll for your team. Not sure how SO can help here :)
High Integrity C++ Coding Standard Manual - Version 2.4
Try this one, it's the one that NASA's Goddard space flight centre uses.
http://software.gsfc.nasa.gov/AssetsApproved/PA2.4.1.3.pdf
I've written a coding standard for a major British company and was very conscious of putting reasons why I selected certain things rather than just make it a bunch of "Thou shalt" pronouncements. (-:
As a quick way out, I'd suggest mandating:
Scott Meyers's Effective C++ 3rd Edition (Amazon link) - if you can find a copy of the 1st edition of this book then buy it for the overview of OO design which was removed from later editions. )-:
Scott Meyer's book Effective STL (Amazon link) - you must use STL to use C++ efficiently.
Steve McConnell's book Code Complete 2 (Amazon link) - not C++ specific but full of great insights.
Coding standards are only meaningful if they help you write code. So they just need to keep your code consistent (ie if someone puts m_ for variable members and someone doesn't, it can take longer to grok the code than if they all used the same style).
That's all they (should) do, so just pick up your existing code and make sure your team codes to the same style.
I like to think of it like cartoons. If you become a cartoonist on the Simpsons, you have to draw eyes in the official way or everything looks pants, but if you go to Family Guy, you have to draw them differently. Neither way is wrong.
Too many standards are about meaningless restrictions, written by people who don't code themselves (or consider themselves too good to keep to them). Others try to teach you how to code. Neither has its place in a good standard, those just make it easier for you to look at some code and understand what its doing.
eg. my standards include rules for naming directories - you will always have your code in a directory called the same name as the project, and all binaries go in the bin subdir, with all config files in the same place, and a changelog, etc. All simple stuff, but I guarantee I'll never find a project called something different with its binaries in the root directory where I don't know what changes were made to it. Simple, easy stuff that makes a huge difference.
I agree with Harald Scheirich, it is most important to have the team agree on what the rules should be rather than just picking a set that has been recommended by outsiders.
My personal recommendation would be to read Code Complete, 2nd Edition by Steve McConnell which describes (among a whole lot of other useful stuff) several common coding standards and offers commentary on each. This might help your team in setting up your own standards.
Lockheed Martin's JSF Air Vehicle C++ Coding Standards is an interesting read but it's a bit overkill unless you're working in fields where a bug can kill people. It's still a very important example to look at from a computer ethics standpoint about an example of how to program with safety and correctness being top priority.
For general-purpose C++ coding, I'd personally recommend C++ Coding Standards by Herb Sutter. From the very beginning, it emphasizes what not to standardize (things relating to style or preference rather than practices that promote safety, correctness, efficiency). It's also among the easiest reads in your list giving very brief but concise arguments for each standard, making it something easy to show your co-workers.
Poco C++ Coding Style Guide.pdf
Apple Coding Guidelines for Cocoa
GNU Coding Standards
Bell Labs' Recommended C Style and Coding Standards
reserved names from POSIX / ISO
Facebook HHVM Coding Conventions
GeoSoft C++ Programming Style Guidelines
LLVM Coding Standards
C Style and Coding Standards for SunOS