tags:

views:

179

answers:

8

When writing a class do you group members variables of the same type together? Is there any benefit to doing so? For example:

class Foo
{
private:
    bool a_;
    bool b_;
    int c_;
    int d_;
    std::string e_;
    std::string f_;
    ...
};

As opposed to:

class Bar
{
private:
    std::string e_;    
    bool a_;
    int d_;
    bool b_;
    std::string f_;
    int c_;    
    .

.. };

Or do you simply have them in the order they were added?

+8  A: 

You should have them in the order you want them initialised, because that's the order they will be initialised, regardless of your initialiser list within the constructor. Not all compilers will warn you about inconsistencies, either.

Alastair
+1, but it's better to design classes in such a way that initialization order does not matter -- exactly because it's too easy to slip up without even a warning.
atzz
+1 Yeah. This is important to know and care about.
AndreasT
+11  A: 

I group them according to semantics, i.e.

class Foo
{
private:
    std::string peach;
    bool banana;
    int apple;

    int red;
    std::string green;
    std::string blue;
    ...
};

The more readable, the better.

Federico Ramponi
+1 for semantics, it's definitely the best way of doing it
Rob Stevenson-Leggett
Just want to add a little pointer to alistairs answer below! Members will be initialized in the order they appear in the class definition, not in the order they appear in the ctor initializer list! So you have to bear this in mind and cannot order them completely to your liking!
AndreasT
+2  A: 

If you care about the size of your objects, and if the compiler doesn't re-order members within a class (if there are no access-specifiers between the members, it must not reorder), then the objects might come out smaller if you order your members from biggest to smallest. The reason is that there is less likely to be a need for padding to satisfy alignment requirements. This ordering will result in members of the same type being closer together.

Compared with code clarity, this usually loses. Compared with initialization order of course it always loses (although you can add access-specifiers and hope for the best). But you did ask if there's any benefit.

Steve Jessop
+1  A: 
  1. Group semantically.
  2. If you need to group semantically, it's a code smell. Extract a class. Lucky for you, related members are already in a group, so it's easy to pick the right ones to move.
Jay Bazuzi
A: 

In most cases you should not have too many members in a structure.

If you do, maybe you should think about substructures...

I know there are exceptions, but as a general rule of thumb, try to keep your structures small.

Edouard A.
A: 

In order of effective priority :

0) IF members have dependencies between them on construction OR if members need to be constructed in a specific order : declare them in the order of construction you wish to be exectued. On construction, the only order of member construction is the member order of declaration in the class.

0.5) IF the class instance object size matters (you want the minimum size for this type - but dont do early optimization!), with some compilers it is better to order your members in size order, the bigger the first, for byte alignement or other similar behavior on compilation.

1) Prioritize CLARITY first : write for the reader first (be clear, order in thematic/purpose groups, etc), then for the compiler (maybe following the previous advices).

In almost all cases, only 1) is important. Prefer thematic, purpose and "composants of the same system" for grouping members over grouping by type.

Note : if you group your members by system and you see you have lot of groups, maybe you should encapsulate those systems in classes.

Klaim
A: 

I agree with the others about some reasons for ordering the variables, but beyond that, I like to see the variables that are most important to the class functioning near the beginning, and the variables that are less important near the end. If you have other reasons to override this, fine, but when other things fail, that's what I do.

Of course, this usually ends up happening by declaring the variables in the order they occur to me. The most important variables are the ones I think of first, and the others I encounter as I'm actually writing the class implementation.

Caleb Huitt - cjhuitt
+1  A: 

From Steve McConnell's Code Complete, p762:-

Order declarations sensibly: ... Grouping by types is usually sensible since variables of the same type tend to be used in related operations ... If your list of variables is so long that alphabetical ordering helps, your routine is probably too big.

Also, on the previous page, he lays into the style from my previous answer showing multiple declarations "crowded" onto one line...

bool a_, b_, c_;

... with a "Coding Horror" icon. So I'm reconsidering that approach. Thanks to commenters for making me reevaluate this.

Roddy