2

Normally, a C++ class is written with public: and private: applying to a series of members:

class Foo {
private:
    int x;
    int y;
    int z;
public:
    int getX();
    int setY(int val);
    void print();
}

An alternative coding style is to explicitly prefix every member with public/private. Is this a good or bad idea in C++? Here is the previous example altered in this way:

class Foo {
    private: int x;
    private: int y;
    private: int z;
    public: int getX();
    public: int setY(int val);
    public: void print();
}

I couldn't find any opinions or advice on this topic from searching the web, Software Engineering SE, or Stack Overflow. Here is my brainstorm:

Advantages of the first, traditional way:

  • It's the idiomatic style when reading and writing C++ code.
  • It reduces redundancy when a large batch of members are all public or all private.

Advantages of the second, explicit way:

  • The public/private designation is immediately visible at the point where each member is declared. There is no need to scan upwards a potentially large amount of code to find the most recent visibility modifier.
  • When rearranging the order of members, this technique reduces noise in diffs, and eliminates the possibility of accidentally moving a public variable into a private section or vice versa. (However, it is debatable whether these problems are common or uncommon in the traditional style.)
  • It is the mandatory style in closely related languages like Java and C#.
14

This is what is invariably going to happen sooner or later:

class Foo {
private: int x;
private: int y;
private: int z;
   int w;
   int q;
public: int getX();
public: int setY(int val);
public: void print();
   int getW();
   int setQ();
private:
   int f;
}

And then all those extraneous publics and privates are just noise. The compiler doesn't enforce it, so you might as well go with what's idiomatic and it's far easier to be dilligent in just ordering your class members.

Requiring the permission modifiers on every member as is done in Java and C# and whatnot is not nearly as obnoxious because you don't have to maintain source files and header files as you do in C++. The class, the members, and their permissions are written only once and all in the same spot, so it's no big deal.

  • 1
    Inevitable, unless you set up your code linter to prevent it :-) – Philip Kendall Apr 9 '17 at 7:27
  • 1
    You make good points, but I disagree when you mention about maintaining source and header files. It is not relevant to the discussion because public/private are only declared in C++ header files, not in source files. – Nayuki Apr 9 '17 at 14:55
  • @Nayuki: except it is, because maintaining two pieces of interlinked information in two different places is obnoxious. – whatsisname Apr 9 '17 at 15:39
10

It's a bad idea.

As you say yourself:

[The traditional way] is the idiomatic style when reading and writing C++ code.

Always use the style of least surprise. When somebody else reads your code, they will wonder "why did you do this?" when you do things differently than expected. This reaction slows down reading your code.

3

Your analysis ignores an advantage of the traditional format: it encourages a style whether members are grouped by visibility, promoting a kind of tidiness that makes code navigation substantially simpler. Using that style entirely avoids any of the issues that you cite as advantages of your suggested format, and in my experience has become almost universal in real C++ code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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