I think that the controversy has chiefly stemmed from the fact that the
emphasis here has been misplaced. The emphasis of this thread has been on
what a property should or should not *do.* In fact, I believe the
controversy can be avoided altogether (by reasonable minds) by shifting the
emphasis to what a property *is*. This is OOP, and OOP is all about
abstraction. A property is called a "property" because it represents a
"property of an object." Now, we all know that an object is not really an
object, but an abstraction of an encapsulation of process and data that are
treated as part of the same "thing" in order to make it easier for us
humans, who more naturally think abstractly about almost everything, to work
with that process and data. We all know what the word "property" has meant
since long before it was used in programming terms: " A characteristic trait
or peculiarity, especially one serving to define or describe its possessor."
Like the words "class," "type," and "object," the word "property" was
employed to describe the abstraction that the programming definition of
"property" represents.
In the same vein, the word "method" is defined in the English language as "A
means or manner of procedure, especially a regular and systematic way of
accomplishing something." Therefore, the appellations of these 2 terms
should tend to indicate when each rule should be employed. The confusion
arises because in programming, a property has a get and/or set "method" to
return the "value" of the property. However, is this not true in real life
as well? For example, a human being has a "property" called "weight." How is
the weight known? It is not stored as a number somewhere in the person. It
is obtained by a "method" which employs using a measuring device of some
kind to weigh the person.
Of course, in the realm of programming, as in real life, it is not optimal
to calculate any property every time the state is required. Some properties
are stored, and calculated less often, to save processor work. The same can
be said of real-life properties. Does a person step on a scale every time
their weight is requested of them? No, they weigh themselves occasionally,
and report the results, which are accurate enough for general purposes, but
may not be exact, when asked. If an accurate measurement is required, the
person can step onto the scales again, executing the "method" by which the
"property" is obtained.
In other words, a "property" in programming is appropriate for use in
obtaining what can be abstractly termed as "a characteristic or trait" of an
object, while a "method" is appropriate for use when describing a "means or
manner of procedure." That is, regardless of what sort of calculation may be
involved, it is the abstraction which is important. Creating a property for
any method that takes no arguments would be silly. Creating a method for any
process that is required to obtain data is equally silly. The only "correct"
definition is that for which the terms were originally employed - as
abstractions in an abstract paradigm.
I hope this serves to put out those flames.
--
HTH,
Kevin Spencer
Microsoft MVP
Professional Numbskull
Show me your certification without works,
and I'll show my certification
*by* my works.
"Bruce Wood" <br*******@cana da.com> wrote in message
news:11******** **************@ g10g2000cwb.goo glegroups.com.. .
Nick,
Fortunately for both of us you don't have to work with my software, nor
I with yours. :-)
I don't recommend writing a property that has to head off to the
database. It was an experimental thing which hasn't caused us any
problems here because the few of us working here understand what's
involved. It's become a "local idiom," if you will. The first access is
slow; all subsequent accesses are lightning fast.
However, in retrospect, I would probably have made these methods, even
though it (unfortunately) leaks information about the implementation
into the interface. Certainly I would make them methods if our software
were ever to be used outside our tiny shop.
We're going to rewrite the whole thing soon anyway... the first effort
was a throwaway.
Anyway, that was an extreme example. The point was (and still stands)
that it's sometimes not a simple thing to decide between method and
property. O-O principles tell us "don't expose implementation details
in your public interface," while sensible .NET convention tells us
"properties should execute quickly." Sometimes those two goals are at
odds and you have to weigh each one off against the other.
"A property can never do any calculations" is silly, just as "a
property can do anything it likes" is silly. The truth is somewhere in
between.