@property get/set or public varaible?

angel via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Dec 4 09:21:30 PST 2016


On Sunday, 4 December 2016 at 15:30:22 UTC, vladdeSV wrote:
> Hello!
>
> I have a question not directly related to D as it is with 
> coding standards.
>
> My issue at hand is if I have one variable for a class, which I 
> want to be directly accessible for anything else, should it be
>  1. public, or
>  2. private, with @property get/setters?
>
> From what I have been told is that variables should be private. 
> But if I do not want to make any checks whatsoever when setting 
> a variable, I see no benefit to the private approach.
>
> Are there any other reasons to use get/setters?

Make the member variable public, if it serves your purpose.
If (and when) you feel like taking some control over setting and 
getting its value, you will upgrade it to @property 
setter/getter, making the actual member variable private.
For most reasonable use cases the upgrade should pass with no 
problems.
...
If you envision such an upgrade possibility, try to keep away 
from taking your member variable address, and other not 
method-friendly operations, that might hold the upgrade back.


More information about the Digitalmars-d-learn mailing list