local variable naming convention

Jacob Carlborg doob at me.com
Fri Dec 20 06:35:13 PST 2013


On 2013-12-20 10:15, Boyd wrote:
> I'm in the process of adapting my library to the D standard naming
> convention.
>
> The problem is that I used to separate member variables and local
> variables through capitalization.
>
> X would be a member variable.
> x would be a local variable.
>
> this allowed me, among other things, to create constructors such as:
>
> this(int x, int y)
> {
>     X = x;
>     Y = y;
> }
>
> So now my question is, how do you distinguish between member and local
> vars in such cases?

This is how I would do it:

class Point
{
     private int x_;
     private int y_;

     this (int x, int y)
     {
         x_ = x;
         y_ = y;
     }

     int x ()
     {
         return x_;
     }

     int y ()
     {
         return y_;
     }

     int x (int x)
     {
         return x_ = x;
     }

     int y (int y)
     {
         return y_ = y;
     }
}

If I don't have a conflict with methods I just drop the underscore and 
use "this." in the constructor:

struct Point
{
     int x;
     int y;

     this (int x, int y)
     {
         this.x = x;
         this.y = y;
     }
}

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list