Problems with Copy Pasting Code - And Modifying Slightly... Then How To Go Back And Fix It All Up Afterwards.

As developers, we often find ourselves under a lot of pressure to crank out code and copy then paste it somewhere else in a solution and make a slight modification here and there and get the product out the door. It's a frequent occurrence and hence the number of lines of code is not a great measure of quality code. There's nothing wrong if it gets the code out the door, however, when we have time we can go back and fix this.

What happens, if we don't and some code has been copied and pasted in different renditions and a bug comes up in one of the scenarios? We can fix it in the one instance, but that bug may also be applicable in all or some the copy/paste instances of code.

Considering the frequency of these scenarios, we've introduced Code Clone Analysis in Visual Studio 2012. What is does it search a solution and looks for similar code.

From the Analyze menu in Visual Studio 2012, we select "Analyze Solution for Code Clones"

We the observe that in this case we have two strong matches.

 Let's select both the files and compare as shown below...

 

We can see below that the methods are very similar. This is an opportunity to refactor. Perhaps to turn this into a single method, with some extra parameter or some class redesign or perhaps to use generics.

In fact you can select a piece of code and select "Find Matching Clones in Solution" from the context menu as shown below.

So, when you get a chance, like a couple of minutes, check out the tool and see how much copying and pasting has been going on in your team ;)

Comments

  • Anonymous
    March 24, 2013
    Hi, I'm using Visual Studio 2012 Professional. I can't see that option, is there a way to enable it?

  • Anonymous
    March 24, 2013
    This feature isn't available in Professional. Analyse solution for Code Clones" is only available in Ultimate and Premium editions of Visual Studio 2012

  • Anonymous
    March 24, 2013
    I'm guessing it's only available in premium/ultimate edition.  In 2010 none of the analysis was available in Pro; and while they pushed a lot of it down to express/pro editions for 2012 I think this is one of the features that's being kept for the higher end versions.  Hopefully this will be pushed down in VS2014 (most new features do); but until then it looks like those of us with IT depts with limited budgets will again just be taunted by all the cool new stuff we can't use.

  • Anonymous
    March 24, 2013
    Oh WOW.  I love this.  I find copy/paste goes on way to much in code. This feature is making me want to upgrade to 2012 today!

  • Anonymous
    March 25, 2013
    In light of the question, I've posted a blog today to provide a little more clarity on such matters. blogs.msdn.com/.../what-are-the-editions-of-vs2012-what-s-the-value-proposition.aspx If you are on MSDN, you can upgrade to 2012 and with project interoperability between 2010 and 2012, this should not be an issue.

  • Anonymous
    June 11, 2013
    That's save my life. Thanks for your tips.