www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Class Order Style

reply Jolly James <j.j jmail.com> writes:
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;
    }
}
Feb 20 2017
next sibling parent reply ketmar <ketmar ketmar.no-ip.org> writes:
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. ;-)
Feb 20 2017
parent reply Jolly James <j.j jmail.com> writes:
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;
    }
}
Feb 20 2017
parent reply Lenny Lowood <lo wood.as> writes:
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 ...
Feb 21 2017
parent reply Jonathan M Davis via Digitalmars-d-learn writes:
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 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
Feb 21 2017
parent Jolly James <j.j jmail.com> writes:
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!
Feb 22 2017
prev sibling next sibling parent Seb <seb wilzba.ch> writes:
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
Feb 21 2017
prev sibling parent Jacob Carlborg <doob me.com> writes:
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
Feb 24 2017