Class Order Style

Jacob Carlborg via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Feb 24 04:59:40 PST 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


More information about the Digitalmars-d-learn mailing list