Phobos classes member declaration order

Jonathan M Davis jmdavisProg at gmx.com
Fri May 13 15:31:11 PDT 2011


> On 13.05.2011 23:07, Jonathan M Davis wrote:
> > I believe that the two most common practices when declaring member
> > variables is either to put them all at the top of the class declaration
> > or all at the bottom. If they're private (as is usually the case), I
> > much prefer to put them at the bottom.
> 
> Could you please tell me, why you put them at the bottom? I just want to
> understand why declarations at the bottom are better than at the top...
> especially when it makes search difficult when working with someone else
> code...

Because they're private. It's quite typical to put all of the private stuff at 
the bottom. This makes particular sense in C++ where you're dealing with 
header files, and people are reading the header file to see the public API, 
not the implementation. As such, you really don't want the private stuff at 
the top.

While we have ddoc documentation to read the API in D rather than header 
files, I see no reason to change the practice of putting the private stuff at 
the bottom. And if there is no generated ddoc documentation available, then 
someone is going to be reading the file to look at the API - much as they 
would in C++ - and then the private stuff is just in the way. 
Organizationally, I think that it makes a lot of sense to put the public stuff 
at the top and the private stuff at the bottom.

> For instance, talking about core.thread, it took me some time to find the
> type of m_addr (I am digging for some mysterious segfault in
> Thread.priority), as this member is used quite often, so search, of
> course, took some time.
> 
> Given the size of this code, even finding class bottom was a problem.
> Sure, there are some IDE's or editors which can point you exactly to
> declaration, but none are available for Linux in text mode, AFAIK...

I would expect most IDEs and code editors to be able to at least hop to 
matching braces. As such, finding the bottom of a class definition is easy. If 
your editor can't do something even that basic, I'd advise picking a different 
one. And regardless of that, you can always search for them. If the private 
member variables were at the top, then it would be something else that you 
were trying to find (some particular function for instance) and you'd still 
have to go digging through the class. So, no matter where you put what in a 
class definition, you're going to have to search for _something_ at some point 
unless the class is very small.

- Jonathan M Davis


More information about the Digitalmars-d mailing list