Thread overview
Class Order Style
Feb 20, 2017
Jolly James
Feb 20, 2017
ketmar
Feb 20, 2017
Jolly James
Feb 21, 2017
Lenny Lowood
Feb 21, 2017
Jonathan M Davis
Feb 22, 2017
Jolly James
Feb 21, 2017
Seb
Feb 24, 2017
Jacob Carlborg
February 20, 2017
How to sort the members of a class?

like:
>1. properties
>then
>2. private 3. methods
>4. ctors
... and so on. are there any recommendations?


And what is better?

>class A
>{
>private:
>    int a;
>    int b;
>public:
>    int c;
>    int d;
>}
or
>class A
>{
>    private
>    {
>        int a;
>        int b;
>    }
>    public
>    {
>        int c;
>        int d;
>    }
>}
February 20, 2017
Jolly James wrote:

> How to sort the members of a class?
> like:
>>1. properties
>>then
>>2. private 3. methods
>>4. ctors
> ... and so on. are there any recommendations?
> And what is better?

just add ddoc documentation to 'em, and then it doesn't matter in which order they are declared: people will generate documentation to find out how to use your code. ;-)
February 20, 2017
On Monday, 20 February 2017 at 13:50:26 UTC, ketmar wrote:
> just add ddoc documentation to 'em, and then it doesn't matter in which order they are declared: people will generate documentation to find out how to use your code. ;-)

ah okay, thx


But what about this?

>>class A
>>{
>>private:
>>    int a;
>>    int b;
>>public:
>>    int c;
>>    int d;
>>}
> or
>>class A
>>{
>>    private
>>    {
>>        int a;
>>        int b;
>>    }
>>    public
>>    {
>>        int c;
>>        int d;
>>    }
>>}
February 21, 2017
On Monday, 20 February 2017 at 18:02:20 UTC, Jolly James wrote:
> On Monday, 20 February 2017 at 13:50:26 UTC, ketmar wrote:
>> just add ddoc documentation to 'em, and then it doesn't matter in which order they are declared: people will generate documentation to find out how to use your code. ;-)
>
> ah okay, thx
>
>
> But what about this?
>
>>>class A
>>>{
>>>private:
>>>    int a;
>>>    int b;
>>>public:
>>>    int c;
>>>    int d;
>>>}
>> or
>>>class A
>>>{
>>>    private
>>>    {
>>>        int a;
>>>        int b;
>>>    }
>>>    public
>>>    {
>>>        int c;
>>>        int d;
>>>    }
>>>}


Me as a beginner would like to know this, too ...
February 21, 2017
On Tuesday, February 21, 2017 22:41:40 Lenny Lowood via Digitalmars-d-learn wrote:
> On Monday, 20 February 2017 at 18:02:20 UTC, Jolly James wrote:
> > On Monday, 20 February 2017 at 13:50:26 UTC, ketmar wrote:
> >> just add ddoc documentation to 'em, and then it doesn't matter in which order they are declared: people will generate documentation to find out how to use your code. ;-)
> >
> > ah okay, thx
> >
> >
> > But what about this?
> >
> >>>class A
> >>>{
> >>>
> >>>private:
> >>>    int a;
> >>>    int b;
> >>>
> >>>public:
> >>>    int c;
> >>>    int d;
> >>>
> >>>}
> >>>
> >> or
> >>
> >>>class A
> >>>{
> >>>
> >>>    private
> >>>    {
> >>>
> >>>        int a;
> >>>        int b;
> >>>
> >>>    }
> >>>    public
> >>>    {
> >>>
> >>>        int c;
> >>>        int d;
> >>>
> >>>    }
> >>>
> >>>}
>
> Me as a beginner would like to know this, too ...

It's completely a stylistic preference. There are a number of different ways to order your member variables and functions, and there are several different ways to apply attributes to them.

As far as public and private go, I think that most folks either use the labels like

private:
public:

or they put them directly on the members like

private int a;

I suspect that the folks who programmed a lot in C++ tend to do the former, since that's the way you have to do it in C++, and I'd guess that the folks who have programmed a lot in Java or C# typically do the latter, because that's how you have to do it in those languages.

There is no right or wrong way. Personally, I use the labels like in C++ and put the public stuff first in a class or struct and the private stuff last (and I think that that's what Phobos mostly does), but you'll find plenty of folks who do it differently.

- Jonathan M Davis

February 21, 2017
On Monday, 20 February 2017 at 13:47:07 UTC, Jolly James wrote:
> How to sort the members of a class?
>
> like:
>>1. properties
>>then
>>2. private 3. methods
>>4. ctors
> ... and so on. are there any recommendations?
>
>
> And what is better?
>
>>class A
>>{
>>private:
>>    int a;
>>    int b;
>>public:
>>    int c;
>>    int d;
>>}
> or
>>class A
>>{
>>    private
>>    {
>>        int a;
>>        int b;
>>    }
>>    public
>>    {
>>        int c;
>>        int d;
>>    }
>>}

Two pointers towards two popular style guides, which could also help to answer future questions:

http://dlang.org/dstyle.html

https://vibed.org/style-guide
February 22, 2017
On Tuesday, 21 February 2017 at 23:06:23 UTC, Jonathan M Davis wrote:
> On Tuesday, February 21, 2017 22:41:40 Lenny Lowood via Digitalmars-d-learn wrote:
>> [...]
>
> It's completely a stylistic preference. There are a number of different ways to order your member variables and functions, and there are several different ways to apply attributes to them.
>
> [...]

thank you!
February 24, 2017
On 2017-02-20 14:47, Jolly James wrote:
> How to sort the members of a class?
>
> like:
>> 1. properties
>> then
>> 2. private 3. methods
>> 4. ctors
> ... and so on. are there any recommendations?

In my opinion:

1. Manifest constants (enum)
2. Static variables
3. Instance variables
4. Constructors
5. Properties
6. Methods

> And what is better?
>
>> class A
>> {
>> private:
>>    int a;
>>    int b;
>> public:
>>    int c;
>>    int d;
>> }
> or
>> class A
>> {
>>    private
>>    {
>>        int a;
>>        int b;
>>    }
>>    public
>>    {
>>        int c;
>>        int d;
>>    }
>> }

I usually go with "private int a;" if there are four or less fields (static or instance). Otherwise I use the block style (with curly braces). For the methods I use the Java style with the protection attribute attached for each method. Sometimes I put a bunch of internal methods at the end, then I use the label (C++) style.

class Foo
{
    enum a = 1;
    enum b = 2;

    static int c = 3;
    static int d = 4;

    private int e_;
    private int f_;

    this(int e, int f) {}

    static void g();
    static void h();

    int e() { return e_; }
    int f() { return f_; }

    void i();
    void j();

private:

    void k();
    void l();
}

-- 
/Jacob Carlborg