www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Code style for property

reply Andrey <andrey kabylin.ru> writes:
Hello, how better to declare properties, for example I have class:
 class Foo {
     this(in int x, in int y, Bar bar) {
         this.x = x;
         this.y = y;
         this.bar = bar;
     }
 private:
     int x;
     int y;
     Bar bar;
 }
And I want make access to read x, y and bar. Probably I should add prefix for private members, that is a question: what prefix should I use? Now I use prefix p_ (from the word property), but maybe prefix m_ is better and you need to use it for all private members? Another question: what style is better for declare getters? this:
 class Foo {
      property int x() { return p_x; // or m_x; }
      property int y() { return p_y; // or m_y; }
      property int bar() { return p_bar; // or m_bar; }
 }
or this:
 class Foo {
      property {
         int x() { return p_x; }
         int y() { return p_y; }
         int bar() { return p_bar; }
     }
 }
And one more question: should I add ref for property, to be able do this (if setter is declared):
 foo.x += 5
Mar 12 2017
next sibling parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Sunday, 12 March 2017 at 10:47:35 UTC, Andrey wrote:
 Hello, how better to declare properties, for example I have 
 class:
 class Foo {
     this(in int x, in int y, Bar bar) {
         this.x = x;
         this.y = y;
         this.bar = bar;
     }
 private:
     int x;
     int y;
     Bar bar;
 }
And I want make access to read x, y and bar. Probably I should add prefix for private members, that is a question: what prefix should I use? Now I use prefix p_ (from the word property), but maybe prefix m_ is better and you need to use it for all private members? Another question: what style is better for declare getters? this:
 class Foo {
      property int x() { return p_x; // or m_x; }
      property int y() { return p_y; // or m_y; }
      property int bar() { return p_bar; // or m_bar; }
 }
or this:
 class Foo {
      property {
         int x() { return p_x; }
         int y() { return p_y; }
         int bar() { return p_bar; }
     }
 }
And one more question: should I add ref for property, to be able do this (if setter is declared):
 foo.x += 5
You should only declare getters/setters if you need to (or think you may need to later) intercept the assignment or acquisition of a variable (logging, computing on demand) have a field as externally read only (setter only) otherwise you should have the variables as normally assignable. A single leading underscore is usually used to denote a private variable ( names prefixed with two leading underscores are reserved for use by the compiler). If you must use property and you have a whole lot of them in a row the second form is preferred (consider also using ` property:` if you have no more non- property function to declare). Note that you still need to declare the variables you are going to return in the property if the variable you are going to return is ref then unless you are choosing which variable to return at runtime (see first paragraph) then just have the variable be public.
Mar 12 2017
next sibling parent XavierAP <n3minis-git yahoo.es> writes:
On Sunday, 12 March 2017 at 11:15:04 UTC, Nicholas Wilson wrote:
 On Sunday, 12 March 2017 at 10:47:35 UTC, Andrey wrote:
 And I want make access to read x, y and bar. Probably I should 
 add prefix for private members, that is a question: what 
 prefix should I use? Now I use prefix p_ (from the word 
 property), but maybe prefix m_ is better and you need to use 
 it for all private members?
A single leading underscore is usually used to denote a private variable ( names prefixed with two leading underscores are reserved for use by the compiler).
If you need any prefix at all, a single underscore is enough, and Whether a private member is exposed as property or in some other way, can be seen in the getter/setter, no need to classify it into the member declaration. C++ kind or requires a letter on top such as m_ simply because any identifiers starting with an underscore are (mostly and certainly at the global scope) reserved (namespace pollution anyone?). It's really up to you, we won't call the police ;)
Mar 12 2017
prev sibling parent Andrey <andrey kabylin.ru> writes:
On Sunday, 12 March 2017 at 11:15:04 UTC, Nicholas Wilson wrote:
 You should only declare getters/setters if you need to (or 
 think you may need to later)
       intercept the assignment or acquisition of a variable 
 (logging, computing on demand)
       have a field as externally read only (setter only)
 otherwise you should have the variables as normally assignable.
What about it?
  property FrameRect margin() { return p_margin; }
  property void margin(in vec4 val) { p_margin = FrameRect(val); 
 }
  property void margin(in FrameRect val) { p_margin = val; }
or I started to use public members and after I wanted make some optimizations, I need rewrite my public member to property like this:
  property vec2 position() { return p_position; }
  property void position(vec2 val) {
     p_position = val;
     needUpdateMatrices = true;
 }
 
  property float rotation() { return p_rotation; }
  property void rotation(float val) {
     p_rotation = val;
     needUpdateMatrices = true;
 }
 
  property vec2 scaling() { return p_scaling; }
  property void scaling(vec2 val) {
     p_scaling = val;
     needUpdateMatrices = true;
 }
 
  property vec2 pivot() { return p_pivot; }
  property void pivot(vec2 val) {
     p_pivot = val;
     needUpdateMatrices = true;
 }
But code with this public members has spread throughout the project, e.g.:
 obj.position += vec2(2, 3);
 obj.rotation -= pi/2;
In that case I need to spend a lot more time than if I made members initially as property.
Mar 12 2017
prev sibling parent reply Stefan <dl ng.rocks> writes:
On Sunday, 12 March 2017 at 10:47:35 UTC, Andrey wrote:
 Hello, how better to declare properties, for example I have 
 class:
 class Foo {
     this(in int x, in int y, Bar bar) {
         this.x = x;
         this.y = y;
         this.bar = bar;
     }
 private:
     int x;
     int y;
     Bar bar;
 }
Andrey, you could try https://github.com/funkwerk/accessors We use it to avoid clutter. Stefan
Mar 12 2017
parent Andrey <andrey kabylin.ru> writes:
On Sunday, 12 March 2017 at 17:50:41 UTC, Stefan wrote:
 On Sunday, 12 March 2017 at 10:47:35 UTC, Andrey wrote:
 Hello, how better to declare properties, for example I have 
 class:
 class Foo {
     this(in int x, in int y, Bar bar) {
         this.x = x;
         this.y = y;
         this.bar = bar;
     }
 private:
     int x;
     int y;
     Bar bar;
 }
Andrey, you could try https://github.com/funkwerk/accessors We use it to avoid clutter. Stefan
Wow! that's great! thank you so much!
Mar 12 2017