Copy And Paste / RYRY

Written by David Frampton @ 10:27 am, June 23, 2009

I have heard that copying any part of your code and pasting it somewhere else in your code is bad. The argument usually goes along the lines of ‘You needed it twice, so you should make a function’. OK, I’m thinking that maybe seems kind of reasonable, but I have to ask. WHY? ‘Because then when you need to make a change to that functionality, you can make it in the function, instead of two places in your code.’

OK so if both callers of that code need the same change, I can change it only once, instead of twice. Great! So making a whole extra function, making it callable from both places, passing in all the crap it needs from both places means that on the off chance that both of those use cases change in exactly the same way, I need only change the code in one place?

I don’t think so. I’ll change two places. In fact more than likely I’ll change one place. Or maybe I’ll change one place to do something a bit different, and then change the other place to do something completely different. And, I can do so and not worry about affecting anything else.

Screw DRY. I say repeat yourself, and then repeat yourself again.









4 Comments

  1. Tim Haines

    I bags not maintaining your code.

    Comment by Tim Haines — June 23, 2009 @ 10:55 am


  2. David Frampton

    True. I work alone, so I don’t care what poor bastard might have to look at my code down the line. In fact this project has a life cycle of about 5 years, and I can hack that, I’ve dealt with worse, so I don’t even care that I might have to maintain it. Right now I’m aiming for release. What happens beyond that can be dealt with beyond that… IF it ever becomes a problem, which is pretty unlikely!

    Comment by David Frampton — June 23, 2009 @ 11:56 am


  3. martin

    I usually have no problem copy/pasting code… once. But if I find myself copying the same code again, then it’s time to make it a separate method.

    Comment by martin — June 29, 2009 @ 2:21 am


  4. Kalle

    I find it suprising that you don’t agree with DRY, since you seem to manage quite large projects. I find that often when I want to use slightly different versions of basically the same code, there is an obvious shortcoming in my design/architecture. 9/10 times when I first want to copy a a few lines of code, I almost immediately see how a little refactoring can substantially improve the structure of my code. Just my $0.02 :)

    Comment by Kalle — July 2, 2009 @ 1:35 pm


RSS feed for comments on this post.

Sorry, the comment form is closed at this time.