Re: Tell, Don't Ask
- From: "Daniel T." <postmaster@xxxxxxxxxxx>
- Date: Sun, 14 May 2006 14:10:21 GMT
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.
.
- References:
- Tell, Don't Ask
- From: Squeamizh
- Re: Tell, Don't Ask
- From: Daniel T.
- Re: Tell, Don't Ask
- From: Dmitry A. Kazakov
- Tell, Don't Ask
- Prev by Date: Re: Programming to an Interface
- Next by Date: Re: Question on LSP
- Previous by thread: Re: Tell, Don't Ask
- Next by thread: Re: Tell, Don't Ask
- Index(es):
Relevant Pages
|