Friday, December 1, 2006

The Crazy Class Layout

When I read books about software development there is one thing that increases my blood pressure to clinically hazardous hights: The class layout.

Regardless of whether they're classics or quite modern, whether they use C++, Java or Python, whether the topic is software design or technical wisdom, they follow the same pattern for the layout of an object oriented class definition:

class FunnyExample
{
private:
FunnyDataType funnyData;
FunnyDataType2 moreData;

void somePrivateFunction();

public:
FunnyExample();
~FunnyExample();

theFirstPublicFunction();
};

The private data is at the top.

In all the books I read, I never found anybody explaining the reason for the private-at-the-top layout. In this blog entry I'll explain why I consider this "bad practice".

When the programmer opens a file like this, the first thing that she sees are the data implementation details of the class. When she writes code using this class, she will probably not be able to forget about those implementation details and use them subconsciously, which makes it harder to change those details afterwards.

This class layout contradicts the object oriented design idea: The class specifies an interface, a set of "messages" that can be sent to an object. In my experience this class layout often shows that the programmer didn't understand the object oriented idea. Classes are simply used as convenient data containers and methods are used as ways to manipulate their data.

But most of the time an object oriented class should be a reference to a domain concept. Methods on this class should be ways to interact with a domain object. Data oriented design leads to a technical view of the solution, rather than the problem, which will be hard to maintain later. Writing the public interface of a class at the top stresses the message oriented design.

Organize a class layout from most used to less used from the perspective of a developer using the class.

This implies putting the private data at the end of the class definition, where it is hidden away if a developer just want's to use the class:

class FunnyExample
{
public:
// this is what I see first when I open this source file

FunnyExample();
~FunnyExample();

theFirstPublicFunction();

private:
// if I just want to use the class, I can stop reading here

FunnyDataType funnyData;
FunnyDataType2 moreData;

void somePrivateFunction();
};

2 comments:

  1. Very insightful post.
    I must note, that I've lately been in the habit of placing the most used/referenced (public) methods at the top and everything else after. But, I wasn't consciously thinking of limiting visibility to users.
    Member variables are still up there but, I like having them all in one place.

    I think that I'll add this to my policy: Order class methods in order of broadest to narrowest scope.

    ReplyDelete
  2. Using Blood Pressure Monitor At Home...

    Hypertension and high blood pressure are alternative beats of the same heart. Do not neglect this serious type of disturbance in your system. Do not be under the impression that these high and lows happen in life!...

    ReplyDelete