Code Reuse via Cut 'n Paste

grantm on 2005-10-20T00:53:28

A bit of background...

Some years ago I attended a marketing presentation for the (then) shiny new release of MS Visual Studio. The rep proudly demoed a library function which "allowed easy copying/pasting of frequently used code snippets" which was supposedly a good idea because it would "promote code reuse". I remember thinking at the time That sounds like a maintenance nightmare in the making.

A different bit of background...

A common approach to adding functionality to a working system is:

  1. Copy the existing functionality (one or more of: subroutines, modules, templates, tables etc) which most closely resembles the desired new functionality
  2. Modify the copy to do the new thing
  3. Refactor aggresively to eliminate redundancy

Note, I didn't say it was a good way, just a common way. Unfortunately it's a particularly poor approach if step three is never executed.

This week in my life

The client requested a simple change. At various points in their app, objects displayed on screen are highlighted in a colour which indicates the states of four flags. The flags are all independant, so there is a specified priority order for applying colours. The client simply specified a change the priority order.

I am now into day four. I have edited 86 files and the patch (including context and other diff overhead lines) is now standing at 3359 lines.

And yes, I am executing step 3.


The Paste Light

dws on 2005-10-20T05:59:07

Some friends and I mused about the idea of an Eclipse plugin that would watch for pastes. After the second paste of the same code, the plugin would cause a light to go on or a buzzer to sound, signaling the need for a code review.