Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
A Modest Proposal for the Copy and Paste School of Code Reuse (codinghorror.com)
31 points by bdfh42 on April 22, 2009 | hide | past | favorite | 22 comments


You should copy and paste code so rarely that this shouldn't be a problem. The author of the article proves this point effectively with his snippet example; the correct way to fade out a window in Cocoa is with an NSAnimation and the NSViewAnimationFadeOutEffect on an NSWindow. His code blocks the thread directly and could cause problems, especially if called from a routine that responds to an IBAction (GUI event).

There's a reason good developer documentation calls example code Listings or Illustrations: you're supposed to read and understand it, then write it yourself in your own application. Copying and pasting code verbatim is always a bad idea, even for simple things like short regexps.

Don't copy and paste code that you don't understand or couldn't write yourself.


Copy, paste, and modify is one of the ways I managed to learn web development in the first place. Sometimes, understanding doesn't come from reading alone. You need to feel the clay in your hands to mold it into the pot you want to make.


Ever think that it actually slows your learning down? Honestly, you learn code much better if you actually type it yourself. Even if you have a working example, retype it manually, it'll make you appreciate doing proper reuse via function calls much better. Copy and paste is a bad habit, code is not clay, and treating it as such hurts you.


I'm not saying I do this today, I'm saying it was integral to my learning process when I started, years ago. Typing it out doesn't help solve the issues of learning the subtleties of syntax. You need to start with something that works and then start seeing the ways your poor typing breaks the code.


If you learn better that way, sure, feel free to do what works best for you. I don't see how this applies to the article, which advocates inserting code with a GUID in a comment so that you can find it again later and copy/paste a new version in.


It's a direct response to your comment, not the article.

"Don't copy and paste code that you don't understand or couldn't write yourself."


But sometimes I need to use code that I couldn't write myself.

An example: you can send an XmlHttpRequest to a different port on the same domain in Firefox, but only if first you add the line

  document.domain = document.domain;
Now, I wouldn't expect to be able to figure that out myself. So I just copy and paste, add a comment saying that this is a bit of magic that makes cross-port xhr's work in firefox, and add the url I got it from in the comment. Doesn't that seem reasonable?


Yes, it does. We don't need some GUID standard for stuff like this, as advocated by the article. Your example is fine.


Err .. I am the only one with this view or did anyone else also think that the article wasn't provided any new insight or point of view. He just basically iterated everything that everyone knows about the pros and cons of copying code ?!


That is what Jeff does. I really don't understand how every one of his inane posts makes the front page of HN.


I thought this was new: "Attach a one line comment convention with a new GUID to any code snippet you publish on the web. This ties the snippet of code to its author and any subsequent clones. A trivial search for the code snippet GUID would identify every other copy of the snippet on the web."


Yeah. Many blog posts (and not just his!) consist of rehashed material from books like _The Pragrmatic Programmer_, _Code Complete_, _The Practice of Programming_, etc., with small amounts of commentary.

Sometimes the author is doing it as a way to ruminate on the ideas, sometimes they're doing it for some kind of ad revenue, etc.


If there were only some way to give code snippets on the web some form of universal resource locator, you could skip the guid...


The existing problem (for Jeff) is that it is hard to find modified or improved versions of the same snippet. Whereas a URL points to an inidividual snippet, the GUID could point to different versions. The same GUID could also be used for the same snippet in a different language.

Obviously you could also group related snippets on a site, but that formalizes it too much. Snippets are mean to be copy/pasted and passed around on forums, blogs and sites.


But ... you can still Google for the URL.


If one had a database of code snippets, then, given a specific code snippet it would be possible to find "similar" code in the database: code that had been changed or improved from some original version. One could even create an evolutionary tree for each snippet, if one were so inclined.

The pertinent algorithms are already in heavy use in bioinformatics.


There already is a globally unique identifier, although not necessarily a "GUID", on things you get from the web:

It's called a URL.


Jeff Atwood is essentially describing git/github.


Finding copy-pasted pieces of code that have been modified in place is actually a rather difficult problem. I've been working on a tool for this in my spare time over the last several months, and I hope to release it sometime this summer. Pinning down what's "similar enough" is subtle, and if you go too far, it complains that boilerplate-y code in e.g. Java is unnecessarily redundant. (While this is certainly true, it's not useful.)

I look forward to the upcoming structured programming revolution. Some day, people will use subroutines!


Oh my god, I can almost see some noir company disabling Ctrl-C/Ctrl-V on programmer's laptops.


I think it needs a system, not simply developers do it like that.

May be a new startup that takes care of snippets


Like the ones he linked to?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: