Coffeehouse Thread

3 posts

How to prevent copy & paste coding?

Back to Forum: Coffeehouse
  • User profile image
    terrajobst

    Hi,

    recently I have released a software called Clone Detective for Visual Studio (CodePlex). This tool allows you to find duplicated source code in C# projects. Although this is a very powerful tool it does not prevent copy & paste coding before it happens; it can only detect it.

    What do you do against this well known behavior? Code reviews? Better dev education? Aspect oriented programming (e.g.with PostSharp)?

    I believe code duplication is not always something one must fight. For example, our company sometimes intentionally duplicates source code in order to remove a dependency between two components. I think from time to time it is better to eliminate a dependency instead of taking code reuse to the extreme. More dependencies  make a system more complex and harder to support. On the other hand I have heard the Excel team at Microsoft has made a religion out of this -- they even have developed their own C compiler. I believe this takes dependency elimination a bit too far. (to be honest, this story about the Excel could also be an urban legend Smiley)

    What do you think? How do you fight copy & paste? Do you fight at all?

  • User profile image
    JoshRoss

    The best written code is none at all.  If the shared code is well documented and the API is clean, there is much less incentive to roll your own solution to a problem that has been solved already.  Avoid creating large classes that do all kinds of things, keep them small, single purposed and use names that clearly define what it is your class does.  These simple rules encourage code reuse.

  • User profile image
    figuerres

    darwin's survival of the fitest.

    "Good" developers do not want to just copy mindlesly.
    "Good" developers learn, grow, advance...

    hacks who try to just copy will fall out.

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.