tags:

views:

225

answers:

12

When you copy a bulk of code from one place to another, do you always fix all that needs to be fixed in the pasted code, or do you sometimes discover bugs that were caused by not fixing that code thoroughly?

This rarely happens to me, but when it does it's quite frustrating. I wish there were a tool that would somehow mark pasted code, so that after a coding frenzy I could take a second look on that code... I know some people might suggest just taking a better look at the code for the first time, however sometimes one might be blind to to what one has just written (i.e. 2 to...).

+7  A: 

A big red warning light goes of in my head each time I copy code. Remember the DRY principle!

JesperE
+3  A: 

Constantly. Am I going to stop doing it? probably not ;)

Michael Neale
+1  A: 

I used to work at a company where the expression "the Devil of the copy paste" was widely known. In addition to violating the DRY rule there is a chance to introduce very obscure bugs.

+7  A: 

Why do you copy code in the first place?

Create a library/package/module/method/function and use that in the new program, generalizing it as you go. I read somewhere (Code Complete?) that NASA (was it NASA?) spends time when finishing each project to identify and generalize things that could be reused and that it paid off really well for them.

If you'll still do it, you can mark it yourself with comments and then search for the comments.

Vinko Vrsalovic
+3  A: 

In my experience the problem with reckless copy/pasting of code usually comes from people googling for code fixes and then copying straight from the web page to their source code.

If you're pasting any large chunk of code into your source without properly understanding it it is a major no-no.

Ash
+2  A: 

I was bitten by the copy-paste issue on a mere five lines of code in the same function recently. I got so focused on solving one problem that I didn't factor the code out till I found the bug when testing it.

I try to never copy code from one place to another, unless it's standard, boiler-plate stuff (standard #includes, header comments, etc).

warren
A: 

Copy-pasting code is not the best practice, but sometimes it helps. Of course bugs may occur, but they are usually easy to find and solve.

As for marking pasted code, sometimes I put comment marks on the code I'm unsure of like: "s081008", so if something goes wrong, I'd have a best candidate for review. And chronology helps finding sequence of what has been done recently, often leading to the cause of a trouble.

UPD: To make it clear, source control can't tell unsure piece of code from anything else, and marks can. It doesn't have to be date, or current revision number, or something informative at all. Date just makes things easyer.

akalenuk
Date comments?! Source Control?
David B
Not necesserly date comments, revision number is great too. It helps to find unsure piece of code, not dated piece of code. No source control can tell me which code is possibly bad, or possibly good. And date just helps to start digging.
akalenuk
I don't think that copy-pasting helps at all. I'm of the mindset that if you're copying and pasting code, then there is likely a flaw with the design. Perhaps you should abstract some of that code into a more generalized form (method, hierarchy, etc).
Tom
Yes, there is a flaw in a design. So what? I've never seen flawless piece of code ever anyways. The thing is to do your job with the least resources spent. If you need your code to be highly reusable - design is your goal. But if it is to have you projest working like yesterday, design'll wait.
akalenuk
+1  A: 

That is an antipattern. I do it myself.

EvilTeach
+4  A: 

I would consider copying and pasting code to be an error.

Jason Baker
+1  A: 

Every time I do it. And every time that happens, I scrap it, and generalise the code.

ayaz
+1  A: 

In my dark ages as a developers, I copied and pasted all the time and I did make alot of errors. I've found practicing TDD really reduces the amount of code I copy and paste. I can't really remember the last time I actually copied and pasted some code, nevermind a creating a bug.

Now the bugs are soley down to my idiocy now :)

Chris Canal
+1  A: 

I never copy and paste code. If I need to, I'll bring up two windows, and read from one as I retype into the other. This round-trip through the brain forces you to think about what the code actually does.

ephemient