Talk About Network

Google





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

Re: Total Newbie Backward Compat Question

by "Chris ( Val )" <chrisval@[EMAIL PROTECTED] > Mar 18, 2008 at 07:12 AM

On Mar 15, 9:02=A0pm, Francis Glassborow
<francis.glassbo...@[EMAIL PROTECTED]
> wrote:
> Daniel T. wrote:
> > Bart van Ingen Schenau <b...@[EMAIL PROTECTED]
> wrote:
> >> Daniel T. wrote:
> >>> 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:
> >>>>> =A0 =A0int getValue() const;
> >>>>> =A0 =A0int setValue( int v );
> >>>>> =A0 =A0 =A0 // ensure: result =3D=3D old getValue() && getValue()
=
=3D=3D v
> >>>>> };
> >>>> And if I understand the principle correctly, a setter like
> >>>> =A0 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 =3D=3D true ? getValue() =3D=3D v :
getValu=
e()
> >>> =3D=3D 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.
>
> > That depends on how you use exceptions. I don't use them that way,
some
> > people do.
>
> >>>>> 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?
>
> >>> =A0 =A0 if ( p.setValue( 7 ) > 10 )
> >>> =A0 =A0 =A0 =A0 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.
>
> > Sure it does, the argument is that setValue can only be used once to
> > retrieve the value it provides, the above showed that the argument is
> > true.
>
> This thread begins to smell of dogma. If someone advocated that setters
> should always/never return the prior value I would disagree. Whether
> they do or not should IMO depend on what their purpose is.

If it's a *toy* program, then fine, and from what I gather, the
name 'playpen' dooesn't re****t itself to be of enterprise quality
software :)

In all seriousness, the main issue is whether or not it is acceptable
for writing robust and scalable program code.

While we are at it, can you please explain in brief, what the
advantages
of having are, and why the C++ standards commitie introduced the
'const'
quallifier to member functions, as in: int GetX() const; ?


> In my graphics sup****t for novices I have a type called 'playpen' that
> represents a fixed and deliberately limited graphics window. Instances
> have member variables that 'remember' such things as the current scale,
> location of origin etc.

Having an undo capability is implemented in many graphical programs.

> As such things only control the way graphical data is displayed attempts
> to use out of range values simply saturate. Setters for these features
> all return their prior values so that code such as, for example:
>
> void foo(playpen & p){
> =A0 =A0 // get scale info from user
> =A0 =A0 try{
> =A0 =A0 int oldscale(p.scale(newvalue));
> =A0 =A0 // do something
> =A0 =A0 }
> =A0 =A0catch(...){
> =A0 =A0 =A0p.scale(oldscale);
> =A0 =A0}
> =A0 =A0p.scale(oldscale);
>
> }

What is the real name of the function you are describing above?
It's certainly not foo, and 'oldscale' is not even in scope.

Anyway, what's wrong with:

p.edit();

// Work with p...

p.commit(); or p.rollback(); ?

Where the old object is cloned internally.

> work. And the above is just off the top of my head to illustrate a
> mechanism. Yes It can be written with an extra line to recover the
> oldscale distinct from the line to set the new one and the class does
> have matching getters for each of its setters.

Why does it have matching getters?

Can you explain your thinking as to why you incor****ated them, only
to overlook them with an *AllInOne(*)" chameleon?

> IMHO any time that you have a situation where it is very likely that the
> programmer will wish to change state tem****arily and then restore the
> old state is one where a getter might legitimately return the prior
state.=


I would expect a getter to always legitimately return the *current*
state. If
you want store a specific state, then create another member function
to do
that, using the *getter* itself or clone the object if possible.

--
Chris Val
 




 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
localhost-V2008-12-19 Wed Jan 7 14:09:02 PST 2009.