You are headed to add a new piece of code. Unbeknownst to you, it already exists in the system. If you find out about the pre-existing code, you have had a HappyCollision. If you don't, duplication has stealthily propagated.
If you want more HappyCollisions, try to:
-
Follow the OneResponsibilityRule -- If every class has a clear responsibility, you'll know right where to go to add your current enhancement.
-
LotsOfShortMethods -- So that, when you go to add the method to the class it belongs in, you'll find out if someone else has already done it. If not, at least the next guy will.
-
Hold StandUpMeetings -- Tell everyone working on the codebase what you did the previous day. If you say, "I added this great BlizQuazzler class," someone else on the team might have the insight to ask you why you didn't just reuse the QazzleBlizzer class.
-
Have a CodingStandard -- KentBeck's SmalltalkBestPracticePatterns has some good advice along these lines. (TODO: summarize what he said.)
-
Use IntentionRevealingSelectors -- (Instead of comments)
-
Use StatelessObjects -- these tend to have more ad-hoc reusability than stateful ones
- Congratulations, you reinvented functions!
Meeting... perhaps advisable to circulate the idea before writing the code!
- CommitList for ConcurrentVersionsSystem?
It is surprising how often this can bite you in the behind even in a one man project. On my path to building the HtagLanguage system for web development I came across a spurious problem (bug). It seemed to stem from the urlencoding routines. Strangely when logging code was added the routine was obviously not entered. Eventually it turned out that there were no less then 3 different sets of urlencode and urldecode both as methods and as functions. And each one could be called depending on scoping and location in the class hierarchy. The highest grade of duplication and code smell! So maybe it would be useful to have standup meetings with oneself as well.
Is there XP advice I can follow in very large one-man "team" efforts? -- SvenNeumann
Check out ExtremeProgrammingForOne.
Take a look at PeeEmDee's cut and paste detection tool: CpdTool. Run this periodically or even just towards the end of an iteration. -- AdewaleOshineye
Also see other duplication-finding tools: CategoryDuplicationFindingTool
See AccidentalLinking in wiki.