Should I use an object property or internal variable when encoding inside an object?

I am clearing some code in a class that has a setable property, with a local variable containing a value.

Some parts of the code are currently called externally to get the value, rather than using its own property, other parts sometimes use the property itself, while others use a local variable.

In this case, the property is simply set using the factory method, which creates an object of this class, so the property always matches the variable. However, I am not sure that I should assume that this is so in the code that I am writing, as this makes it difficult in the future to add logic to the getter method.

Is this something that can be right, or am I forgetting something basic in my OO principles?

+4
source share
4 answers

To reduce complexity and maintainability.

The first thing you need to do is to reduce the number of places in which the item exists (which, as you seem to be doing).

Then you should use any language tricks to reduce the code size. For exmaple in some versions of C #, you do not need to declare fields to store property values.

As soon as you do this, you can choose any of these methods - performance optimization does not occur due to the other. But be consistent. This will increase maintainability.

Personally, I always prefer to use properties where I can.

+1
source

Use Auto-updated properties and add only a private variable and optional get / set code if you need later.

eg.

public int CustomerID { get; private set; } 
0
source

I use properties, except for the fields that I mark as readonly , which means that they get their constant value only through the initialization / constructor of the objects.

Properties for me are easier to maintain if any requirements of your application change in the future. You just need to add the code in one place.

0
source

From an OO perspective, I think there are two types of properties. (Note: this only applies inside the class. Class users should not be aware of this difference.)

Some properties are truly independent classes contained in the main class. (The "main" class is the class in question, not necessarily the one that runs the program.) They should be specified only through the property, even in the main code of the class. Looking at its methods of obtaining and installing, you must tell you the whole story of the property. They are not integral parts of the main object, but rather related to information similar to public fields, but more secure.

Another view is an integral part of the main object. In extreme cases, they may not even have a connected area; when called, a getter can collect a value with calculations involving many fields. When there is one field, the internal code that reads and writes to it can find a getter, and the setter will make too many changes. (An object can track a value starting at 5. Over time 10, you can subtract and add 12. At this point, the value should be 7. But the getter may never want to admit that the outside world has a value less than zero, so when the value is -5, it will return 0.)

I would decide what type of property each of them is, and then go one way or another. Do not share the difference. Either all links must be connected to the object itself, or all links must be in the field or fields behind it. (Except that it is best to refer to the property directly, rather than duplicating the code in getter and setter. However, this is not very convenient for me.)

Under the "outside call", I see that there is a normal method that returns the value of a field. That might make sense. To be intuitive, the property should behave as possible as a public field, where the regular method can make changes. (Consider the DistanceInFeet property and the GetDistanceInMeters () method). And it would be advisable to call the Get method, rather than duplicating its code. This means that the common property is of the second type, an integral part of the object.

So, the code you are clearing might be right, OO-wise - but I suspect not.

0
source

Source: https://habr.com/ru/post/1415511/


All Articles