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

It doesn’t necessarily have to be in code you use for production applications, but if you, as an example, answer a question on Stack Overflow, and finds a code snippet somewhere that shows what you mean, a guid in both places would tie them together, even if nobody ever copies that code into a live application.

Perhaps Stack Overflow could do something automatically with this? Auto-insert a comment above/below every code snippet? Or would this add too much noise to the code? Would be technical hurdles as well, figuring out how to inject it safely, with respect to comment symbols, etc.

Oooooooh, are you talking about Brice Richard here?
http://discuss.joelonsoftware.com/default.asp?joel.3.568375.67

Looks like we’re gonna have StackOverflow iPhone client pretty soon!

I can see it now…

A language that incorporates snippets by reference. Pages and pages of code that consists entirely of GUIDs!

I haven’t ever sweated about copy and paste from the web, though I think Jeff’s GUID idea is neat. My big issue is with people who copy and paste inside their own code instead of properly modularising it. I have come to the conclusion that this practice is the single most reliable indicator of a poor programmer that one can encounter.

More often you can’t remember exactly how you wrote a routine (function,method) - instead of rewriting the whole thing again, you can copy and paste the whole shabang. I say that much of programming is not about writing new things, instead it’s writing old crap over and over again.

Guns don’t kill people… Rappers do!

I’m in agreement with almost everything you said - except the whole cognitive dissonance thing - it would actually be expectation disconformance.

But besides that, I have seen copy and paste made a so-so programmer actually fairly good. They would take components that worked, slot them together then fart-arse around until the whole things worked. They aren’t a good enough programmer to have written it from ground up by themselves, but they are good enough to make sure the entire thing works.

In fact - I think most programming seems to be going this way.

Also - I’ve got to agree with SteveC above, re-editable is much bettr than re-usable. I don’t want to re-invent the wheel, but I don’t want the exact same wheel I already have.

Oh - and you listed codeproject, that has quickly become one of my goto places.

This is a great idea, but I don’t think it will work as intended. People who copy and paste code verbatim from the Internet aren’t likely to know what a guid is or how to create one for their own code snippets, plus a lot of people don’t want you to know they copied their code from the Internet instead of writing it themselves so they’ll probably strip the guid comment out anyway. But for people who know what they’re doing, this is definitely a keeper.

I think you missed the best argument against copy’n paste code vs. usage of team leader abstraction:

Not using the (maybe or not) established abstraction of your team might make you miss the opportunity of handling similar things the same automatically and getting later improvements for free.

Maybe when pasting a code snippet from browser into IDE, the editor could automatically get the original URL from the browser and add it as code comment to the pasted text? Note sure if it’s technically possible, though…

Also, if the snippet site then adds the snippet GUID to the URL right from the start, you could also find the snippet even when the original site is long gone.

Seems like two very different things, copying and pasting within your own code and copying and pasting from an example on the web. The first is generally a no-no, the second doesn’t seem so bad to me. I guess the GUID might be useful for large code snippets but generally I only copy small bits of code from the web, which then get modified so much they bare little resemblance to the original, so who cares if the original gets updated?

I see stackoverflow code sharing coming.

Would be good snippets in posts could somehow link out to an overall database of code snippets which are updatable and version controlled.

The GUID sounds good to me, may need a bit more detail possibly assigning GUID’s at different levels i.e. assembly, class, method also maybe include the simple stuff language, version, date, authors/contributors… so it’s more searchable.

I’m going to leave the Copy/Paste software engineering discussion alone, and I’d like to throw my own twist on the proposed solution.

Instead of :
// see codesnippet:1c125546-b87c-49ff-8130-a24a3deda659

I propose a URI solution, which includes traditional URLs:
// see http://www.example.com/foo/bar

As well as the URN solutions:
// see urn:uuid:1c125546-b87c-49ff-8130-a24a3deda659

If you are interested, I wrote up some more complete thoughts here:
http://www.fort-awesome.net/blog/2009/04/22/uuid_snippets_and_rfc

Cheers!

Jeff, usually an essay titled A Modest Proposal… is written in the vein of Jonathan Swift’s famous one in which he suggested the Irish stave off famine by eating their own babies.

I recognize this is an honestly modest proposal, but you should be aware that most people will be assuming satire when they see the title.

That said, cool idea!

I think you confuse the issue by jumping from copying and pasting internally, and copying and pasting from external sources. The two have entirely different sets of problems.

The main problem with internal copying is that it’s usually a sign that you should be reusing the code.

The main problems I see with copying from external sources, are that there’s no guarantee of code quality, and that you’re using the code without understanding it. Far better would be to examine the code, and then write your own implementation.

ìTo me, the most troubling limitation of copypasta programming is the complete disconnect between the code you’ve pasted and all the other viral copies of it on the web. It’s impossible to locate new versions of the snippet, or fold your features and bugfixes back into the original snippet. ì

I donít really see why this should be necessary. Unless you are copying large amounts of functionality, in which case you really ought to be using a library, why would you need to keep searching the internet to find updated versions of the code snippet. Youíve inspected the code, youíve modified it for your own purposes; what advantage could there be in keeping up with every minor variant out there?

Copy Paste within a single project is bad, because unnecessary. If you see you need the same code again elsewhere in the project, make it an own function (or an own object or method) and just call it twice. Why? Because it makes the code base smaller (less memory usage), because it cuts compile/parsing times, because if the code has a bug, you only fix it in one position (not in 20 others where you copied it) and so on and so on.

However, copying code from another project is not necessarily bad, if you are allowed to do so and if this is good code. Your UUID idea looks like a great idea to me… but it is incomplete. E.g., what if I copy code and improve it? It is not the same code anymore, should it have the same UUID? Most likely not. But if I just give it a new one, where is the reference to the original code? So if you just copy code, I would say do it like you described

// codesnippet:{uuid}

but if you improve it (it may look like this)

// codesnippet.original:{uuid}
// codesnippet:{new-uuid}

That way you are using Google as a world wide, global SVN (or CVS if you prefer). If someone sees my code, he sees the UUID of this code (and hopefully copies it), so I can find my copied code in his project by UUID. However, he also sees the original UUID. In case he wants to see how the code looked BEFORE my improvements, he can just search for this UUID and will find the original code snippet.

If every author modifying this code keeps backtracks of all UUIDs and always adds a new UUID, you will have a HISTORY of code changes. This is nothing more than SVN, just that you use webpages to store the code and Google is your SVN indexing and history tracking service.

I think this is a very interesting idea and you should invest some more time into it. You may come up with a completely new way of tracking code world wide over the Internet, with all changes. Making this idea popular, Google may even create a special service for that. It searches its whole index and builds dependency trees where you can see how code evolved… you probably need to add name and date to the comment for that. In the end, Google can show you who originally created the code and when, who copied it, who modified it, who took a modification of it. Who took two modifications and merged them together again. See where I’m going here?

is it april 1 the second time this year?

I recognize this is an honestly modest proposal, but you should be aware that most people will be assuming satire when they see the title.

Are you sure this isn’t intended as satire? Most of it seems reasonable, but the actual GUID proposal seems a bit far fetched.

I know others have said it already, but I think gist is what you’re looking for.

http://gist.github.com/

Aren’t you just a little sad when you find out someone has already had your great idea?