This post originated from an RSS feed registered with Agile Buzz
by Kevin Rutherford.
Original Post: the wrong duplication
Feed Title: silk and spinach
Feed URL: http://silkandspinach.net/blog/index.xml
Feed Description: kevin rutherford on agile software development and life in macclesfield
Here's something I've seen many times in code right across the spectrum, and which I caught myself doing just the other day. It seems to occur particularly often in codebases that weren't TDD'd, and which haven't been kept lean with regular refactoring...
Imagine a codebase in which one particular text string occurs frequently. The string may be some kind of message header, for example, or an XML tag (both of these examples are usually accompanied by another repeated string, representing the message footer or the closing tag). As the codebase grows, you notice the string popping up more and more frequently, until the "duplication" bell goes off. What to do?
It turns out we've all been taught the answer: Create a manifest constant (a #define or a static final String or whatever, depending on the language), thereby giving the common string a name, and replace all instances of the string by use of the manifest. There's even a name for this refactoring in Martin Fowler's catalogue: Replace Magic Number With Symbolic Constant. Duplication sorted, right? Wrong!