Possible Duplicate:
Properties vs Methods
For many situations it is obvious whether something should be a property or a method however there are items that might be considered ambiguous.
Obvious Properties:
Obvious Methods:
Ambiguous:
I suppose I would lean towards methods for the ambiguous, but does anyone know of a rule or convention that helps decide this? E.g. should all properties be O(1)? Should a property not be able to change other data (ColorSaturation might change R,G,B values)? Should it not be a property if there is calculation or aggregation?
Just from an academic perspective, (and not because I think it's a good idea) is there a reason not to go crazy with properties and just make everything that is an interrogation of the class without taking an argument, and everything that can be changed about the class with a single argument and cant fail, a property?
I typically convert a property to a function if it has one of the following behaviors
I´ve found some interesting text about this
MSDN | Properties vs Methods
EDIT
It says things like:
Use a property when
Use a method when
Another consideration is bindability. Most frameworks can bind only to properties. For example, if you want IsValid to be usable in binding (say, as a binding sourcee for the OK button's IsEnabled property), then it has to be a property rather than a method.
In deciding whether to use a property or a method you should also consider the ammount of work the method involves. I.e. if it is relatively cheap to retrieve the value make it a property, if it costly make it a method.
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