Los Techies : Blogs about software and anything tech!

Refactoring Day 15 : Remove Duplication


A day late on this one. Sorry about that!

This is probably one of the most used refactoring in the forms of methods that are used in more than one place. Duplication will quickly sneak up on you if you’re not careful and give in to apathy. It is often added to the codebase through laziness or a developer that is trying to produce as much code as possible, as quickly as possible. I don’t think we need anymore description so let’s look at the code.

   1: public class MedicalRecord
   2: {
   3:     public DateTime DateArchived { get; private set; }
   4:     public bool Archived { get; private set; }
   5:  
   6:     public void ArchiveRecord()
   7:     {
   8:         Archived = true;
   9:         DateArchived = DateTime.Now;
  10:     }
  11:  
  12:     public void CloseRecord()
  13:     {
  14:         Archived = true;
  15:         DateArchived = DateTime.Now;
  16:     }
  17: }

We move the duplicated code to a shared method and voila! No more duplication. Please enforce this refactoring whenever possible. It leads to much fewer bugs because you aren’t copy/pasting the bugs throughout the code.

   1: public class MedicalRecord
   2: {
   3:     public DateTime DateArchived { get; private set; }
   4:     public bool Archived { get; private set; }
   5:  
   6:     public void ArchiveRecord()
   7:     {
   8:         SwitchToArchived();
   9:     }
  10:  
  11:     public void CloseRecord()
  12:     {
  13:         SwitchToArchived();
  14:     }
  15:  
  16:     private void SwitchToArchived()
  17:     {
  18:         Archived = true;
  19:         DateArchived = DateTime.Now;
  20:     }
  21: }

This is part of the 31 Days of Refactoring series. For a full list of Refactorings please see the original introductory post.

Kick It on DotNetKicks.com
Posted Aug 15 2009, 06:29 AM by schambers

Comments

Gunnar Peipman wrote re: Refactoring Day 15 : Remove Duplication
on 08-16-2009 7:44 AM

There is tool called Simian (www.redhillconsulting.com.au/.../simian) that you can use in your integration server to find out code duplications. This tool may greatly help you to get over-all picture of code duplications in entire project.

John Miller wrote re: Refactoring Day 15 : Remove Duplication
on 08-16-2009 11:21 AM

I use duplicatefinder.codeplex.com to check for this code smell.

Refactoring Day 15 : Remove Duplication - Sean Chambers - Los … | Cd duplication live today wrote Refactoring Day 15 : Remove Duplication - Sean Chambers - Los … | Cd duplication live today
on 08-19-2009 3:37 AM

Pingback from  Refactoring Day 15 : Remove Duplication - Sean Chambers - Los … | Cd duplication live today

张荣华 wrote 31天重构指南之十五:移除重复内容
on 09-29-2009 3:42 AM

这是最常用的重构之一,我们经常要用到它,因为我们的代码中经常因为这或那的原因产生重复代码,让我们来看下面的例子: 1: public class MedicalRecord 2: { 3: publi...

31 Days of Refactoring « Vincent Leung's .NET Tech Clips wrote 31 Days of Refactoring « Vincent Leung's .NET Tech Clips
on 10-28-2009 9:28 AM

Pingback from  31 Days of Refactoring « Vincent Leung's .NET Tech Clips

PetterLiu wrote 31 Days of Refactoring
on 11-27-2009 4:01 AM

Refactoring Day 1 : Encapsulate Collection Refactoring Day 2 : Move Method Refactoring Day 3 : Pull ...

PetterLiu wrote 31 Days of Refactoring
on 11-27-2009 4:03 AM

Refactoring Day 1 : Encapsulate Collection Refactoring Day 2 : Move Method Refactoring Day 3 : Pull ...

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

Enter the numbers above:
Copyright Los Techies 2008, 2009. All rights reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems