Re: Tell, Don't Ask



In article <102np4un1wbjn$.16sfqscl8pu2v$.dlg@xxxxxxxxxx>,
"Dmitry A. Kazakov" <mailbox@xxxxxxxxxxxxxxxxx> wrote:

On Sun, 14 May 2006 01:30:00 GMT, Daniel T. wrote:

I have said before the method preconditions should not reference the
state of the object the method is called on which I think has the same
effect that Hunt and Thomas are aiming for...

An example is in order. In most example texts, Stack::pop() has a
precondition that Stack::empty() is false. We end up with code like this:

while not myStack.empty():
myStack.pop()

but the above is exactly what Hunt and Thomas are saying is bad
practice, we are querying the state of the object in order to decide
what to do with it. The precondition, in fact, *requires* us to make the
query (or otherwise "know" that the stack in not empty.)

Only a short remark. The above can be rewritten as:

try
{
while (1) myStack.pop();
}
catch (EmptyStack&)
{}

which would be "Tell. Don't Ask." So I think that your point about
precondition was important, because, ignoring for a while merits of both
designs, the latter obviously requires the precondition relaxed to true.

It could also be rewritten as:

while ( myStack.pop() ) { }

where myStack.pop() returns 0 if (and only if) myStack is empty.

Both your code and the above run afoul of the principle of Command/Query
separation though.
.



Relevant Pages

  • Re: Tell, Dont Ask
    ... The precondition, in fact, *requires* us to make the ... The user of Stack no longer has the details of the pre-condition. ... Hunt and Thomas said is against the "tell don't ask" concept. ...
    (comp.object)
  • Re: Tell, Dont Ask
    ... The precondition, in fact, *requires* us to make the ... Hunt and Thomas said is against the "tell don't ask" concept. ... defining contracts in terms of query ops. ...
    (comp.object)
  • Re: Tell, Dont Ask
    ... On Sun, 14 May 2006 01:30:00 GMT, Daniel T. wrote: ... effect that Hunt and Thomas are aiming for... ... The precondition, in fact, *requires* us to make the ...
    (comp.object)
  • Re: Tell, Dont Ask
    ... The precondition, in fact, *requires* us to make the ... Hunt and Thomas said is against the "tell don't ask" concept. ... I'm asking whether the pre-condition is met, ...
    (comp.object)
  • Re: C programming environment
    ... emacs. ... "The Pragramtic Programmer" (Hunt, Thomas) where they mention that you ...
    (Ubuntu)