OO: Should you access your personal variables through properties within your class?

I was wondering what good practice is:

private int value; public int Value { get { return this.value; } } private int DoSomething() { return this.Value + 1; //OR return this.value + 1; } 

So the question is how should you relate to your class variables. Should you access them through your properties or just direct them?

+4
source share
8 answers

In this case, it does not really matter, but in cases where you use lazy loadable variables, it matters, and you get access to your property. For instance:

 private Myobject _value; public Myobject Value { get { if (_value == null) _value = new MyObject(); return _value; } } private MyObject DoSomething(); { //If you access _value here, it might be null... //so you should access it through the property: return Value; } 

In the case when your method failed by calling the field directly, you either process it properly or access it using the cleanup method - through your property.

It all comes down to the architecture of your application, and for this you must ask the question: what matters most from the point of view of service?

I must say that if the field is initialized correctly, and it does not cause you a headache to access it directly, then contact it directly. If this causes additional work (and therefore additional maintenance), then refer to the property.

As with anything else, weigh the pros and cons. Use your own common sense - standards wars - a ridiculous distraction. If they do not provide you with arguments that you have not thought about yourself, do not waste your breath. If you have a good reason to choose whichever path you choose, then this path is correct - it comes down to the prerogative of the designer.

My thoughts on the pros and cons are:

Transition with property:

  • I can change the implementation of the method of returning this value without the need to refactor my entire class.
  • I can be lazy to load the value, which means that if I never get access to it, it will never waste resources.
  • I can hide all implementation details of how the value is returned in one place, without having to process it throughout my code.

Transition with a field:

  • I don’t have a potential overhead resource when I need to go through the property code every time I access a value.
  • I need to make sure that the value is initialized correctly on every call or handles cases where it is not.
  • I can affect the value even if my property can only provide a readable interface.

So, I assume that my approach would be to use this property if I didn’t need to write the value directly, in which case I would go with a field - since my property is read-only and therefore cannot be written to.

It's just me, though - your property can be read / written, and you can decide from a design point of view that access to the field directly is okay - and that's fine too.

A trick always does something for a reason, don’t do anything blindly just because.

+8
source

I would go for direct access. All this is to "keep him in the family," so to speak.

+2
source

This is what war standards fight. Usually this will not make much difference, and I decided to simply access the variable directly from within the class, with the exception of getters and setters that do something other than just getting or setting a value.

The trick is that you should know what getters and settings do, or you should always limit them to a simple operation without any additional functions.

+1
source

In the properties, the “hide” structure is introduced to end up being a more complex structure than returning a single value from a field.

I would suggest using this property than a field in all of your code, so if the property code subsequently changes to something more complex, you do not need to reorganize the rest of the class.

+1
source

I vote to return this.value + 1.

The argument against this (i.e., passing through a property) is that later on you will want to add additional code to your property method, so if you do this, there will be fewer changes. But I believe that properties should only do what is advertised and no more; that is, they must do enough to bring back that value.

0
source

In some situations, this is a good idea. For example, in the case of the iPhone, given the low memory capacity, you may receive “memory alerts” from the OS. In accordance with these warnings, you will be asked to free memory so as not to close it. Typically, this memory comes from resources such as images, sounds, or large pieces of data that you do not need permanently in memory.

In this case, I sometimes access some private ivars through private Objective-C accessors, which usually check if ivar is "NULL" and loads data into memory, "lazy loading", ad-hoc. For this, I believe that private properties are really useful. Otherwise, I use ivar direct access.

As always, I don’t think there is a final answer to this question: "it depends."

0
source

If your class cannot trust its own methods of accessing its own variables, who can it trust?

0
source

I do not think this is a black and white question. A few spring things:

Firstly, if the property has no behavior (i.e. get / set simply goes to get and return the field), then make the field an auto-property and remove the dilemma. Fewer lines of code without confusion.

Secondly, if a property has side effects (lazy loading, notification of changes, statistics collection, etc.), then you should consider whether it is advisable to run this behavior through private updates. If appropriate, just use the property. If this does not fit, do not (or change the design to make it more obvious).

In addition, if necessary, you can always enter a shell type to eliminate confusion, if very important.

eg. let's say you have an Angle property that is protected by argument checking.

 public class ManThatCanRotate { public int Angle { get { return m_angle; } set { if(value >= 0 && value < 360) m_angle = value; } } public void RotateLikeSomeKindOfLunatic() { // imagine this has been called 359 times already. m_angle++; // ruh-roh } } 

Bad things (tm) will happen if you set m_angle directly as above; the angle will become invalid. You can simply convert the angle to your own type so that it is impossible to invalidate, thereby eliminating the problem.

0
source