I've inherited some research code where there's already a fair amount of code duplication: on several occasions, the original author duplicated a file and changed minor things to calculate a variation of the original problem. I'm tasked with making modifications to calculate a different variation. Following the style that's already going on, I could complete the task by duplicating and modifying 4-5 files. But, I started doing this and felt I was just making an already somewhat disorganized project, worse.
To be slightly more specific: say there's some class (this is C++) Foo whose definition makes use of some other class Bar. One possibility is to copy Bar to Bar2 and make minimal changes, and then copy Foo to Foo2 with some minimal changes including changing all instances of Bar to Bar2.
Alternatively, I could make use of templates: change Foo to FooX<BarX>
, and then have Foo and Foo2 be aliases for FooX<Bar>
and FooX<Bar2>
with a handful of redefined functions. To me this feels like the right thing to do, but my worry is mostly that it increases the level of abstraction (there's already some templating going on so this will cause nested templates FooX<BarX<Baz>>
) and it's unlikely there will be a third use of FooX
.
I guess the general question it to what extent one can excuse code duplication by (1) reduction of questionably useful abstractions; (2) following somewhat questionable preexisting patterns.