Home > Uncategorized > Duplicate Code is Evil

Duplicate Code is Evil

September 21, 2010 Leave a comment Go to comments

I’m hoping no one here disagrees with the FACT that duplicate code is evil.  Nothing good can come from it.  You’ve heard “Money is the root of all evil” but in software I say “Duplication is the root of all evil.”  I have never agreed with the copy and paste programming style that I have come to witness many times. You should literally feel sick whenever you copy/paste a block of code.  Now I don’t mind if you’re copying a block of similar logic that you’re going to change 90% of but I do care if you’re going to copy/paste an entire block and leave it in its entirety or just change some variable names.

If you ever have the need to copy/paste the first thing you should think is “I should be able to cut and paste instead.”  IMO, the easiest method of refactoring is extract method. (Yes, there are real names for refactoring approaches.)  Re-use the logic instead of duplicating it.  Make a new method in the same class or move the method into a common object that others can share.

I’ve heard a couple times that “copy/paste is faster than refactoring” and in that split second that you’re pasting that block it probably is (I argue that cut/paste takes no more time).  The problem is that someone (maybe you) now has to maintain this code and have to update those now duplicate blocks and either keep perpetuating the lie or eventually clean it up the way it should have always been.  This will take them/you way more than a couple seconds and now you’re wasting time because you took the “easy way” out.

Why Duplication is Bad

  • More code means more places for bugs to hide.
    • If your original code has a bug in it, now all your copies do too.
  • More code to maintain
    • Why should I have to update more than one place with the same change?
  • More code to test
    • We still need to test the duplicate logic right?
  • More to sift through, which slows down future work.
    • You’re going to make me sit and read the same code block again and again?
    • Why are there duplicate blocks here?
    • Are they REALLY duplicated? Now I have to compare letter by letter to see if these 20 lines are really the same.
  • Direct violation of the DRY principle.  Don’t Repeat Yourself.

One way we combat duplicate code at Sentry is by analyzing our code during every build.  We use PHPCPD (CPD = Copy Paste Detection) to statically analyze our PHP code to make sure we’re not introducing duplication.  PHPCPD generates a report that Hudson is happy to display and tell us where our faults are.

I can view any job/build and click into the “Duplicate Code” section.  It allows me to navigate through the warnings and see the exact source code that has been identified as duplicate.  You can’t make it any easier than that.

About these ads
Categories: Uncategorized Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: