in reply to Refactoring tools for copy/paste jobs

I would imagine that your line-based approach will - once you exclude the standard-for-your-coding-style lines above - show you most of your copy-and-paste.

I'd actually start from the other end, looking for infrequently-occuring-but-nonunique lines. A line which is used in exactly 2 or 3 places is more likely to be part of a block of cut and paste than one used in 100.

When you are refactoring, a nice option you have in perl is to make use of first class functions/closures/anonymous subs.

This allows you to have more "fuzzy matching" between blocks of similar-but-not-identical code. You can pull out the shared boilerplate as a new method/sub and pass into it a closure which does the 'bit which is different for each occurence'.

It's a lightweight alternative to putting together an inheritance hierarchy to share code, which you may see in design patterns etc. For example:

sub print_tree { my $tree = shift; print_tree($tree->left); print $tree->value; print_tree($tree->right); } sub sum_tree { my $tree = shift; return sum_tree($tree->left) + $tree->node + sum_tree($tree->right); }
could become (sorry, this is untested - also, this exact example might be better as a Tree class, which probably already exists in CPAN, etc etc., but I hope the point survives):
sub walk_tree { my $tree = shift; my $per_node = shift; walk_tree($tree->left); $per_node->($tree->node); walk_tree($tree->right); } sub print_tree { my $tree = shift; walk_tree($tree, sub { print $_[0]; }); } sub sum_tree { my $tree = shift; my $total = 0; walk_tree($tree, sub { $total += $_[0]; }); return $total; }

Replies are listed 'Best First'.
Re^2: Refactoring tools for copy/paste jobs
by planetscape (Chancellor) on Oct 09, 2006 at 19:08 UTC
Re^2: Refactoring tools for copy/paste jobs
by rhesa (Vicar) on Oct 09, 2006 at 15:55 UTC
    I'd actually start from the other end, looking for infrequently-occuring-but-nonunique lines. A line which is used in exactly 2 or 3 places is more likely to be part of a block of cut and paste than one used in 100.

    That's a great suggestion, I hadn't thought of that yet. Thanks!

    I'm well aware of the various refactoring techniques, and I do use both OO and functional approaches where appropriate. Your example is illustrative enough though, although your walk_tree should probably read:

    sub walk_tree { my $tree = shift; my $per_node = shift; walk_tree($tree->left); $per_node->($tree->node); walk_tree($tree->right); }
      Thanks for the correction (which I've incorporated into the original node - this reply is partly to record that fact).

      And I'm sorry if I seemed to imply that you might not be aware of such techniques - no offence intended.