Re: Internal State vs. Navigating Associations



"Matt McGill" <matt.mcgill@xxxxxxxxx> wrote:

As a newcomer to OO design, I've been wrestling lately trying to wrap
my mind around the "Tell, Don't Ask" principle and the law of Demeter
for methods. I have been trying to avoid query/decision operations of
this nature:

Bar bar = foo.getFrozzledBar();
if (bar.isFranted()) // do something else

under the assumption (here I am trying to apply "Tell, Don't Ask") that
in this example foo's internal state (bar) is being exposed. Instead I
try to replace the above with a single, more meaningful method on foo,
containing the conditional in some form.

The above does break the LoD but doesn't necessarally break "Tell, Don't
Ask", that all depends on what "do something else" actually does.

Actually, it might not even break LoD, depending on if bar is a newly
constructed object or not.

The question this has prompted me to ask: in what situations is bar a
part of foo's internal state, and in what situations is it really just
an object to which foo has an association?

Obviously this isn't a cut and dried type question. I will say this
though, that object is part of the internal state of *something* and
that thing is the only thing that should be messing with it. So if it is
just an association, then what is it part of the internal state of?

If bar *is* just an
associated object, is it then OK to navigate the association in the
above fashion?

If bar is just an associated object, then it shouldn't be necessary to
navigate the association in the above fashion. :-)


Quick review just to make sure we are on the same page...

"Tell don't Ask" means you should not have to query the state of an
object to see if you have permission to send a particular message to
that object.

"Law of Demeter" means you should only be changing the state of objects
by calling methods on the object whos state you want to change.

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer have anyone to discuss your doubts with,
nor any ability to discuss them.
.



Relevant Pages

  • Re: Internal State vs. Navigating Associations
    ... Tell (dont ask) bar to do something to itself. ... in this example foo's internal state is being exposed. ... try to replace the above with a single, more meaningful method on foo, ...
    (comp.object)
  • Internal State vs. Navigating Associations
    ... Bar bar = foo.getFrozzledBar; ... in this example foo's internal state is being exposed. ... try to replace the above with a single, more meaningful method on foo, ...
    (comp.object)
  • Re: Internal State vs. Navigating Associations
    ... The question this has prompted me to ask: in what situations is bar a ... part of foo's internal state, and in what situations is it really just ... it's fine to navigate the association in this way as long as you ... impact the foo instance as well. ...
    (comp.object)
  • Re: Legal Abuse Syndrome (LAS)
    ... Bar, an out-of-control agency ... ... Would you agree that since all three branches of our government are ... "THERE IS NO FEDERAL COMMON LAW, AND CONGRESS HAS NO POWER TO DECLARE ... HE INTERNATIONAL BANKERS' ...
    (misc.legal)
  • Re: Casey endorses Obama (nbc)
    ... Rose Law Firm has worked for me, I've practiced law in three states, ... passed two bar exams, and been in the legal field for over 25 years. ... She's not that brilliant. ... Interest Research Group before moving to Chicago in 1985 to take a job ...
    (rec.music.artists.springsteen)