This is something that I've always wrestled with in my code. Suppose we have the following code:
public class MyClass {
private string _myVariable;
public string MyVariable {
get { return _myVariable; }
set { _myVariable = value; }
}
public void MyMethod() {
string usingPrivateMember = _myVariable; // method A
string usingPublicProperty = MyVariable; // method B
}
}
Which way is more correct - method A or B? I am always torn about this. Method A seems like it would be minutely faster, due to the fact that it doesn't have to go access a property before getting the real variable. However, method B is safer because if the getter for MyVariable gets business logic added to it, you are safe by always calling it, even if there is no current business logic.
What's the general consensus?
A special use case for private fields is immutable objects; this is very common in e.g. Java, examples are String and BigDecimal . These classes have no public setters at all, which guarantees that their objects, once created, will not change their state.
Private: The class members declared as private can be accessed only by the member functions inside the class. They are not allowed to be accessed directly by any object or function outside the class. Only the member functions or the friend functions are allowed to access the private data members of the class.
A private member variable or function cannot be accessed, or even viewed from outside the class. Only the class and friend functions can access private members.
Broadly speaking, public means everyone is allowed to access, private means that only members of the same class are allowed to access, and protected means that members of subclasses are also allowed. However, each language adds its own things to this.
Use the property.
I think the property should be wholly responsible for managing that field.
There are plenty of implementations where it won't matter, but there are lots where it does matter -- a lot. Plus, this can be a bit of a pain to track down, because it always looks right.
You'll go wrong calling the property far fewer times than calling the field, and where there are exceptions to this rule, document the rationale.
This would really depend on what you are accessing the property for. Consider the following two scenarios:
Scenario 1: you write a method to provide a common action on the data in the class:
// assume a hypothetical class Position
public class Circle
{
private int _radius;
private int _xpos;
private int _ypos;
public int Radius { get { return _radius; } }
public Position Center { get { return new Position(_xpos, _ypos); } }
public bool PointInCircle(Position other)
{
return distance(this.Center, other) < this.Radius;
}
}
Clearly the behavior of PointInCircle should be the same as if the user executed the code inside it. Therefore, it makes sense to use the public properties.
Scenario 2: you write a method to manipulate the underlying data. A good example of this is serialization. You would want to serialize the underlying data members as opposed to the values returned by property accessors.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With