views:

228

answers:

2

How might I justify using a non-void method while not using any property getters? What is the distinction between these two concepts such that getters are evil but non-void methods are acceptable?

EDIT:

int CalculateSomething();

int Calculation { get; }

The fact that I can change the signature of CalculateSomething and pass values into it if I wanted to completely slipped my mind. So my question is changed to: Is the fundamental distinction between getters and non-void methods that arguments can be passed into non-void methods?

+3  A: 

Isn't the big difference, that you can't pass a variable to a getter?

a function:

private int Getyear(datetime date) { }

a property:

private DateTime GetDate {
    get {return _date};
}

you can't do that with a getter property can you?

Jack Marchetti
Well, we don't know what language we're talking about yet...
jeffamaphone
good point. hopefully he clarifies.
Jack Marchetti
I would still like to know from Sam in what context the getters are evil?
Sands
You can pass a variable to the indexer property: http://msdn.microsoft.com/en-us/library/2549tw02(VS.71).aspx
David
+9  A: 

Microsoft has guidance for choosing between properties and methods.

  • Consider using a property if the member represents a logical attribute of the type.
  • Do use a property, rather than a method, if the value of the property is stored in the process memory and the property would just provide access to the value.
  • Do use a method, rather than a property, in the following situations.
    • The operation is orders of magnitude slower than a field set would be. If you are even considering providing an asynchronous version of an operation to avoid blocking the thread, it is very likely that the operation is too expensive to be a property. In particular, operations that access the network or the file system (other than once for initialization) should most likely be methods, not properties.
    • The operation is a conversion, such as the Object.ToString method.
    • The operation returns a different result each time it is called, even if the parameters do not change. For example, the NewGuid method returns a different value each time it is called.
    • The operation has a significant and observable side effect. Note that populating an internal cache is not generally considered an observable side effect.
    • The operation returns a copy of an internal state (this does not include copies of value type objects returned on the stack).
    • The operation returns an array.
Craig Stuntz
Good read, thanks for that.
Matt Grande