Best practice: ordering of public/protected/private within the class definition?

后端 未结 10 1312
遥遥无期
遥遥无期 2020-12-12 14:34

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

10条回答
  •  失恋的感觉
    2020-12-12 14:56

    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.

提交回复
热议问题