M
mike3
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'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?