Re: Kinds of methods
- From: "Daniel T." <postmaster@xxxxxxxxxxxxx>
- Date: Wed, 22 Mar 2006 18:21:39 GMT
In article <8sfUf.136$B_1.119@edtnps89>,
"Oliver Wong" <owong@xxxxxxxxxxxxxx> wrote:
"Daniel T." <postmaster@xxxxxxxxxxxxx> wrote in message
news:postmaster-6FCD75.09250822032006@xxxxxxxxxxxxxxxxxxxxxxxxxx
I just wrote this up for someone on one of the C++ newsgroups. Any
comments?
Member-functions can be categorized into one of 4 types:
modifier - a member-function that can change the external state of the
object. IE calling a modifier could cause one or more other
member-functions of the object's class to behave differently or
produce different output when called on this object, than they
otherwise would before the modifier was called.
producer - a member-function that creates an object for the caller to
use, or sets the state of an object the caller passed in.
accessor - a member-function that gives the caller a pointer/reference
to one of the object's member-variables such that modifying the value
contained in the pointer/reference will change the state of the
object as if a modifier was called.
provider - a member-function that gives to the caller a
pointer/reference to one of the object's member-variables such that
modifying the value contained in the pointer/reference will not
change the state of the object that the member-function was called on.
If it were up to me, I'd split "producers" which create new objects from
those which mutate objects passed in.
I did not do that because methods that mutate objects passed in
generally load them up with data/state that the caller wants. The
mutated object is a substitute for a return value.
Then, I'd merge the producer which
creates new objects with the provider which returns objects.
I kept them separate because the two methods have very different
semantics. Multiple calls to a provider return the same object, whereas
a producer creates a new object for each call.
I'd probably end up with something like:
Setter
Getter
Factory
Mutator
What is the difference between a "Setter" and a "Mutator" in your
scheme? What is the difference between a "Getter" and a "Factory"?
I could see changing the name of "producer" to "factory" in my list, but
I figured that the term already has a rather strong connotation of a
member-function that returns something that must be deleted, and I
wanted to include member-functions that return by value.
But then there are functions which just return a value based on its inputs,
without setting, getting, making, or mutating anything. E.g.
number function double(number n) {
return n * 2;
}
The above is a producer, it creates an object.
And then there are functions which produce side-effects in the system,
without affecting the objects themselves. E.g.
console.write("Hello World!");
I guess you could argue that this write() method is affecting the state of
the conceptual console (of which the console object is acting as a proxy
for), but then you'd have these "black hole setters", where you're setting
some state which you can never actually retrieve again.
I would indeed argue that, and I see no problem with "black hole
setters".
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
.
- Follow-Ups:
- Re: Kinds of methods
- From: Oliver Wong
- Re: Kinds of methods
- References:
- Kinds of methods
- From: Daniel T.
- Re: Kinds of methods
- From: Oliver Wong
- Kinds of methods
- Prev by Date: Re: UML-ish question
- Next by Date: Re: Kinds of methods
- Previous by thread: Re: Kinds of methods
- Next by thread: Re: Kinds of methods
- Index(es):
Relevant Pages
|