Geeks With Blogs
Dave Chestnutt: SparklingCode and CodeGaffes Writing better code; for fun and profit

I use a coding tool called ReSharper - and I was pleasantly surprised the other day when it pointed out a copy & paste bug that was waiting to explode.  You see, copy and paste programming is so very easy to do - that we all do it.  Even the best of us accidentally leave in duplicate code snippets from time to time.


Why Cut & Paste is so bad

There are two big reasons why this is an anti-pattern:

  1. If the code you cut and pasted has a bug in it (and it invariably will), what do you think are the chances that you'll find and fix all occurrences of it?
  2. If you're rubber-stamping code (copy and paste, paste, paste…) so that you can make minor edits to the pasted version, it's too easy to make a mistake - and the code will compile fine.  It won't fall over until a customer runs it.  Rubber-stamping typically happens in a switch statement or a batch or if/else-if statements (where the cases are nearly identical).


It's this second case that ReSharper showed me a bug that wouldn't have been found until run-time.  It surprised me because ReSharper doesn't look for duplicate code snippets.  But one of its rules can get triggered if you copy & paste code incorrectly.


ReSharper is...

This is an add-in to Visual Studio.  It does many things, but one thing it does well is point out flaws in your C# code via highlighting - so you can quickly see suspicious or questionable practices.  There are other tools that do the same for C#, and there are similar tools for Java. [Disclaimer: I do not work for JetBrains. I paid for my own copy out of my own pocket. I'm just a happy user.]


This is not a tutorial on using ReSharper - I'm just showing one tiny example of how it can help you.  Here is some legacy code opened in Visual Studio with ReSharper:



Notice the highlights on the right of the source window.  Those give an indication of where there might be problems in the source.  You can click those lines and it will jump to the place in the code with the problem.  It highlights the error and offers fixes, too.  The orange lines are approximately lined up with where the scroll bar would be when you view the code.  Orange means warning, and red means error.  Opening up legacy code often shows many many orange lines because ReSharper warns about suspicious or bad practices.  These are things that the compiler is happy with, but you'll be unhappy about in the long run.


Here's what ReSharper found

So, back to how I was pleasantly surprised.  Look at this code example.  It's pretty obvious at first glance that this code was rubber-stamped to handle many cases - with slight edits in each version.


This code will compile, but it won't work right.  Here's what it looks like in normal Visual Studio:




Do you see the problem?  It's in line 41.  When copying and pasting, the person forgot to modify the if statement to use htmlButtonMatch instead of htmlTextboxMatch.  It's clearly wrong, but it might not show up as a bug until months after someone wrote this.


Look at what ReSharper shows (it does additional highlighting of things like method names, etc.):




With this cool tool, I'm immediately alerted to the problem.    The tool tip for the grayed out variable warns me that it wasn't used.  From that, I can easily spot the error in line 41.  The important thing is that ReSharper alerted me that there was a problem.


This was some actual legacy code that I opened up one day - when I noticed the problem.  I was fix it and find out why unit tests didn't cover this case.


You know, if I didn't tell people I used ReSharper, they'd think I was really smart.


But I am smart enough to use good tools.



See also:


Posted on Monday, December 11, 2006 12:22 PM CodeGaffes , Visual Studio Tips | Back to top

Comments on this post: CodeGaffe: Copy & Paste (and ReSharper to the rescue)

# re: CodeGaffe: Copy & Paste (and ReSharper to the rescue)
Requesting Gravatar...
Good ! thank the author.
Left by Google排名 on Apr 23, 2007 5:30 AM

# re: CodeGaffe: Copy & Paste (and ReSharper to the rescue)
Requesting Gravatar...
u have not used their java tool, which identifies similar codes, that is, if u a copy paste coder, u can get a lot of warnings (can be turened off, though)
Left by another happy user on May 10, 2008 1:37 AM

# re: CodeGaffe: Copy & Paste (and ReSharper to the rescue)
Requesting Gravatar...
Great tutorial.Thank you.
I love CodeGaffe.
Left by mens casual shoes on Oct 12, 2009 7:36 AM

Your comment:
 (will show your gravatar)

Copyright © Dave Chestnutt | Powered by: