Code style for property

Andrey via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Mar 12 06:48:59 PDT 2017


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.


More information about the Digitalmars-d-learn mailing list