Seriously, Do not Repeat Yourself

By Kenneth 'RabidDog' Clark at September 05, 2011 23:53
Filed Under: C#, Work, Architecture

A few projects I have been on and off seem to suffer the same problem. Tight deadlines and the dreaded Ctrl+C, Ctrl+V. I honestly can’t figure out how this happens. Repeated logic is not consolidated into a single method that performs the required actions and returns the result.

 

Let me illustrate the problem very quickly

 

public Class1 CreateInstanceOfClass1(SuperInformation superInformation){
    var myInstance = new Class1();
    
    myInstance.SuperInfo = superInformation;

    return myInstance;   
}

public Class2 CreateInstanceOfClass2(SuperInformation superInformation){
    var myInstance = new Class2();
    
    myInstance.SuperInfo = superInformation;

    return myInstance;   
}

 

Right this is a very simplified example but lets examine it anyway. I am pretty certain that we have all gathered that these methods create an instance of class, assign a shared object to it and return it.

 

Do you notice a pattern here? Every time an instance of the object is created the shared superInformation definition is assigned to the instance and returned. Can anyone see how we are repeating ourselves? How do you think we might resolve this? Well my first thoughts would be to use a generic mechanism to create the instance and assign the shared object to the instance.

 

This might look something like

public T CreateClassInstance<T>(SuperInformation superInfo){
    var output = Activator.CreateInstance(typeof(T));
    
    output.getType().getProperty("SuperInfo").SetValue(output, superInfo, null);

    return output;
}

 

Which now changes our code to in the first example to

public Class1 CreateInstanceOfClass1(SuperInformation superInformation){
    return CreateClassInstance <Class1>(superInformation);
}

public Class2 CreateInstanceOfClass2(SuperInformation superInformation){
    return CreateClassInstance <Class2>(superInformation);  
}

 

Well that is one way of addressing the issue using Generics in C#. A similar mechanism can be applied to if else statements following the same logical flow inside different functions.

 

I guess the point of this article is this. If you copy and paste one piece of code you have replicated that code. If that code contains one bug, you have now created two bugs. If something intrinsic to the one changes you have an additional place to go and change. Perhaps I am just to pedantic but I am incline to state that if you replicate one piece of code, you would be far better off wrapping it into a general method. This doesn’t mean trying to find all places this might potentially happen. In my experience, code bases are organic (well kinda). They grow, they change, the expand, they contract. When the expansion happens, expand with wisdom, when they contract, shrink with wisdom, when they change, change with wisdom. We can all identify a pattern in our code. If you identify one, fix it. I know the deadlines are tight but taking a shortcut now will cost a substantial amount to rectify down the line when the 4th change set comes in. If you identify something that you can fix without adding risk to the project then do it. If it means an extra hour behind the machine, do it.

 

If you are working on a legacy project and are asked to add features to the project be smart. If you see that the previous code base was replicating code, don’t do the same thing! Identify the pattern and figure out how to not replicate the code in your feature set. Do not push these changes across the whole system unless you have done a risk analysis. Keep your area clean. Be proud of your work and craft it, be proud of your teamj mates work and offer feedback. To often it is just a matter of writing as much code as possible in the most contrived fashion possible to prove how smart we are. I tell you something, you are going to look like an idiot when the next guy steps in and has to work with your code. Don’t be afraid to ask for help, don’t be so arrogant as to not give help when asked for. At the end of the day, the success of the project is not based on an individual's effort but the combine effort of the team involved in the project. Work together and deliver something you can all stand back and look at with admiration. If projects fail we need to take a hard look at ourselves and accept responsibility, no finger pointing.

 

Code hard, think hard and polish until  it is done and by virtue of the fact that it continually changes, it never is done Winking smile

Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading








I am South African. Always have been always will be. I love my country. I love my wife and two children.


I also really enjoy solving problems. I currently work as a Software Architect exploring new solutions for business problems. Having been round the block a few times I enjoy showing new developers how best to solve problems, how to find answers and how to approach solution development.


In my spare time I enjoy riding my super bike, training in Systema and horsing around with my family.


Month List

Visitors

Twitter Feed

21. May 10:15
Still can't believe that they used american actors in Invictus. Just doesn't fit!

17. May 17:12
@UnexpectedPippa only 3? "Don't touch me on my studio!"

17. May 17:12
@SaartjieJoan if you look around you might see many forks hanging out of eye sockets

17. May 17:09
@SaartjieJoan That truly is amazing HAHAHAHA!