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 23 of 53 Topic 4060 of 4400
Post > Topic >>

Re: Total Newbie Backward Compat Question

by Bart van Ingen Schenau <bart@[EMAIL PROTECTED] > Mar 12, 2008 at 06:40 PM

Chris ( Val ) wrote:

> On Mar 12, 4:58 am, Bart van Ingen Schenau <b...@[EMAIL PROTECTED]
>
> wrote:
>> Chris ( Val ) wrote:
>>
>>
>> > On Mar 11, 1:21 am, Richard Heathfield <r...@[EMAIL PROTECTED]
>
>> > wrote:
>> >> Chris ( Val ) said:
>>
>> >> <snip>
>>
>> >> > Why did you write setters that return values?
>>
>> >> int oldx = p.setx(newx);
>> >> dosomethingwith(p);
>> >> p.setx(oldx);
>>
>> >> is quite a useful thing to be able to do. You can get the same
>> >> deal with:
>>
>> > I think I can understand your reasons for it, but that is a hack in
>> > C++, and goes against everything we learn about OOP and writing
>> > good, maintainable and safe code.
>>
>> I am sorry, but I don't see how you come to that conclusion.
> 
> I'm sorry, but I have identified what I believe to be more than
> enough reasons to justify my position on this matter :-)
> 
> Please clarify for me (and our lurkers):
> Would you (and/or have you) personally used that code in production
> work?

I can't remember if I have ever used a setter that returned the old
value, but I do know that I have used functions that both change the
state and return a value. The most obvious one would be a function like
  int stack::pop();
that pops a value of a stack and returns the popped value.

To me, there is no fundamental difference with the code Richard
presented, so I would not hesitate to use it in production code.

> 
> If not, would you defend it's inclusion in the design of production
> code,
> whether by you or anyone else on the coding team?
> 
>> 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.
>> 2. This return value is not the sole means of retrieving the current
>> value.
>>
>> For Richard's example, the second condition is definitely matched and
>> the first might be debatable if we knew more about the context in
>> which the point class will be used. For now, I would just accept it.
> 
> Well raw pointers and changing owner****p according to Richard'
> function
> specification can fall under these rules too, but is that a good
> thing?

They can, and therefor are these not the only rules used to validate a
design.

> 
>> > 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, then I would strongly
>> prefer a return value over an exception to indicate failure, unless
>> there is reason to believe that the immediate caller will not be able
>> to handle the problem.
> 
> Well, you don't have a boolean return value here...
> 
>    if( p.setx( 500 ) )
>       std::cout << "Point plotted.\n";
>    else
>       std::cout << "Error: - Off the canvas.\n";
> 
> ...and since you sup****t Richard' example as being reasonable
> and acceptable code, what will you do here?

The same as I would do if setx() does not return anything.
I would request an interface change for the class.
Probably, I would also need an additional interface to set the extent of
the canvas.
Otherwise, Richard's version would not have survived the first round of
reviews because it has a silent failure mode. (It did not tell the
caller about an inability to fulfil the request.)

> 
> How would you get the "SetterGetter" to re****t the success or
> failure of the operation?

If setting can fail, then re****ting the failure is more im****tant than
the convenience of returning the old value.
Richard's code is acceptable, but it mostly remains a convenience
option.

> 
> --
> Chris Val

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
localhost-V2008-12-19 Wed Jan 7 10:26:15 PST 2009.