C++ Coding Standards: 101 Rules, Guidelines, and Best Practices

Category: Programming
Author: Herb Sutter, Andrei Alexandrescu
4.5
All Stack Overflow 38
This Year Stack Overflow 2
This Month Stack Overflow 7

Comments

by anonymous   2019-07-21

not nearly as concise as the link you provided: but the following chapter 14 - 24 may help :) hehe

ref: http://www.amazon.com/Coding-Standards-Rules-Guidelines-Practices/dp/0321113586/ref=sr_1_1?ie=UTF8&s=books&qid=1284443869&sr=8-1-catcorr

by anonymous   2019-07-21

i base myself on "C++ Coding Standards: 101 Rules, Guidelines, And Best Practices" by Sutter and Alexandrescu, and also Bob Martin's SOLID. I agree with them on this point of course ;-).

If the message/function doesnt interract so much with your class, you should make it a standard ordinary function taking your class object as argument.

You should not polute your class with behaviours that are not intimately related to it. This is to repect the Single Responsibility Principle: Your class should remain simple, aiming at the most precise goal.

However, if you think your message/function is intimately related to your object guts, then you should include it as a member function of your class.

by anonymous   2019-07-21

Ugly or pretty depend on you! But keep in mind

  • Programs should be written for people to read, and only incidentally for machines to execute.
  • Make it clearer to both humans and compilers that the code does what you intended.

As Other answers I would shorten your code to while (currGame.playGame(currPlayer));. But anyway this is not a big deal.

For further this small book would be a good reference to read http://www.amazon.com/Coding-Standards-Rules-Guidelines-Practices/dp/0321113586

by anonymous   2019-07-21

Designing C++ class hierarchies for others to use is a minefield. I would highly recommend reading up on some of the common pitfalls in class design. Two books I suggest:

  • C++ Coding Standards (Herb Sutter & Andrei Alexandrescu). Packed full of concrete examples of how to create proper classes in an easy to understand way.
  • Effective C++ (Scott Mayers). How to write correct C++, starting from the transition from C to C++. A classic.
by anonymous   2019-01-13

Unlike Java, C++ doesn't have a "standard style". Pretty much very company I've ever worked at has its own C++ coding style, and most open source projects have their own styles too. A few coding conventions you might want to look at:

It's interesting to note that C++ coding standards often specify which parts of the language not to use. For example, the Google C++ Style Guide says "We do not use C++ exceptions". Almost everywhere I've worked has prohibited certain parts of C++. (One place I worked basically said, "program in C, but new and delete are okay"!)

by luke   2017-08-20

Others have answered how to resolve the syntax problem and why it can be dangerous to derive from standard classes, but it's also worth pointing out:

Prefer composition to inheritance.

I doubt you mean for 'A' to explicitly have the "is-a" relationship to multimap< int, bool >. C++ Coding Standards by Sutter/Alexandrescu has entire chapter on this (#34), and Google points to many good references on the subject.

It appears there is a SO thread on the topic as well.

by anonymous   2017-08-20

In the words of Alexandrescu & Sutter (item 9) Don't pessimize prematurely

Avoiding premature optimization does not imply gratuitously hurting efficiency. By premature pessimization we mean writing such gratuitous potential inefficiencies as:

• Defining pass-by-value parameters when pass-by-reference is appropriate. (See Item25.)

• Using postfix + + when the prefix version is just as good. (See Item 28.)

• Using assignment inside constructors instead of the initializer list. (See Item 48.)

Whenever you are writing assignments inside constructors, your code reviewers will be on alert: is something special going on? Did he really want some special two-stage initialization (because there is an implicit default construction of the member being generated anyway!). Don't surprise the readers of your code gratuitously.

Note that Alexandrescu & Sutter go on in Item 48 to discuss the potential inefficiency, but don't claim anywhere that there an actual inefficiency in real optimized code. That is also beside the point, it's about expressing intent and avoiding the risk of inefficiency.

by anonymous   2017-08-20

You have new A(...) way down there. What gets called is A's copy constructor (created implicitly by the compiler.

What you want is a clone method. See here. It recaps the appropriate item from the excellent C++ Coding Standards book. Below is a shameless copy of the final solution, which also shows a nice use of the NVI idiom to avoid the slicing problem.

class A {// …
public:
  A* Clone() const {                        // nonvirtual
    A* p = DoClone();
    assert( typeid(*p) == typeid(*this) && "DoClone incorrectly overridden" );
    return p;                                // check DoClone's returned type
  }

protected:
 A( const A& );
 virtual A* DoClone() const = 0;
};

class B : public A { // …
public:
  virtual B* Clone() const {return new B(*this); }

protected:
  B( const B& rhs ) : A( rhs ) {/* … */}
};

update A bit of an explanation. The basic idea of the clone is the same as the other excellent answers here.

Now, with cloning you have the danger of slicing objects. For example, if some object which derives from A forgets to implement its own clone method, then a call to A* a = d->clone() will not return a full D object (assuming D is a descendant of A)

The NVI idiom says to separate a public interface from a virtual interface. Thus, in this example, clone is public, but not virtual. It call a protected virtual method, doClone, which does the actual cloning, and which derived objects also implement. Because of the split, the clone method can verify that the type of the cloned object matches the type of the original object.

by litb   2017-08-20

Beginner

Introductory, no previous programming experience

Introductory, with previous programming experience

* Not to be confused with C++ Primer Plus (Stephen Prata), with a significantly less favorable review.

Best practices


Intermediate


Advanced


Reference Style - All Levels

C++11/14 References:

  • The C++ Standard (INCITS/ISO/IEC 14882-2011) This, of course, is the final arbiter of all that is or isn't C++. Be aware, however, that it is intended purely as a reference for experienced users willing to devote considerable time and effort to its understanding. As usual, the first release was quite expensive ($300+ US), but it has now been released in electronic form for $60US.

  • The C++14 standard is available, but seemingly not in an economical form – directly from the ISO it costs 198 Swiss Francs (about $200 US). For most people, the final draft before standardization is more than adequate (and free). Many will prefer an even newer draft, documenting new features that are likely to be included in C++17.

  • Overview of the New C++ (C++11/14) (PDF only) (Scott Meyers) (updated for C++1y/C++14) These are the presentation materials (slides and some lecture notes) of a three-day training course offered by Scott Meyers, who's a highly respected author on C++. Even though the list of items is short, the quality is high.

  • The C++ Core Guidelines (C++11/14/17/…) (edited by Bjarne Stroustrup and Herb Sutter) is an evolving online document consisting of a set of guidelines for using modern C++ well. The guidelines are focused on relatively higher-level issues, such as interfaces, resource management, memory management and concurrency affecting application architecture and library design. The project was announced at CppCon'15 by Bjarne Stroustrup and others and welcomes contributions from the community. Most guidelines are supplemented with a rationale and examples as well as discussions of possible tool support. Many rules are designed specifically to be automatically checkable by static analysis tools.

  • The C++ Super-FAQ (Marshall Cline, Bjarne Stroustrup and others) is an effort by the Standard C++ Foundation to unify the C++ FAQs previously maintained individually by Marshall Cline and Bjarne Stroustrup and also incorporating new contributions. The items mostly address issues at an intermediate level and are often written with a humorous tone. Not all items might be fully up to date with the latest edition of the C++ standard yet.

  • cppreference.com (C++03/11/14/17/…) (initiated by Nate Kohl) is a wiki that summarizes the basic core-language features and has extensive documentation of the C++ standard library. The documentation is very precise but is easier to read than the official standard document and provides better navigation due to its wiki nature. The project documents all versions of the C++ standard and the site allows filtering the display for a specific version. The project was presented by Nate Kohl at CppCon'14.


Classics / Older

Note: Some information contained within these books may not be up-to-date or no longer considered best practice.

by anonymous   2017-08-20

You might wnat to check out The Definitive C++ Book Guide and List

For your purposes I would especially recommend:

They are not in particular order, also you might want to read and code something in between them.

(Note: As noted by @John Dibling the Boost book might be a bit out of date, I do not have experience with that one myself)

by anonymous   2017-08-20

Your colleague is correct, you should wrap cpp with namesapce which means you define your funcitons inside namesapce NS.

namespace NS
{    
  void A::method() 
  {
    // ...
  }
}

You may get conflict name lookup if you have multiple A available.

The dangerous thing happening here is that the using declaration takes a snapshot of whatever entities named A::method in namespace NS have been seen by the time the using declaration is encountered.

Have a read of Google cpp style guilde regarding namespace, also 101 c++ coding standards

by anonymous   2017-08-20

For this kind of thing I recommend:

C++ Coding Standards: 101 Rules, Guidelines, and Best Practices

by anonymous   2017-08-20

You're probably looking for C++ "best practices", not "laws". This should help you searching on the net.

Moreover, there's a book called "C++ Coding Standards: 101 Rules, Guidelines, and Best Practices" by Herb Sutter and Andrei Alexandrescu which is supposed to be good, but I haven't read it myself. You can order it, e.g., over at amazon.com.