2

I am sure this question was already answered couple of times, and i will soon close this topic, but i couldn't find it.

Is there a recommendet way of arranging member functions?

I am pretty sure not everything need rules, but wanted to if there are thoughts about this topic, which goes more then sorting by access level.

Example class in Pseudocode:

Class
     amethod()
     bmethod()
     cvariable
     avariable
     bvariable
Jason
  • 3,777
  • 14
  • 27
user1767754
  • 23,311
  • 18
  • 141
  • 164
  • In C++, the order of the member declarations matter insofar as it determines the order that they are initialized when you instantiate an object of that class. See [this](http://stackoverflow.com/a/12222509/2297365). – huu Aug 27 '15 at 01:11
  • @huu True of C++, but what I know about Python you can put inside the head of a pin. Conflating the two is probably a bad idea, but I'm willing to wait for Olaf to swing by and confirm that hypothesis. – user4581301 Aug 27 '15 at 01:15
  • @user4581301, didn't mean to conflate the two. The SO answer I linked is specifically for C++. I'll clarify this in my comment. – huu Aug 27 '15 at 01:17
  • Sorting members lexicographically can give a quick visual index, which sometimes is good, but it could be difficult to enforce. Another implication in C/C++ is the layout of the class in memory though. This can affect space needed for padding alignments (size in memory), as well as cache locality. – Jason Aug 27 '15 at 01:39

2 Answers2

3

There are some recommendations, but they are rather vague. The only common point is, quoting the Google Java Style Guide, § 3.4.2,

that each class order its members in some logical order, which its maintainer could explain if asked. For example, new methods are not just habitually added to the end of the class, as that would yield "chronological by date added" ordering, which is not a logical ordering.

The Google C++ Style Guide recommends to order by visibility (which is obvious for C++), then by type:

  • Typedefs and Enums
  • Constants (static const data members)
  • Constructors
  • Destructor
  • Methods, including static methods
  • Data Members (except static const data members)

Oracle’s Code Conventions for Java, § 3.1.3, recommend to order by type, then by visibility (for variables) or functionality (for methods):

  • Class (static) variables (ordered by visibility)
  • Instance variables (ordered by visibility)
  • Constructors
  • Methods (grouped by functionality rather than by scope or accessibility)
Robin Krahl
  • 5,268
  • 19
  • 32
  • (Why 66% of your answer are about Java?) – deviantfan Aug 27 '15 at 01:24
  • 1
    @deviantfan Because I am more familiar with the Java conventions and because the thoughts behind the recommendations (esp. ‘have a logical order’ and ‘group by functionality’) also apply to most other languages. I’m happy if someone can add recommendations from PEPs or other Python resources. – Robin Krahl Aug 27 '15 at 01:26
  • After some research: I could not find any recommendations for Python in the standard resources (PEP 0008, Google Python Style Guide, ‘Hitchhiker’s Guide to Python’, popular projects). – Robin Krahl Aug 27 '15 at 01:31
2

I´m not aware of any generally/widely accepted convention, other than "use something that makes sense" (eg. grouping variables instead of writing a wild mix of vars and functions).

However, it´s not only a style question:

In C++ (and C), the order maps directly to the memory layout, this can lead to different variable sizes because of alignment and padding. Additionally, if serializing something in a binary format, where which value is in the data is of course important (but serializing that way is not exactly good, because the memory layout depends...).

And, like @huu noted in the comments, the variable order determines the initialization order, this is important a member variables is initialized with the value of another member variable (of the same object). A mismatch in variable declaration order and initialization order will lead to a compiler error.

Community
  • 1
  • 1
deviantfan
  • 11,268
  • 3
  • 32
  • 49
  • 1
    A slight correction here, a mismatch in the variable *declaration* order and the initializer list order will cause a compiler warning, unless `-Wall` is specified, which of course makes all warnings errors. – huu Aug 27 '15 at 01:24
  • @huu `-Wall` doesn't make all warnings errors, `-Werror` does. https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html – cmorse Aug 30 '15 at 15:58
  • @cmorse, ah yes, you're correct. `-Wall` enables warning messages – huu Aug 30 '15 at 16:00