I am getting used to see properties implemented this way:
So you have your private members, and you created the properties for those, so you are happy and go on with your life. But I ask, Is there anything wrong in what we see here? Take a minute to think about it.
You see, the purpose of the properties is to protect the private members. But if there is absolutely no code in the properties getter or setter, then we are not really protecting anything, we just pass the values to the private member as they come.
This is a common mistake, we create a class and immediately, almost unconsciously, we create the properties for our private fields. Because we were taught this way. It will not cause any exception and certainly you can use it without problems is you have validations elsewhere, But then you are not taking advantage of one of the purpose of properties.
Now suppose our age variable has to have valid range, Or that for reporting purposes you have to see a 'Not Available' when a person does not have an address registered. You could do this:
See, now we are really using the properties for something meaningful. This is just a small example. What do you think about that ? It certainly got ME thinking.
No comments:
Post a Comment