Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Duplicated Code Woes

Reply
Thread Tools

Duplicated Code Woes

 
 
mike3
Guest
Posts: n/a
 
      06-12-2012
Hi.

I've heard that, and it seems like a pretty good rule, duplicated code
is bad. Duplicated code is harder to maintain -- if you update one you
better make sure you update the clones (and if you forget one --
BUG!!!!!), harder to read, etc. But in some cases, just what can you
do?

Like this little puppy I've got. It is supposed to implement
"saturation arithmetic" (i.e. where a number is "clamped" in certain
bounds, so that when it "overflows", the result is "clipped" at the
bounds.). I have two routines -- one to add, one to subtract -- but
they look kinda similar: they're the same basic structure but with
different function calls:

---

SaturationNumber<T> &operator+=(const T rhs)
{
if(DoesAdditionExceedMax(number, rhs, max))
number = max;
else if(DoesAdditionExceedMin(number, rhs, min))
number = min;
else
number += rhs;

return(*this);
}


SaturationNumber<T> &operator-=(const T rhs)
{
if(DoesSubtractionExceedMax(number, rhs, max))
number = max;
else if(DoesSubtractionExceedMin(number, rhs, min))
number = min;
else
number -= rhs;

return(*this);
}

---

It looks kind of duplicatey. Should one be worried about this? Yet it
seems there is no easy way to merge these since the function calls are
thoroughly different.

(You may wonder: why do we need a special function
"DoesAdditionExceedMax", etc.? Why not just "if a + b > max then..."?
The answer, of course, is that a + b may Overflow, and the routine is
designed to catch that. To write the checks directly in the ifs would
be a nasty complicated conditional, and so I move that to a separate
function to keep things clear.)

So, is this "duplicated code", as in the "bad thing" I worry about,
and if so, is there any better way to do this that doesn't just add
more complexity with no clear benefit? What would you do if you were
writing those routines?

 
Reply With Quote
 
 
 
 
goran.pusic@gmail.com
Guest
Posts: n/a
 
      06-12-2012
On Tuesday, June 12, 2012 8:51:09 AM UTC+2, mike3 wrote:
> Hi.
>
> I've heard that, and it seems like a pretty good rule, duplicated code
> is bad. Duplicated code is harder to maintain -- if you update one you
> better make sure you update the clones (and if you forget one --
> BUG!!!!!), harder to read, etc. But in some cases, just what can you
> do?
>
> Like this little puppy I've got. It is supposed to implement
> "saturation arithmetic" (i.e. where a number is "clamped" in certain
> bounds, so that when it "overflows", the result is "clipped" at the
> bounds.). I have two routines -- one to add, one to subtract -- but
> they look kinda similar: they're the same basic structure but with
> different function calls:
>
> ---
>
> SaturationNumber<T> &operator+=(const T rhs)
> {
> if(DoesAdditionExceedMax(number, rhs, max))
> number = max;
> else if(DoesAdditionExceedMin(number, rhs, min))
> number = min;
> else
> number += rhs;
>
> return(*this);
> }
>
>
> SaturationNumber<T> &operator-=(const T rhs)
> {
> if(DoesSubtractionExceedMax(number, rhs, max))
> number = max;
> else if(DoesSubtractionExceedMin(number, rhs, min))
> number = min;
> else
> number -= rhs;
>
> return(*this);
> }
>
> ---
>
> It looks kind of duplicatey. Should one be worried about this? Yet it
> seems there is no easy way to merge these since the function calls are
> thoroughly different.
>
> (You may wonder: why do we need a special function
> "DoesAdditionExceedMax", etc.? Why not just "if a + b > max then..."?
> The answer, of course, is that a + b may Overflow, and the routine is
> designed to catch that. To write the checks directly in the ifs would
> be a nasty complicated conditional, and so I move that to a separate
> function to keep things clear.)
>
> So, is this "duplicated code", as in the "bad thing" I worry about,
> and if so, is there any better way to do this that doesn't just add
> more complexity with no clear benefit? What would you do if you were
> writing those routines?


I would keep my eye on how often and how these change. If often, and in sync, then I would err towards one helper routine that does the work and parametrize moving parts.

I would not lose sleep over this detail. Duplicated code can be MUCH worse, and there's a point of diminishing return for ANYTHING.

Goran.
 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      06-12-2012
mike3 <(E-Mail Removed)> wrote:
> I've heard that, and it seems like a pretty good rule, duplicated code
> is bad. Duplicated code is harder to maintain -- if you update one you
> better make sure you update the clones (and if you forget one --
> BUG!!!!!), harder to read, etc. But in some cases, just what can you
> do?


In general that's true (and the longer the duplicated code, the worse).
However, there are situations where "unduplicating" it only makes things
worse (ie. rather than having two lean&clean functions that look almost
identical, you end up having one single function that's a mess full of
conditionals or other extraneous code, which isn't much better).

> SaturationNumber<T> &operator+=(const T rhs)
> {
> if(DoesAdditionExceedMax(number, rhs, max))
> number = max;
> else if(DoesAdditionExceedMin(number, rhs, min))
> number = min;
> else
> number += rhs;
>
> return(*this);
> }
>
>
> SaturationNumber<T> &operator-=(const T rhs)
> {
> if(DoesSubtractionExceedMax(number, rhs, max))
> number = max;
> else if(DoesSubtractionExceedMin(number, rhs, min))
> number = min;
> else
> number -= rhs;
>
> return(*this);
> }


Those two functions are so short that I'd say it's not worth the effort
to try to merge them. The resulting function would most probably be almost
as long as those two functions combined and/or ridden with conditionals
or complicated template metaprogramming.
 
Reply With Quote
 
Terry Richards
Guest
Posts: n/a
 
      06-12-2012
I already deleted the original message so excuse me for tagging this into
the wrong part of the tree.

How about sidestepping the entire issue:

SaturationNumber<T> &operator-=(const T rhs)
{
return(operator+=(-rhs));
}



 
Reply With Quote
 
mike3
Guest
Posts: n/a
 
      06-12-2012
On Jun 12, 7:53*am, "Terry Richards" <(E-Mail Removed)> wrote:
> I already deleted the original message so excuse me for tagging this into
> the wrong part of the tree.
>
> How about sidestepping the entire issue:
>
> SaturationNumber<T> &operator-=(const T rhs)
> * * {
> * * * return(operator+=(-rhs));
> * * }


I thought of that too, but that requires the range of the
SaturationNumber
to be perfectly balanced +/- - wise, i.e. -n to +n, but that might not
be the
case. The range can be made essentially anything. And you _couldn't_
have a range like that if T = unsigned int, for example.
 
Reply With Quote
 
Pavel
Guest
Posts: n/a
 
      06-13-2012
mike3 wrote:
> Hi.
>
> I've heard that, and it seems like a pretty good rule, duplicated code
> is bad. Duplicated code is harder to maintain -- if you update one you
> better make sure you update the clones (and if you forget one --
> BUG!!!!!), harder to read, etc. But in some cases, just what can you
> do?
>
> Like this little puppy I've got. It is supposed to implement
> "saturation arithmetic" (i.e. where a number is "clamped" in certain
> bounds, so that when it "overflows", the result is "clipped" at the
> bounds.). I have two routines -- one to add, one to subtract -- but
> they look kinda similar: they're the same basic structure but with
> different function calls:
>
> ---
>
> SaturationNumber<T> &operator+=(const T rhs)
> {
> if(DoesAdditionExceedMax(number, rhs, max))
> number = max;
> else if(DoesAdditionExceedMin(number, rhs, min))
> number = min;
> else
> number += rhs;
>
> return(*this);
> }
>
>
> SaturationNumber<T> &operator-=(const T rhs)
> {
> if(DoesSubtractionExceedMax(number, rhs, max))
> number = max;
> else if(DoesSubtractionExceedMin(number, rhs, min))
> number = min;
> else
> number -= rhs;
>
> return(*this);
> }
>
> ---
>
> It looks kind of duplicatey. Should one be worried about this? Yet it
> seems there is no easy way to merge these since the function calls are
> thoroughly different.
>
> (You may wonder: why do we need a special function
> "DoesAdditionExceedMax", etc.? Why not just "if a + b > max then..."?
> The answer, of course, is that a + b may Overflow, and the routine is
> designed to catch that. To write the checks directly in the ifs would
> be a nasty complicated conditional, and so I move that to a separate
> function to keep things clear.)
>
> So, is this "duplicated code", as in the "bad thing" I worry about,
> and if so, is there any better way to do this that doesn't just add
> more complexity with no clear benefit? What would you do if you were
> writing those routines?
>


How are your Does...() functions implemented? Do they try to calculate the
result in a register and check CPU flags by any chance? Then maybe you have
over-encapsulated your algo already.

-Pavel
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      06-13-2012
Pavel <(E-Mail Removed)> wrote:
> How are your Does...() functions implemented? Do they try to calculate the
> result in a register and check CPU flags by any chance? Then maybe you have
> over-encapsulated your algo already.


Did you use a bullshit generator to come up with that paragraph?
 
Reply With Quote
 
Pavel
Guest
Posts: n/a
 
      06-14-2012
Juha Nieminen wrote:
> Pavel <(E-Mail Removed)> wrote:
>> How are your Does...() functions implemented? Do they try to calculate the
>> result in a register and check CPU flags by any chance? Then maybe you have
>> over-encapsulated your algo already.

>
> Did you use a bullshit generator to come up with that paragraph?

You surely used all your good manners to come up with yours.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with deployment project using duplicated folder names =?Utf-8?B?QW5kcmU=?= ASP .Net 0 10-27-2005 02:46 AM
Duplicated records in a report from two tables =?Utf-8?B?SXNhYmVsIFB1aWdkZXZhbGw=?= ASP .Net 1 04-06-2005 01:59 PM
Re: Cookies only being duplicated landeiro@gmail.com ASP .Net 0 01-07-2005 10:54 AM
NTLM and many duplicated requests Tom ASP .Net 6 10-17-2004 11:30 AM
how to avoid duplicated instance Erica Java 5 02-26-2004 06:07 AM



Advertisments