Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C - C++ Learning > Re: Total Newbi...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 32 of 53 Topic 4060 of 4370
Post > Topic >>

Re: Total Newbie Backward Compat Question

by Bart van Ingen Schenau <bart@[EMAIL PROTECTED] > Mar 14, 2008 at 09:34 PM

Daniel T. wrote:

> On Mar 12, 12:43 pm, Bart van Ingen Schenau <b...@[EMAIL PROTECTED]
>
> wrote:
>> Daniel T. wrote:
>> > Bart van Ingen Schenau <b...@[EMAIL PROTECTED]
> wrote:
>> > > Chris ( Val ) wrote:
>>
>> > Code example under consideration:
>>
>> > class Foo {
>> > public:
>> >    int getValue() const;
>> >    int setValue( int v );
>> >       // ensure: result == old getValue() && getValue() == v
>> > };
>>
>> > > I would regard such a setter that returns the previous value as
>> > > perfectly valid OOP, if two conditions are met:
>> > > 1. Having the setter at all does not blatantly violate the
>> > > principles of OOD.
>> >
>> > It blatantly violates the Command-Query Separation principle.
>>
>> Can you give me a pointer to a resource where it is stated that this
>> is a fundamental principle of OOD?
> 
> Meyer, Bertrand. Object-Oriented Software Construction, 2nd Edition.
> Prentice Hall, 1997
> 
> Good book. Now, are you going to pull the "no *true* Scotsman" fallacy
> on me?

No, because I am starting to think that there might not *be* a "true
Scotsman" here.
To my knowledge, that book does not form an authorative (sp?) definition
of OO, nor do my sources. I don't know if there is any authorative
definition around, so I can't tell if the Command-Query separation is
listed there as a fundamental principle.

> 
>> And if I understand the principle correctly, a setter like
>>   bool setValue( int v );
>> would also be in violation, because it both changes the state and
>> returns information about the state.
> 
> Depends on what the ensures clause is, but if the function's post-
> condition is "result == true ? getValue() == v : getValue() == old
> getValue()" then yes, it breaks the rule.
> 
>> Even throwing an exception would violate a strict interpretation of
>> this principle.
> 
> An exception should only be thrown if the contract cannot be
> guaranteed. So no, an exception tells you nothing about the state of
> the object.

Depending on the exception-safety guarantee of the function, it actually
does tell something about the state, in the same way that the boolean
return value above does.

> 
>> > However, the method *is* the sole means of retrieving that which it
>> > retrieves (the value of getValue() before the last time setValue()
>> > was called,) and it only works once. Referential transparency is
>> > broken.
>>
>> It does not work only once.
>> You can call setValue() as often as you want and each time the call
>> returns the value as it was prior to that call.
> 
> Really?
> 
>     if ( p.setValue( 7 ) > 10 )
>         cout << "old value was " << ??? << '\n';
> 
> You can't determine the old value at the "???" point.

Nor can you if setValue did not return the old value, so that argument
does not hold.

> Once you call 
> setValue, you have no means of finding out from the object what the
> return value was (or as you state below, there is no corresponding
> getter for retrieving the value without changing it.) You would be
> forced to create a tem****ary to hold the value.

And is setValue() did not return a value, you would also need to use a
tem****ary or rewrite the code in a different order.
I don't see what your problem is with that.

> 
>> Referential transparency is indeed broken, because setValue() does
>> not claim to be a query operation.
> 
> The OP specifically used it to query the state of the object, he
> returns the value spicifically so he *can* make the query. Of course
> the function claims to be a query operation.

But the function also claims to change the state of the object and a
Query does not do so. Therefor, it can not be a Query.

> 
>> To me, it is acceptable to return the old value from a setter if
>> - having the setter at all does not violate encapsulation, and
>> - there is a corresponding getter for retrieving the value without
>> changing it.
> 
> There is no corresponding getter for retrieving the value that
> setValue returns without changing it. The value that 'setValue'
> returns cannot be retrieved though any getter.

Thanks for trying, but this kind of argument is not going to convince
me.

I think it is best if we agree to disagree on this issue.

> 
> Again, by your own rules, the function isn't acceptable.
> 
>> > > > Some setters are written to return a 'boolean' value to signal
>> > > > either success or failure, but even that is debatable in some
>> > > > cir***stances, when compared to throwing an exception.
>> > >
>> > > If it is actually a setter, and not a function that happens to
>> > > set some values as part of a larger operation,...
>> >
>> > That is getting into implementation details that the interface
>> > shouldn't expose...
>>
>> Just about any interface exposes this kind of information, if only by
>> the naming of the identifiers.
> 
> Really?
> 
> class Foo {
> public:
>     virtual int getX() const;
>     virtual void setX( int x );
>         // ensure: getX() == x
> };
> 
> How does the above function signature tell you *anything* about
> whether it is a "real setter" or just a function that happens to set
> some values as part of a larger operation?

The name "setX" implies that only the conceptually simple operation of
assigning a new value to the property X will take place. Therefor, I
would call that a "real setter".

A function called "drawPicture" on the other hand implies that a more
complex operation takes place.

Bart v Ingen Schenau
-- 
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://c-faq.com/
c.l.c++ FAQ: http://www.para****ft.com/c++-faq-lite/
 




 53 Posts in Topic:
Total Newbie Backward Compat Question
Hal Vaughan <hal@[EMAI  2008-03-10 05:38:03 
Re: Total Newbie Backward Compat Question
Richard Heathfield <rj  2008-03-10 05:48:22 
Re: Total Newbie Backward Compat Question
Hal Vaughan <hal@[EMAI  2008-03-10 05:49:42 
Re: Total Newbie Backward Compat Question
Richard Heathfield <rj  2008-03-10 06:24:13 
Re: Total Newbie Backward Compat Question
Hal Vaughan <hal@[EMAI  2008-03-11 14:35:17 
Re: Total Newbie Backward Compat Question
Crazy c <chrisNchrist@  2008-03-10 04:12:17 
Re: Total Newbie Backward Compat Question
Richard Heathfield <rj  2008-03-10 12:05:13 
Re: Total Newbie Backward Compat Question
"Daniel T." <  2008-03-11 06:37:11 
Re: Total Newbie Backward Compat Question
"Chris ( Val )"  2008-03-10 06:15:26 
Re: Total Newbie Backward Compat Question
Richard Heathfield <rj  2008-03-10 14:21:14 
Re: Total Newbie Backward Compat Question
"Chris ( Val )"  2008-03-11 02:15:39 
Re: Total Newbie Backward Compat Question
Richard Heathfield <rj  2008-03-11 10:29:42 
Re: Total Newbie Backward Compat Question
Bart van Ingen Schenau &l  2008-03-11 18:58:25 
Re: Total Newbie Backward Compat Question
"Daniel T." <  2008-03-12 06:26:50 
Re: Total Newbie Backward Compat Question
Bart van Ingen Schenau &l  2008-03-12 17:43:08 
Re: Total Newbie Backward Compat Question
"Chris ( Val )"  2008-03-11 04:21:32 
Re: Total Newbie Backward Compat Question
Richard Heathfield <rj  2008-03-11 14:56:52 
Re: Total Newbie Backward Compat Question
Anand Hariharan <mailt  2008-03-11 19:31:54 
Re: Total Newbie Backward Compat Question
"Chris ( Val )"  2008-03-12 06:15:16 
Re: Total Newbie Backward Compat Question
"Chris ( Val )"  2008-03-12 06:19:55 
Re: Total Newbie Backward Compat Question
"Chris ( Val )"  2008-03-12 06:27:42 
Re: Total Newbie Backward Compat Question
"Chris ( Val )"  2008-03-12 06:54:16 
Re: Total Newbie Backward Compat Question
Bart van Ingen Schenau &l  2008-03-12 18:40:28 
Re: Total Newbie Backward Compat Question
"Chris ( Val )"  2008-03-12 07:10:18 
Re: Total Newbie Backward Compat Question
Richard Heathfield <rj  2008-03-12 14:28:48 
Re: Total Newbie Backward Compat Question
Hal Vaughan <hal@[EMAI  2008-03-13 05:43:14 
Re: Total Newbie Backward Compat Question
Richard Heathfield <rj  2008-03-13 07:11:42 
Re: Total Newbie Backward Compat Question
Jerry Coffin <jcoffin@  2008-03-14 00:25:02 
Re: Total Newbie Backward Compat Question
Hal Vaughan <hal@[EMAI  2008-03-13 03:23:30 
Re: Total Newbie Backward Compat Question
Philip Potter <pgp@[EM  2008-03-13 15:57:05 
Re: Total Newbie Backward Compat Question
"Daniel T." <  2008-03-12 11:46:58 
Re: Total Newbie Backward Compat Question
Bart van Ingen Schenau &l  2008-03-14 21:34:35 
Re: Total Newbie Backward Compat Question
"Daniel T." <  2008-03-14 22:01:32 
Re: Total Newbie Backward Compat Question
Francis Glassborow <fr  2008-03-15 10:02:29 
Re: Total Newbie Backward Compat Question
Anand Hariharan <mailt  2008-03-12 18:36:13 
Re: Total Newbie Backward Compat Question
"Daniel T." <  2008-03-13 06:52:53 
Re: Total Newbie Backward Compat Question
Anand Hariharan <mailt  2008-03-12 23:36:10 
Re: Total Newbie Backward Compat Question
Micah Cowan <micah@[EM  2008-03-14 09:40:31 
Re: Total Newbie Backward Compat Question
"Daniel T." <  2008-03-13 10:23:19 
Re: Total Newbie Backward Compat Question
Bart van Ingen Schenau &l  2008-03-14 21:45:47 
Re: Total Newbie Backward Compat Question
"Daniel T." <  2008-03-15 07:14:33 
Re: Total Newbie Backward Compat Question
Francis Glassborow <fr  2008-03-15 16:14:23 
Re: Total Newbie Backward Compat Question
"Daniel T." <  2008-03-16 09:37:37 
Re: Total Newbie Backward Compat Question
"Chris ( Val )"  2008-03-18 05:04:15 
Re: Total Newbie Backward Compat Question
"Chris ( Val )"  2008-03-18 05:40:29 
Re: Total Newbie Backward Compat Question
Richard Heathfield <rj  2008-03-18 22:54:23 
Re: Total Newbie Backward Compat Question
"Chris ( Val )"  2008-03-18 07:12:06 
Re: Total Newbie Backward Compat Question
"Chris ( Val )"  2008-03-21 05:44:42 
Re: Total Newbie Backward Compat Question
Richard Heathfield <rj  2008-03-21 13:34:56 
Re: Total Newbie Backward Compat Question
Ben Bacarisse <ben.use  2008-03-12 18:05:26 
Re: Total Newbie Backward Compat Question
"Daniel T." <  2008-03-12 20:46:50 
Re: Total Newbie Backward Compat Question
Ben Bacarisse <ben.use  2008-03-13 01:57:08 
Re: Total Newbie Backward Compat Question
Ben Bacarisse <ben.use  2008-03-13 18:54:38 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Nov 21 11:33:08 CST 2008.