Best practice: ordering of public/protected/privat

2019-01-30 10:27发布

I am starting a new project from the ground up and want it to be clean / have good coding standards. In what order do the seasoned developers on here like to lay things out within a class?

A : 1) public methods 2) private methods 3) public vars 4) private vars

B : 1) public vars 2) private vars 3) public methods 4) private methods

C : 1) public vars 2) public methods 3) private methods 4)private vars

I generally like to put public static vars at the top, but then would a public static method be listed ahead of your constructor, or should the constructor always be listed first? That sort of thing...

I know it's finnicky but I just wondered: what are best practices for this?

PS: no I don't use Cc#. I know. I'm a luddite.

10条回答
可以哭但决不认输i
2楼-- · 2019-01-30 10:29

In Clean Code, Robert C. Martin advises coders to always put member variables at the top of the class (constants first, then private members) and methods should be ordered in such a way so that they read like a story that doesn't cause the reader to need to jump around the code too much. This is a more sensible way to organize code rather than by access modifier.

查看更多
Animai°情兽
3楼-- · 2019-01-30 10:37

I generally agree with the public, protected, private order as well as the static data, member data, member functions order.

Though I sometimes group like members (getters & setters) I generally prefer listing members within a group ALPHABETICALLY so that they can be located more easily.

I also like lining up the data/functions vertically. I tab/space over to the right enough so that all names are aligned in the same column.

查看更多
Evening l夕情丶
4楼-- · 2019-01-30 10:37

The sequence of public followed by protected and private is more readable to me, It's better to describe the class logic in comments at top of the header file simply and function call orders to understand what a class dose and algorithms used inside.

I am using Qt c++ for a while and see some new sort of keywords like signal and slot I prefer to keep ordering like above and share my idea with you here.

#ifndef TEMPLATE_H
#define TEMPLATE_H


class ClassName
{
    Q_OBJECT
    Q_PROPERTY(qreal startValue READ startValue WRITE setStartValue)
    Q_ENUMS(MyEnum)

public:

    enum MyEnum {
        Hello = 0x0,
        World = 0x1
    };

    // constructors

    explicit ClassName(QObject *parent = Q_NULLPTR);
    ~ClassName();

    // getter and setters of member variables

    // public functions (normal & virtual) -> orderby logic

public slots:

signals:

protected:

    // protected functions it's rule followed like public functions


private slots:

private:

    // methods

    // members

};

#endif // TEMPLATE_H
查看更多
孤傲高冷的网名
5楼-- · 2019-01-30 10:42

I used to care a lot. Over the last several years using modern IDEs pretty much everything is only 1 or 2 keystrokes away, I've let my standards relax substantially. Now, I start with statics, member variables, then constructors after that I don't worry about it much.

In C# I do let Resharper organize things automatically.

查看更多
戒情不戒烟
6楼-- · 2019-01-30 10:43

Personally I like to have public at top, protected and then private. The reason for this is that when somebody cracks open the header he/she sees what he/she can access first, then more details as he/she scrolls down.

查看更多
叛逆
7楼-- · 2019-01-30 10:46

To each his own, and as Elzo says, modern IDEs have made it easier to find members and their modifiers in an easy way with colored icons in drop-down menus and such.

My take is that it is more important for the programmer to know what the class was designed for, and how it can be expected to behave.

So, if it is a Singleton, I put the semantics (static getInstance() class) first.

If it is a concrete factory, I put the getNew() function and the register / initialize functions first.

... and so on. When I say first, I mean soon after the c'tors and d'tor - since they are the default way of instantiating any class.

The functions that follow are then in:

  1. logical call-order (e.g. initialize(), preProcess(), process(), postProcess() ), or
  2. related functions together (like accessors, utilities, manipulators etc),

depending on if the class was meant primarily to be a data-store with some functions, or function provider with a few data members.

查看更多
登录 后发表回答