Code style for property
Andrey via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Mar 12 03:47:35 PDT 2017
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
More information about the Digitalmars-d-learn
mailing list