Reputation: 17
Hi I was reading on the microsoft website when I found this, can someone please explain why this is bad programming, because I see it in the way of doing more in less lines of coding. Ps I am still a novice in classes...
It is a bad programming style to change the state of the object by using the get accessor. For example, the following accessor produces the side effect of changing the state of the object every time that the number field is accessed.
private int number;
public int Number
{
get
{
return number++; // Don't do this
}
}
Upvotes: 1
Views: 1527
Reputation: 28272
There are perfectly valid reasons to modify the state of an object on get accessors, and that's why it's permitted (for example, for statistical purposes):
For example:
private int _timesMyCustomerWasAccessed;
private Customer _myCustomer;
public Customer MyCustomer
{
get
{
_timesMyCustomerWasAccessed++;
return _myCustomer;
}
}
That said, it's generally bad practice to make state changes on the object that affect how this object is observed from the outside (much worse if the change applies to the results of that very same property).
It's a syntax thing: get accessors are not supposed to have side effects, because they look weird and makes the code hard to understand.
In your example code. Imagine this code accessing the property:
Console.Write(String.Format("{0} {1} {2},
myObject.Number,
myObject.Number,
myObject.Number));
The output would be:
0 1 2
If you try to read that code, except if the object's side effects are perfectly documented, what would make you think that myObject.Number
would give different results every time?
For your particular case, it would make much more sense if you had a method:
public int GetNumberAndIncrease()
{
return number++;
}
And when you read that code:
Console.Write(String.Format("{0} {1} {2},
myObject.GetNumberAndIncrease(),
myObject.GetNumberAndIncrease(),
myObject.GetNumberAndIncrease()));
The output is perfectly understandable by just reading that code
Upvotes: 3
Reputation: 1972
As given in your example, lets assume a situation.
You are developing an API in which you are providing the size of the collection(Number) you have restricted to use by the user, but you are allowing the user to see how many contents/items are there in the collection.
Now one has to get the counts at three different places- and in that case you are increasing the number every time he gets it, that is absolutely wrong.
Hope you got the point, why its a bad programming style.
Upvotes: 0
Reputation: 6390
Certainly...
Get is read by most developers as a read only action, and people reading your code will not expect a Get property to be modifying any data. It's a convention, but when you think about it, one that makes sense - it helps us all to understand one another's code.
In the example you cite, one approach would be to make this into two calls - a get, followed by a set to increment the variable... Another would be to implement a method to increment the variable and return the result.
Upvotes: 3