[Tutor] Re: How can I avoid cut'n'paste in this case?

Andrei project5 at redrival.net
Sun Apr 17 02:19:32 CEST 2005


Nigel Rowe <tutor.fisheggs <at> xoxy.net> writes:

> >> I have two modules, both define the same set of classes (with differing
> >> implementations) and a number of utility functions that use those
> >> classes.
> >> The utility functions are identical (the differences I need are
> >> abstracted in the classes), so they are cut'n'pasted into both files.

How about putting all utility functions in a separate module and doing a "from P
import *" or "from C import *" at the top of that module, depending on some
command line parameter or whatever it is that determines which one should be
imported? It's not elegant by any means, but at least you don't have to
copy-paste code around any more and the changes to the existing code are minimal.

> >> How can I refactor these modules to avoid the code duplication?

I would say that the fact that there are two parallel implementations of the
same classes with small differences is worrying as well. There should be an
ancestor layer in there implementing common behavior.

> Maybe I'm going too far, trying to eliminate the cut'n'paste, but I've
> always thought it a bad smell.

It does indeed smell of a maintenance nightmare waiting to happen :). The bad
part is that it tends to get progressively worse. Imagine also what would happen
if you'd get even more of those similar modules. 

Yours,

Andrei





More information about the Tutor mailing list