A Modest Proposal for the Copy and Paste School of Code Reuse

What a great idea and a perfect post at the perfect time for me. Just blogged a more detailed response here:
http://www.mtelligent.com/journal/2009/4/22/copy-and-paste-code-reuse-proposal-accepted.html

I have been in beta for the last two weeks of the second version of a product I created to manage code snippets. New features include integration with Snipplr, one of the code sharing sites Jeff mentioned.

I will implement this auto commenting feature in the product this weekend. I will probably need to generate the Guid’s internally, making them only searchable through my search interface (or through the xml files they are stored as), but I can also automatically comment out a reference url, another property I allow snippets to be tagged with.

I will try to get this done this weekend.

Thanks for the GUID! I’d been looking for one all day!
Now I’ll just copy paste 1c125546-b87c-49ff-8130-a24a3deda659 onto my code snippets, thanks for the advice!

:slight_smile:

This boggles my mind. I cannot think of one single time in the last 15 years that I have copied code from some place and not modified it to suit my needs. So none of my code would ever have the same GUID as some snippet on the net.

If I use a good piece of code and I want to add a comment as to where I found it (where there might be a good explanation of what it does for the next programmer to look at my code) then I will add a url to the source in my comments, not some useless GUID.

I was once completely blown away by Eclipse when I was working as a maintenance programmer in Java land.

Lots of repeated code in a module that went on for about 200 lines (gah! don’t go there!). I selected the code and went to the nice refactoring tool and selected turn into method.

I thought I would have to trawl through the code and pick up all the rest of the repetition. Eclipse refactoring just identified it and rewrote the whole module.

I ‘lost’ 100 lines of redundant code in about 20 seconds and, when I noticed some bits of the stupidly-long case statement didn’t refactor, even found a couple of bugs.

In Ruby land this doesn’t happen anywhere near as often because it encourages you to think small.

c p programming IS DANGEROUS in the hands of careless, non commited, irresponsible - chicken ‘programmers’, who don’t bother about what they deliver as long as they get an output and are aversed to think (brain killer)- they are at the most - ‘involved’.

No measures can regulate this unless there is a fundamental change in the thought process and commitment levels.

For the rest - ‘ham’ types - c p is just another good n fast mechanism (not a practise/way of life)to analyze, learn, contemplate, modify/extend and apply something they don’t know or have not done yet!

Atmost, everybody who publishes code for sharing can and should be regulated rather than the ones who use the code. This way atleast wrong code is not spread.

In terms of analogy - Regulate the gun manufacturing and easy access to help save lives.

A GUID makes sense if it can be used to traceback to a code snippet - which is regulated.

Infact, a combination of GUID the MD5 hash will be good - where the guid can be used to identify, track n categorize snippets, while the hash can be used to track and maintain the variations.

Isn’t the simplest thing that could possibly work just putting the URL where you found it in a comment above it? :slight_smile:

Over here the running joke is that .cpp actually stands for Copy Paste Programming. Thankfully it’s getting better…

But how often do you copy paste code and not change it?

When I’m doing copy and paste, it’s because the methods are similar enough that it’ll save me time, but different enough that I’d have to get into some crazy abstractions for relatively simple code to be able to refactor them.

Even in the rare case when I copy something off of the web, I at the very least rename everything to follow our conventions.

Maybe other people do more copy and paste work off the net, but in my experience, cargo cult programming sits at the end of that road. There will be no code in my project that I do not understand, and if it is a) small enough to cut and paste b) simple enough that it is doing exactly what I want without modification c) simple enough that I can understand it without a lot of work, I’ll probably just go write it myself.

Also, Apple was the first to do copy and paste, and they did it with the command button. Isn’t it easier to use your thumb than your pinky? Also, I’m not sure if this is intentional or not, but when working with a command line it is very nice to not have your copy command and your force-exit program command bound to the same shortcut :slight_smile: Use the terminal on OS X for a half hour, then its windows cmd.exe equivalent for a while, and I think you’ll agree with me :slight_smile: You could always change the command line shortcut, but I think ctrl-c to exit your current process has been around even longer than cmd-c to copy.

Didn’t we solve this problem once through precompiled libraries? The idea of code components is pretty old by now.

The article is long gone now of course, but MSDN used to have a piece that admitted code reuse failed in C/C++ even though it became one of VB6’s biggest successes via COM.

On sun hardware keyboards have one button each for cut, copy paste, so no extra dexterity (which may cause RSI) needed…

http://www.xahlee.org/emacs/i/kb/sun_keyboard_left.jpg

I read an interesting research paper (I could dig up the citation if people are interested) about copy paste programming. They used some techniques to find repeated code in a couple different projects, then tracked what happened to it over the version history of said projects. What they found was that there was actually surprisingly little code that was repeated. Most of the time, the repeated code was quickly factored out into a separate function or the two copies diverged enough that it’d be next to impossible to generalize.

I know I’ve had similar things in what I’ve done. I’ll need a near copy of some code, but there will be some difference that makes it hard to generalize. (The sort where the only way I see to do it is to have the function take other function pointers as parameters, factor said functionality out into a ridiculous class hierarchy just for that purpose, or pass in some extra flag that just says ‘which of these three functions should I call’?. I’m having trouble thinking of concrete examples though.) So I’ll copy and paste it. I’ll check to see if it works, and then I’ll see what I can do to take care of the code reuse. Sometimes I figure something out that works well, sometimes not. But if I do try to generalize and it doesn’t work, I’ll know it’s a problem with the generalization and not a problem with the foundational code.

Adding a GUID seems kinda useless to me. You want it to track a snippet. Why not just subscribe to updates on the blog of the author?

Otherwise, just use the snippet databases (or even StackOverflow) to keep track of snippets.

I love the double meaning of Undoubtedly the most popular reason for
creating a routine - mostly programmers that simply do a routine are the ones that copy and paste (too much)

I love the double meaning of Undoubtedly the most popular reason for
creating a routine - mostly programmers that simply do a routine are the ones that copy and paste (too much)

I love the double meaning of Undoubtedly the most popular reason for
creating a routine - mostly programmers that simply do a routine are the ones that copy and paste (too much)

smug smile

How about instead of just a GUID we add some sort of general meta-data? Include the GUID for uniqueness, but add some tags or other data as well.

If I want to search and find a quick-sort algorithm, I’m not going to search by GUID. I want to search by sort or quick-sort.

This sounds like a solution in search of a problem. A unique ID for every code snippet? WTF are you talking about? :slight_smile:

github’s gists work well, but your idea is nice too as it does not rely on a single vendor.

We had a coworker who instead of copy-pasting from preexisting code into new code, he cut-and-pasted from the preexisting code into the new code. Naturally, hilarity ensued.

Hey, I can post some copyrighted (or GPL-ed) code with a snip-it on SO anonymously. Don’t mention the license. Then screw everyone using that code. I’ll know where you got the code - you’ll have the link in your code. How can you prove it was me who actually posted the code?

I Like It.

Yeah, agree with the others who have pointed out that they never copy/paste without modifying the snippet immediately afterwards. The GUID idea, while clever, seems very much like overkill for most use cases.