Is copying and pasting code dangerous? Should control-c and control-v be treated not as essential programming keyboard shortcuts, but registered weapons?
(yes, I know that in OS X, the keyboard shortcut for cut and paste uses "crazy Prince symbol key" instead of control, like God intended. Any cognitive dissonance you may be experiencing right now is also intentional.)
Here's my position on copy and paste for programmers:
Copy and paste doesn't create bad code. Bad programmers create bad code.
Or, if you prefer, guns don't kill people, people kill people. Just make sure that source code isn't pointed at me when it goes off. There are always risks. When you copy and paste code, vigilance is required to make sure you (or someone you work with) isn't falling into the trap of copy and paste code duplication:
Undoubtedly the most popular reason for creating a routine is to avoid duplicate code. Similar code in two routines is a warning sign. David Parnas says that if you use copy and paste while you're coding, you're probably committing a design error. Instead of copying code, move it into its own routine. Future modifications will be easier because you will need to modify the code in only one location. The code will be more reliable because you will have only one place in which to be sure that the code is correct.
Some programmers agree with Parnas, going so far as to advocate disabling cut and paste entirely. I think that's rather extreme. I use copy and paste while programming all the time, but never in a way that runs counter to Curly's Law.
But pervasive high-speed internet -- and a whole new generation of hyper-connected young programmers weaned on the web -- has changed the dynamics of programming. Copy and paste is no longer a pejorative term, but a simple observation about how a lot of modern coding gets done, like it or not. This new dynamic was codified into law as Bambrick's 8th Rule of Code Reuse:
It's far easier and much less trouble to find and use a bug-ridden, poorly implemented snippet of code written by a 13 year old blogger on the other side of the world than it is to find and use the equivalent piece of code written by your team leader on the other side of a cubicle partition.(And I think that the copy and paste school of code reuse is flourishing, and will always flourish, even though it gives very suboptimal results.)
Per Mr. Bambrick, copy and pasted code from the internet is good because:
But copy and pasted code from the internet is bad because:
Now, if you're copying entire projects or groups of files, you should be inheriting that code from a project that's already under proper source control. That's just basic software engineering (we hope). But the type of code I'm likely to cut and paste isn't entire projects or files. It's probably a code snippet -- an algorithm, a routine, a page of code, or perhaps a handful of functions. There are several established code snippet sharing services:
Source control is great, but it's massive overkill for, say, this little Objective-C animation snippet:
- (void)fadeOutWindow:(NSWindow*)window{
float alpha = 1.0;
[window setAlphaValue:alpha];
[window makeKeyAndOrderFront:self];
for (int x = 0; x < 10; x++) {
alpha -= 0.1;
[window setAlphaValue:alpha];
[NSThread sleepForTimeInterval:0.020];
}
}
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. Nor can you possibly hope to find all the other nooks and crannies of code all over the world this snippet has crept into.
What I propose is this:
// codesnippet:1c125546-b87c-49ff-8130-a24a3deda659
- (void)fadeOutWindow:(NSWindow*)window{
// code
}
}
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:
http://www.google.com/search?q=1c125546-b87c-49ff-8130-a24a3deda659
I realize that what I'm proposing, as simple as it is, might still be an onerous requirement for copy-paste programmers. They're too busy copying and pasting to bother with silly conventions! Instead, imagine the centralized code snippet sharing services automatically applying a snippet GUID comment to every snippet they share. If they did, this convention could get real traction virtually overnight. And why not? We're just following the fine software engineering tradition of doing the stupidest thing that could possibly work.
No, it isn't a perfect system, by any means. For one thing, variants and improvements of the code would probably need their own snippet GUID, ideally by adding a second line to indicate the parent snippet they were derived from. And what do you do when you combine snippets with your own code, or merge snippets together? But let's not over think it, either. This is a simple, easily implementable improvement over what we have now: utter copy-and-paste code chaos.
Sometimes, small code requires small solutions.
What if you copy-paste code to move it to routine of it's own?
nc on April 22, 2009 2:03 AMTo nc:
I think that to copy-paste some code to make a routine of it is not bad at all, but I don't think this is what Jeff is talking about.
I think that the most important problem with copy-paste are bad programmers, who copy-paste it without understanding what it does completely. Another problem may be the lack of consistency with the rest of code, in error tracking policies for example, or to a lesser extent, the lack of code formatting consistency.
To sum up, I think that code snippets around are better to get ideas of how to solve something rather than copy-pasting them.
Unknown Programmer on April 22, 2009 2:23 AMHopefully you're cutting and pasting, not copying and pasting. Subtle distinction but there is one.
Instead of a GUID, why not use a SHA1 hash that's based on the contents of the code snippet, like what git does?
Joe Chung on April 22, 2009 2:24 AM> (yes, I know that in OS X, the keyboard shortcut for cut and paste uses "crazy Prince symbol key" instead of control, like God intended. Any cognitive dissonance you may be experiencing right now is also intentional.)
Are you saying Apple came before God? (Well, at least the Lisa.)
gassit on April 22, 2009 2:26 AM> Instead of a GUID, why not use a SHA1 hash
Because it's the wrong tool for the job. A hash will change every time the snippet is changed, whereas the GUID will remain constant. This idea relies upon having a reliable, life-long identifier.
Simon Wright on April 22, 2009 2:27 AMI think something like http://gist.github.com/gists is a better approach. That's what I try to do when I grab a snippet.
Ben on April 22, 2009 2:29 AMLegalizing copying of assignments, huh?
Amarghosh on April 22, 2009 2:36 AM> yes, I know that in OS X, the keyboard shortcut
> for cut and paste uses "crazy Prince symbol key"
> instead of control, like God intended.
Given that the assignment of Z, X, C, and V as accelerators for undo, cut, copy and paste was chosen by Apple developers and first appeared on the Apple Lisa, I think it's safe to say Apple is God (not FSM, as once hoped).
http://en.wikipedia.org/wiki/Cut_and_paste
(Hmm, has Prince been assigned a unicode code point?)
Simon Wright on April 22, 2009 2:37 AMI agree on gist.github.com. Distributed version control is the way to go when dealing with snippets. Github gists offer version control with a low barrier, and you can easily fork snippets from other users, while the link between those snippets is maintained. Using tools that integrate with your editor, it is very easy to publish snippets.
Willem on April 22, 2009 2:40 AMI am amazed to see all the apple stuff here. Is the objective-c animation example on purpose? Because with core animation doing this for you how many programmers are finding this code snippet and using this. It makes a point about copy and paste programming.
grrr on April 22, 2009 2:42 AMwww.SnippetOverflow.com
Kale Kold on April 22, 2009 2:44 AMWhere the "crazy Prince symbol key" came from:
http://www.folklore.org/StoryView.py?story=Swedish_Campground.txt
Left Ctrl+X/C/V is just a recipe for RSI btw. Right Ctrl would be better...
Why not just put the URL in the code snippet instead of GUID? Sites should be quite good at keeping URLs valid by now shouldn't they?
dan on April 22, 2009 2:47 AMI couldn't help but notice that you included the picture of a Mac keyboard and marked the Control key...
Copying and pasting under Mac OS X is done via Command + C/V. Soo... Jeez.
Mr Jay on April 22, 2009 3:00 AMI like this idea, a lot.
So I expanded on it.
If you go and download the freeware tool made by Whole Tomato, the makers of Visual Assist, an addin for Visual Studio, specifically, this tool: http://www.wholetomato.com/products/sourcelinks/FogBugzBundle.asp
And then add this rule to it (Tools->Options->SourceLinks):
Name: codesnippet
Keywords: codesnippet:
(yes, add the colon)
Value: String
Url/Exe: http://www.google.com/search?q=%s
the rest as default, then the comment becomes double-clickable in Visual Studio and you'll search for it on Google if you do.
Lasse V. Karlsen on April 22, 2009 3:08 AM@Mr Jay:
> Copying and pasting under Mac OS X is done via Command + C/V. Soo... Jeez.
Jeff clearly outlined the irony of the picture. Anyway, the real tragedy is that the Caps Lock key is where God intended the Control key to be.
guns on April 22, 2009 3:12 AMInteresting idea, i kind of like it. I don't C&P all that much, but it would be great to find a GUID i could search for when i find some arcane C&P'd piece of code in an application.
I used to put the url to the snippet in a comment next to it, but sadly that solution works only as long as the page exists.
C&P web snippets should of course never happen for trivial stuff, but when you are using some arcane library or extension it is often the best way to get a head start in learning how it works. Combinded with inevitable deadlines means that some of the code is bound to stay alive.
Having a GUID next to such code would be perfect to get some context when a bug arises.
Erik on April 22, 2009 3:12 AMIt 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.
Lasse V. Karlsen on April 22, 2009 3:17 AMOooooooh, 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!
Mehrdad on April 22, 2009 3:29 AMI can see it now...
A language that incorporates snippets by reference. Pages and pages of code that consists entirely of GUIDs!
Dennis on April 22, 2009 3:39 AM
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.
Wanko on April 22, 2009 3:56 AMGuns don't kill people.. Rappers do!
Scrimmers on April 22, 2009 3:57 AMThis 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.
tkrehbiel on April 22, 2009 4:10 AMI 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?
Doogal on April 22, 2009 4:17 AMbeware of the right-click warrior
whocares on April 22, 2009 4:20 AMI 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.
pete on April 22, 2009 4:25 AMJeff, 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?
laugh on April 22, 2009 4:43 AM"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.
Steve W on April 22, 2009 4:50 AMWhat 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!
:)
Robert on April 22, 2009 4:58 AMThis 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.
Dan A on April 22, 2009 5:02 AMI 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.
Francis Fish on April 22, 2009 5:04 AMc & 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.
radicalfish on April 22, 2009 5:09 AMA 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.
radicalfish on April 22, 2009 5:22 AMIsn't the "simplest thing that could possibly work" just putting the URL where you found it in a comment above it? :)
Over here the running joke is that .cpp actually stands for "Copy Paste Programming". Thankfully it's getting better...
Benoit Miller on April 22, 2009 5:36 AMBut 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 :) 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 :) 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.
Mike on April 22, 2009 5:44 AMDidn'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.
Old Joe on April 22, 2009 5:44 AMOn 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
Fionn on April 22, 2009 5:47 AMI 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.
Evan on April 22, 2009 5:52 AMAdding 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.
Michel Billard on April 22, 2009 6:01 AMI 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*
Seth on April 22, 2009 6:10 AMHow 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."
Brian on April 22, 2009 6:23 AMThis sounds like a solution in search of a problem. A unique ID for every code snippet? WTF are you talking about? :-)
Andy Roublev on April 22, 2009 6:47 AMWe 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.
Doug T on April 22, 2009 6:55 AMHey, 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?
Aardvark on April 22, 2009 7:11 AMWhat a great idea! Watch as I, a commenter, take it very seriously, like the absolutely sincere and serious idea it is!
anon on April 22, 2009 7:18 AMI Like It.
josh on April 22, 2009 7:32 AMYeah, 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.
Ryan C on April 22, 2009 7:36 AMThis must be satire.
Having said that, if you are going to post snippets just use gist.github.com and move along.
Anthony Eden on April 22, 2009 7:40 AMHow are you going to learn without Googling and looking at someone else's code? If you crack open the manual or API reference, you get overkill on how each object was designed (written by the object designer), but few samples on how to call them or how a program could link them together.
Let's say you want to make an awesome chicken cacciatore for dinner. Do you :
A) Go to culinary school, learn the most advanced techniques of food preparation, read books on how Emeril cooks, go to the store and read all the nutrition information on each package, use your genius to architect the perfect design of ingredients and preparation?
B) Google "chicken cacciatore" and look at the first couple of recipes, print one, and try it out.
If you choose B, you are still building the meal yourself, and can decide if you like the approach. You make use of shared community knowledge, and you save a lot of time.
Careless copy paste is a bad idea. Even good programmers at times do this and fall into the category of bad programmers at times. It is just a to become a bad programmer. When I am stuck with some thing I usually stick to the help provided, but at times do search on google and use the code, but I use caution to first understand the code written, and aslo make sure that I include the source in the comment, and if there is a email, I send a think you note to the author (I started this recently actually). On a recent project that we worked, unfortuantely I was taken off the project. Now they came back to me because they want more change, and the guy that took from where I left refused to work on the furthur. I was going through the code wirtten and I was shocked. Insted of writing one function for populting different ranges, same code is repeated for 6 different ranges. The code basically actictivates a wroksheet, picks a rnage, activates another workbook and pastes the code. So we have six different fucntions that do the same thing, and making corrections to the code is a nightmare. Worst part is I have cannot run the fucntions seprately. I have to run another fucntion which copies and formats the data, after that fucntion, these sxi different fucntions are run step-by-step. I thought that my design was bad, but after going through, my confidence is back that I could write neat code, and to keep my feet on the ground, and not to get over confident, I attribute this to god and god the credit being on his god side.
I always thought Curly's law made more sense with the Single Responsibility Principle (Every object should have one responsibility and do it well / An object should have only one reason to change) as opposed to DRY.
Eagan on April 22, 2009 7:55 AMI think what a lot of people are missing is that a GUID would cause the snippet to show up in search results, no matter where the code is pasted.
So the internet, in a sense, can have a discussion about a snippet and you can see the most relevant discussions/updates by simply searching the GUID.
Practicality on April 22, 2009 8:09 AMWhat about copyright issues on code snippets? Aardvark mentioned GPL-ed code, but _any_ code I write is copyright me, and you can't use it unless I explicitly say you can. I see SO puts every comment under cc-wiki, but many code sites, blogs, newsgroups, etc that I run across don't even have a license. And with our lawyer all up in my face about documenting where every line of our code comes from, that share-alike clause in cc-wiki would probably freak him out. He'd probably want SO blocked at the firewall!
MarcT on April 22, 2009 8:13 AM@MarkT. You probably should get a new lawyer :)
Practicality on April 22, 2009 8:27 AMThis seems like a workable proposal.
Many have commented that we often or always immediately modify a code snippet found on the Internet. You'll only generate a new, additional ID for *your* version if you publish your updated code on a web site.
It might be a solution in search of a problem as Andy Roublev suggests, but at least it's an easy-to-implement solution.
Zack on April 22, 2009 8:36 AMNit-pick: CodeProject isn't a snippet site, at least not in the sense that you're describing (yes, they do advertise "snippets on their front page"...). Indeed, articles that consist of little more than a code snippet have traditionally been discouraged, although more recently they've opened it up a bit with a blog integration service.
Which isn't to say there aren't snippets. CP hosts an extensive forum system, where questions are often answered with code. Think: web-based USENET.
And, IMHO, that's where your suggestion falls apart. I rarely find code solutions on actual snippet sites; by far the most common come from blog posts, newsgroups, forums like CP, and Q&A sites like SO. None of which *require* any actual separation between text and code, even if they allow it.
Fortunately, it doesn't matter: forget GUIDs, use the URL where you found the code. It's a more direct link back to the source, and holds a better chance of being preserved in an attribution for derivative works as well....
Shog9 on April 22, 2009 8:45 AMOne thing,
"Pagerank means that you're more likely to find code that might be higher quality"
If this were true then googling for apache docs would get you apache 2.2 docs instead of 2.0. Googling for Subversion docs would get you svn 1.5 docs instead of 1.4 (or far older). I've been bit by this when I searched and didn't pay attention to the version in what I found.
I can only assume it's worse when searching for random snippets.
Rob Russell on April 22, 2009 8:50 AMHow would spammers use this to make our lives miserable?
Timothy Lee Russell on April 22, 2009 8:51 AMI'd rather hunt down the code snippet I need than peppering my source code with gross-looking GUIDs.
Marc on April 22, 2009 8:59 AMI can't believe this article. Not a single reference to the legality of pasting code off the web? I never copy code off the web because the copyright is never mine and licensing is usually non-existent. If people start GUIDing all these little code snippets and you paste the GUID into your code, all you have done is made the copyright infringement case against you air-tight.
It's an interesting idea. But it needs a front end where submitters can agree to license out the snippets according to some license, preferably a non-attributed license, viral or not.
jmucchiello on April 22, 2009 9:51 AMWhy not include the original URL instead of the UID? Improvements on the code should appear there.
kikito on April 22, 2009 9:53 AMGood concept, but for this to really work there needs to be a common repository of these guids and who the original source is, as well as a version history and where those versions are located. While its good in concept, to use google to find the id and where it is refered to, the context of the original author is lost, as well as the version history of the code in question.
Imagine if a world changing algorithm gets published to the web with a guid, gets copied to thousands of blogs, then the original author finds and corrects a serious bug and reposts. The problem here will be what is the current version your searching for on the web? If I make a change to someone elses algorithm, what version do I call it? Am I working with the original or someone elses copy? Did they make any changes?
The other problem with this concept is that it requires that everyone plays by the same rules. A single repository can enforce those rules.
We currently have services for sharing and serving images on demand, why not have one for code snippits?
SKamradt on April 22, 2009 9:55 AMBah! The gods (inventors of Unix) decreed long ago that Ctrl-C shall be the INTERRUPT key. Using it for COPY is an abomination.
It was probably accidental, but Apple's decision to use Cmd-C for COPY meant that the proper use of Ctrl-C was easily accommodated when they introduced Unix underpinnings with OS X.
CorkyAgain on April 22, 2009 10:08 AMIf we start using GUIDs for everything we're going to run out of them.
fschwiet on April 22, 2009 10:09 AMVery nice point. I post on a beginner's VB .NET forum and see all too often coders more interested in a copy-paste-email-to-professor solution. It makes me sad because even when I have to find a snippet on a blog, I won't paste it until I know what it does and how it does it.
It makes me sad that things like Curly's Law are so easy to learn, do so much for your code quality, and aren't taught in any schools but Hard Knocks.
Owen on April 22, 2009 10:10 AMI read an article in ACM Queue proposing copy/paste as a method for developing code. The editor would keep track of these actions and prompt for multi-editing whenever code that was copied or pasted is edited. Seemed like a good idea.
Brad on April 22, 2009 10:27 AMI'm noticing a bit of a disconnect here, brought on by language.
Copy and Past is not bad. It never has been. Duplicate code (in the same project) is PURE EVIL. It is the first and last thing you should be concerned with as you are coding. People use copy/past as a shortcut term for "Creating duplicate code within a project", but then others hear that and think "So I can't use cut and paste to move code??"
Bringing in code from another project or the web (be it via copy and paste, a single object or a library) isn't a problem at all.
Cut and past, by the way, is never an issue since you aren't even creating a copy!
Finally, as for the idea of tracking where you got something from the web--I consider a snippit something to learn from. You slide it into your toolbox and keep it if it's valuable.
Once you've learned the principles, why would you ever have to go back to the source? Your version will almost certainly be adapted and combined with other code anyway--I can't remember ever taking code off the web and just leaving even a single line as-is.. (but then I'm not a web programmer, there is a lot more boilerplate there, which is likely an indication of a bad programming language/environment anyway)
If you can't import a snip of code into your own brain, understand and re-write it, however, don't put it in your code!
Bill K on April 22, 2009 10:27 AMInstead of trying to predict whether it's going to be useful, why not just try it for a while and see whether it starts providing some value? If it does, I'm sure we'll hear about it. That's the Agile way! Try anything and see what sticks! ;)
Allann on April 22, 2009 10:31 AMI don't DRY so much as DRYMTO (don't repeat yourself more than once). So I will copy and paste one time and if I find a need to do it once more I will refactor. I don't consider 2 instances to be compelling enough to necessitate a method. Having two instances is also a warning sign for me that there is a design issue. Just blindly methodizing it means you might miss the forest for the trees (two trees in this case).
The larger issue is that you can clutter up an object with too many methods that don't have true utility so adding methods willy-nilly is not the best policy - especially if you're being paranoid about it and making methods for things that *might* be repeated some time in the future.
I try to add the obvious stuff and then see how the code grows on it's own - refactoring as I go.
Also, there are languages (I'm looking at you, Java) that force you to write a lot of boilerplate code. Why wouldn't you copy/paste that stuff? If that's the idiom of the language then trying to refactor it would just cause a lot of confusion for the poor sap who has to read it later.
Just a side comment, the ergonomics of the command key vs the control key are one of my favorite things about the Mac keyboard. I don't see why it is 'heathen' - I always find it less of a stretch to reach with my thumb vs my pinky for control. YMMV.
Jeff B on April 22, 2009 10:39 AMHmmm...
Programming with GUIDs. The worm has certainly turned.
Think of all the tools you would need for this language.
First you will be programming with only GUIDs, but will then realize that there is a simpler way to program by using an GUID-assembler. After a few years of this, somebody will then create a higher level language that will abstract out all these numbers and letters so that you eventually end up with a English-like programming language called "G".
You think formatting wars are a problem. Already people are creating a war about the actual algorithm to be utilized to generate the reference (@Joe Chung).
Is there really a problem with copy and paste? What kind of paradox is this? In order to solve a problem you actually end up with the same problem (See first part of comment).
Joseph Gutierrez on April 22, 2009 10:41 AMThe argument that I always use to force myself to not copy/paste is that writing it improves retention: I'll remember it better even if I transcribe it verbatim over copy/pasting it.
Steve-O on April 22, 2009 11:24 AMLike any other programmer, I constantly am googling to find solutions for minor problems I'm having. Often, that solution is found in a ten line code snippet. However, in all my years of programming, I cannot recall a time when I pasted that snippet into my program unmodified and left it at that. I use the snippet as a starting point, but then I always end up refactoring the hell out of it, such that it usually ends up hardly resembling the original snippet. So in my case, I don't really see how ad-hoc version control would help me; by the time I'm done with a pasted code snippet, it usually bears little resemblance to the original copy.
EvanM on April 22, 2009 11:31 AM<a href="http://www.informit.com/articles/article.aspx?p=1193856">Donald Knuth said this in an interview</a>:
"I also must confess to a strong bias against the fashion for reusable code. To me, "re-editable code" is much, much better than an untouchable black box or toolkit. I could go on and on about this. If you’re totally convinced that reusable code is wonderful, I probably won’t be able to sway you anyway, but you’ll never convince me that reusable code isn’t mostly a menace."
Also, there is some neat C-code at
<a href="http://ccan.ozlabs.org/">CCAN (Comprehensive C Archive Network</a>
"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?"
No. ???
"Just a side comment, the ergonomics of the command key vs the control key are one of my favorite things about the Mac keyboard. I don't see why it is 'heathen' - I always find it less of a stretch to reach with my thumb vs my pinky for control. YMMV."
Do you use two hands to cut/copy/paste? Because otherwise I have difficulty seeing how that's not a contortion for a person with regular sized hands. And maybe a Mac keybinding subtly trains you to do it two handed and other bindings train you to do it single handed. If I just rest my hands naturally on a keyboard, with my fingers not specifically moved to the home row in "typing position", my left pinky is on left ctrl and my left pointer is on C.
Ens on April 22, 2009 12:47 PMCopy isn't so evil, Paste is the big culprit...
Code examples on the net should be rendered out as Images so you could look at them, use them as a reference, but aren't able to paste them directly into your own code.
Pasting someone elses code incorporates EVERYTHING from the source. Variable naming, object selection, poor algorithms, poor performance.
And the developer doesn't learn much (if anything at all) by doing it.
The final code is the end result of a virtual blueprint that the developer has designed in their head. Grabbing bits and pieces of other peoples stuff is certainly going to clutter your own model and/or change its essence.
Do you consider developing software is an Art? Or a Process?
Copy/Pasting the Mona Lisa’s head is not proper in the former but encouraged if it’s the latter and you are baking a cake.
Jeff: add a concise method for version tracking (including branches), and then I think you've got something good. Without version tracking, I doubt any of the benefits of your system will hold up in the real world.
I kept waiting for the part about eating script kiddies, but it never came...
I would definitely appreciate a database of random code snippets. There are so many interesting little coding tricks and algorithms, but they're scattered all over the internet and buried in larger applications.
A system sort of like Wonderfl (http://wonderfl.kayac.com/) would be pretty neat. They basically just have a text box on every page where you can randomly fork the code and store modifications. Other people can see the evolution of the apps through forks and revisions.
James on April 22, 2009 1:43 PMReal programmers bind copy and paste to their extra mouse buttons.
coward on April 22, 2009 1:50 PM@coward
Real programmers don't own a mouse at all... :)
HB on April 22, 2009 2:00 PMThis is the most retarded idea I've heard in my entire life.
Either that or Atwood is trolling us, AGAIN.
Steve on April 22, 2009 3:07 PMI'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.
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!
Erik Karulf on April 22, 2009 4:25 PMI know others have said it already, but I think gist is what you're looking for.
Aren't you just a little sad when you find out someone has already had your great idea?
Leah Culver on April 22, 2009 4:51 PMI once worked very briefly at a company where copying and pasting were the norm. My team lead would actively copy and paste entire classes from previous projects and send it via *email* to me (no such thing as source control). Then I would just 'plug and play' (modify lines 8, 45, 58-65) and the code would work 'beautifully'.
github's gists work well, but your idea is nice too as it does not rely on a single vendor.
Austin Wise on April 22, 2009 6:47 PMI really thought you off your rocker until I saw a comment pointing out the title starting with "A modest proposal..".
For all you taking this seriously, what do you think people are going to do with these GUID's? Go back and update their copypasta code?
I hate the Misfortune in a previous job of working with a CPR programmer. Copy Paste Rename a few variables and you are done. It was so great trying to debug imaging code inside a database project. (*NOT*) It seemed this guy just renamed variables based on what he was doing at the time with absolutely no understanding of the intent of the code.
Paul on April 22, 2009 9:11 PM> (yes, I know that in OS X, the keyboard shortcut for cut and paste
> uses "crazy Prince symbol key" instead of control, like God
> intended.
So... let's assume you're a real programmer, who uses real servers, with a real command line.
You SSH into your server, do a locate for a particular file, then wish to copy that filename.
On a mac you select that text, hit command-C, and bang it's copied.
On a PC you hit control-C and bang you've terminated some process...
That's if you can remember to right click first and select "Mark". Blech.
The Apple convention for the command key to be completely local to the computer makes sense. The PC convention to occasionally send control keys and occasionally capture them makes no sense at all.
As usual Microsoft copies Apple, but misses the point.
Ben on April 22, 2009 10:30 PMThis GUID thing is never going to work.
As for copy-n-paste, it's a necessary evil I've grown used to tolerate. I try to avoid it whatever possible, considering it as you say, indicator of design error, but it's sometimes better than the other way around, such as:
bool func(const float param) { ... }
bool func(const int param) { ... }
In cases like this, I often resolve to copy-paste. The other way around involves templates and I often see them as a far too generic tool (you know they're still somewhat "not quite right" in C++). Having those two funcs allows to make the limitation more explicit, which I consider to be an advantage in the long run.
I'd like if some IDE could have a special copy-support to track those things even inside the same file. I know some do indeed have a "snippet management" system but I'm still not accustomed to it (it doesn't seem to work as I would like to).
MaxDZ8 on April 22, 2009 11:33 PMBy copying and pasting you find and get fast what you need. If the code isn't to the standards and designs you are using, you have to format it first. Also of course you should make subroutines instead of pasting, but sometimes you just copy the general block of code that calls some subroutine. For example you copy a database call, just change the call details, but you get the way that database calls are done in that particular project. There are many ways to call database starting from simple calls to object mappers and the programming language can vary too. Hard to just remember how the calls are done in what project. Better design makes the process easier as you don't have to guess what the programming standard and method is this time.
Silvercode on April 22, 2009 11:51 PMPerhaps we need a simple to use, open source, well documented, software library.
Sort of like the Enterprise Library, only easy to use, because when I try to use the Enterprise Library I end up googling the internet and copying and pasting.
I'm very leery of copy/pasting code. I don't want code that I don't understand running. That's how bugs get introduced, and I like writing code that doesn't have bugs in it.
Sometimes though, you're left with no choice, when working with undocumented or poorly documented APIs. In which case I hold my nose, and also experiment with the code until I understand what each line is doing.
Bill on April 23, 2009 3:15 AM"you (or someone you work with) isn't falling into the trap"
Grammar nazi here - "you () AREN'T falling"
Regis on April 23, 2009 3:44 AMUm guys, an URL (therefore a URI) *is* a GUID.
Yes, when I 'borrow' code that I see to be a general-purpose snippet I also add a link in the func declaration comments detailing where I found it. Usually because documentation and analysis there is a little more in-depth than is needed for my single-use routine.
If php.net goes away one day, then maybe that will no longer help those who come after me, but at least I tried. Anything weird like a random GUID will just be ignored or discarded out-of-hand.
As to the article, my thoughts were that the first 2 strikes *against* code re-use listed above were just a "lack of betterness" not actual "bad" points against.
# If the author improves the code, you're not likely to get those benefits.
# If you improve the code, you're not likely to pass those improvements back to the author.
Well no, but adding an intentionally cryptic comment doesn't give you that benefit either.
The other three strikes against are valid - as to the actual quality of the code. But the first two are not.
Drop an URL to the source you copied from. Then move on.
We copy snippets because we don't want the overhead of full-blown libraries and dependencies and extra library files. If it's less than a screenful - and does the job - copy & paste away.
Just wanted to agree with proposal for "snippetoverflow.com" - stackoverflow is a great thing ;)
ivucica on April 23, 2009 5:53 AMHere's what I wrote about this earlier this month:
"Across the unimaginably vast stretches of the public Internet, a tentacled, grotesque being is destabilizing every layer of creation from core routers to half-finished Myspace pages. It’s not a malicious virus. It is not a nefarious plan hatched by a cackling mastermind. The greatest threat to the entire continent of information technology is available on every computer with a few swipes and clicks of the mouse. The ubiquitous, self-replicating destructive force which might usher in the end of everything is the tragically thoughtless copy-and-paste."
From http://www.robbyslaughter.com/blog/?2009-04-06
Robby Slaughter on April 23, 2009 6:09 AMI commented earlier about implementing this capability into my code snippet manager, Snip-It Pro.
I couldn't wait until the weekend, so I ended up implemeting everything last night and I just published an updated beta version on my blog. More details and a download link here:
http://www.mtelligent.com/journal/2009/4/23/snip-it-pro-20s-newest-feature-auto-commenting.html
I ended up implementing four types of "auto commenting" for snippets: The Guid ID that's unique to each locally stored snippet, a Reference Url that can be tagged to a snippet, the user name of who inserted the snippet and the Date the snippet was inserted. Each of these can be toggled as an option, so you can set which ever auto comment types you want.
Thanks again for the idea, Jeff.
David San FIlippo on April 23, 2009 6:43 AMI devised a method to deal with this problem a couple of years ago.
It consists of a ranking system for programmers, expending on the available IDE and language features as one's experience progresses.
you can find the details here:
http://subjectively.blogspot.com/2007/07/role-playing-gmes-we-play.html
and while i agree that guns don't kill people, i do think people need some training before they are allowed to use a particular weapon...
Yossi on April 23, 2009 7:17 AMCopy & Paste is only as dangerous as the developer.
I find myself copy pieces of code from other projects where I have already solved something.
Guns don't kill people. Bullets kill people
Red on April 23, 2009 11:09 AMOh boy, copy and paste some stuff found on some site (could be code project) into the product ... these snippets are almost never product quality, I mean even where's the error checking, and get semi blindly pasted into product code. The product failures which result, the aggravations which results, the misdirection of PHBs which results, ... unbelievable, what a complete waste of time and money.
These snippets could be really useful for learning, to give programmers a bit of a "how to", but in the end have net negative value, becuase there are so many more copy/paste idiots in the world than any other kind of programmer. I know, first hand, I have the displeasure of working with a number of them, because they're cheap.
Quote from the back of an O'Reilly book I received in the mail today:
"Safari is designed for people in a hurry to get the answers they need so they can get the job done. You can find what you need in the morning, and put it to work in the afternoon. As simple as cut, paste and program"
Are all O'Reilly readers / Safari users bad programmers?
Good post all the same ...
mosaic on April 23, 2009 12:41 PM> Are all O'Reilly readers / Safari users bad programmers?
Nope. That would only be all of the "O'Reilly readers / Safari users" who approach software development as "simple as cut, paste and program".
:-)
But copy and paste lets you put in crazy characters easily. ‮ Like U+202E. Which has the ability to reverse all of hte text so that it reads from right to left instead of left to right. This would be hard to do without cut and paste.
trouble maker on April 23, 2009 1:55 PMA good idea, but I want to reiterate that github gists are the real solution here as it allows anyone to edit and improve a snippet and a clear and easy to follow revision history is available for everyone to see.
Of course if github goes down there's your single source of failure.
Max Howell on April 23, 2009 2:13 PMIs there any reason to use your own NSTimer when there are the UIView animation blocks?
(I'm aware this is beside the point of the article but interested none the less)
- (void)fadeOutWindow:(NSWindow*)window{
float beginAlpha = 1.0;
float beginAlpha = 1.0;
window.alpha = beginAlpha;
[UIView beginAnimations:@"transition" context:nil];
[UIView setAnimationDurration:0.02];
window.alpha = endAlpha;
[UIView commitAnimations]
}
Just to go down the overthinking path.
It should be reasonably easy to create a repository where someone could "own" a code snippet and people could (with annotations like you suggested) copy these snippets.
If a third party modified this snippet for their use for whatever reason, they could then submit this code which could be accepted/rejected by the original author (or current owner) and if rejected could then be submitted as a new snippet related to the original.
Then with this repository, we could build a plugin to our ide of choice to automatically get the latest version of our snippets and apply them if we choose.
I suggest snippets.stackoverflow.com :)
Instead of a GUID, I'd propose reverse dns + comment :
// codesnippet:com.codinghorror/fade-out-window
- (void)fadeOutWindow:(NSWindow*)window{
// code
}
}
This makes it Google-friendly : you can now search for all snippets containing 'fade', 'window', or originating from codinghorror.
Patrick Geiller on April 23, 2009 4:45 PMCopy paste programming is real life solution to problem with sophisticated libraries and frameworks.
It is often easier to copy some code, that does what you need, and later customize it, than to use some general library, that has many things you won't use. Also - advantages of using library are often very minor, and removing dependencies and making build your code easier are often more important that getting security fixes.
Also - frameworks and libraries imposes some design choices on you, that sometimes are wrong for your project. Changing them means forking a library and if you want to receive updates from trunk, you have to maintain your fork.
It's a lot of work and when all you need is 1% of functionality of library, it's too much to use that library.
In languages like python, ruby or perl this problem is mostly solved - solution is called cookbook/cpan/ruby gems.
I think that a lot of appeal of python for me is that it enables easy "copy paste" oriented programming. And I mean it in a good sense.
Good, complete solution to some problem in c++/java/c#/other such languages often won't fit to one blog post. And if it will, it would be too big to easily copy paste it to your code, and (the most important) - it will be too big to read and understand before copying (I mean - 2 pages of clean code I can read, 20 pages of code with 15 pages of boilerplate in it - not so easily, and at this point using library doesn't seem so bad).
In dynamic languages easy things are easily done in small amounts of LOC. Go figure it - http://code.activestate.com/recipes/tags/
odrzut on April 24, 2009 1:48 AMBen (about how Mac copying is better than PC copying):
In Unix world you select text and bang - it's copied, then you middle click somewhere and bang - it's pasted.
Beat that :)
odrzut on April 24, 2009 1:54 AMWhat's wrong with a simple URL to where you found the code?
Alex on April 24, 2009 4:38 AMMonkeys with guns also kill people.
c on April 24, 2009 4:47 AMWhen the title of this blog post was "a modest proposal," I have to admit that I was hoping for something along the lines of Jonathan Swift's "A Modest Proposal," laced with pointed sarcasm.
Still, not a bad modest proposal. It would certainly be nice for updating the various Java snippets I find when searching for how to do something.
Katie on April 24, 2009 6:27 AMi'm blown away by the simplicity. top marks!
It's the simplest thing imaginable and i think it's worth trying it out.
Steve Pavlina (life guru who began as a shareware guru) recommends using a 30 day trial to decide on a potential life-habit.
i recommend that SO quietly injects a guid into each code snippet by adding a //guid comment to each code tag entered.
And ideally the guid comment portion would be colored in a very very soft grey -- so that it is deliberately hard to read. What Tufte calls minimal differentiation or something like that. Your eyes will see it only enough to detect that it is present, and to recognise that it's a guid. But not enough to squint and bother reading it.
Once the entire world has learnt this convention from stack overflow, they would all adopt it for their own blogging.
It would be built into all blog engines and it would be recognized/searchable by all IDE's. (Including the browser-based ide's which dominate our future).
And then, checkmate. SO wins. Something.
Nice article Jeff ;-)
lb
secretGeek on April 24, 2009 7:32 AMImpressed with this little idea of GUID.
DRY's main lifeline is the Copy Paste.
Just only only the code, also design pattern, ideas etc.
- maheshexp
maheshexp on April 24, 2009 9:30 AMI mean no offense and perhaps I do not understand your article, but are you talking about two different kind of copy and pastes here?
The first one you site from IEEE appears to be about copy and pasting code that you did in many places instead of having a routine that handles it once. So, instead of having a routine that cleans up a string in five different places, copied and pasted each time you needed it, you have it in one place and all five places reference that one place.
The second set of copy and pastes is about copying and pasting from other sources because you don't have it in your code. And, that this could be bad because of x,y,z.
I think the first one is good. The second one has the inherent problems you pointed out and was the inten of your article. Mostly, for me, it is not understanding the code right off, but using it anyway.
In short, I think the first citation has nothing to do with the intent of your article. From that article, "Aside from the invention of the computer, the routine is arguably the single greatest invention in computer science. "
Sorry if I misunderstand.
john on April 24, 2009 9:43 AMI copy, therefore I paste.
Steve on April 24, 2009 2:30 PMSimon Wright: "Because it's the wrong tool for the job. A hash will change every time the snippet is changed, whereas the GUID will remain constant. This idea relies upon having a reliable, life-long identifier."
On the contrary, an ID that changes if the code it identifies changes is actually the right tool for the job. It's just that Jeffs scheme completely ignored the problem of versioning.
In the real world a majority of people get a snippet and then make some sort of change to it for their specific needs. How long do you think it will be before there are 100 "versions" of a popular snippet that have been posted on blogs, all with the same GUID?
2+ different snippets with the same GUID, just doesn't work. Using a SHA1 along with some other versioning is is a better solution.
AJ on April 24, 2009 6:27 PMThat's not the simplest thing that could possibly work. It's overenginering. A code snippet found on the web already has a unique identifier. The URL at which you found it.
The poster of the snippet no longer has to jump through the additional hoop of publicizing a GUID along with the code, and when looking for the source, I no longer have to use Google to find it. I can just copy the URL directly into my browser.
jalf on April 25, 2009 4:53 AMThe article he linked to said the simplest thing that could possibly work. Jeff said the STUPIDEST thing that could possibly work.
AJ: A SHA1 hash would not solve the problem, so it's clearly the wrong tool. The whole point of using a GUID is to find 2+ different snippets with the same GUID. Remember, the goals here were the following:
1. Identify the author (GUID works, SHA1 works if there's no alteration whatsoever, including tabs vs. spaces type formatting, and point 3 coming up...)
2. Find new versions of the snippet. Hash algorithms fail here.
3. Fold new versions back into the original snippet. Not only do hash algorithms fail here, but it also destroys the ability to use SHA1 for point 1, identity of the author.
Now, a URL to the author's site is in fact a type of GUID, though not conformant to the UUID standard.
The real problem here is the problem of GUID collision of ultimately unrelated functions caused by, ironically, copy-pasting. It might work better if there were a GUID cascade, such that every time you pasted a snippet with a GUID, a new GUID was prepended, so you get a sort of version history.
And then, of course, there's the problem of somebody just deleting the GUID.
Ens on April 25, 2009 11:08 AMI don't think I have ever found something I could use without modification. Maybe, I just write Java in any language, and so I always modify semantics, like indentation and variable name. The situation I always encounter runs something like this: look and look and look, and after hours of looking, write my own thing anyway. Besides, I consider myself a perpetual student anyway and enjoy the challenge of coding from scratch, scary isn't it.
Michael on April 25, 2009 11:23 AMJeff,
Another problem with copy-and-pasting snippets from the internet is that you think you understand how to use an API, but actually you miss a lot of important information that the original writer may have been aware of.
If you remember my studies with the Java samples, this was often the case. (http://www.cs.cmu.edu/~udekel/papers/udekel_emoose_icpc2009.pdf)
Uri on April 25, 2009 6:50 PMWhy not start with http://stackoverflow.com ? that seems like a right place for that kind of thing. http://gists.github.com has a great version control for snippets and allows you to group multiple files/snippets as one gist.
However, what would really be better is, some way to point to relevant articles on the internet (using google or something else more relevant,intelligent) that might explain what the code snippet does. Encouraging the snippet poster to point to such articles would be a huge plus. So, instead of being
//Magic Code
while (!magic){
//do something cool
}
//fire the missiles
We have:
START OF CODE --------------------------------
----- UUID here ----
//Magic Code
//::description:: The code was taken from blah blah blah... that does blah blah blah and uses QuickSort
while (!magic){
Reflections.xxxx
Hibernate.xxxxxxx()
}
---------------------------------- END OF CODE
Links:
Reflections API
Hibernate
Relevant Discussions:
StackOverflow- How do I make Reflections API go Kazooo?
I guess you get the idea. We probably have most of these things already. We just need some sort of push in this direction so that we can all benefit from it. Of course, no matter how much we like RAD, Rome wasn't built in a day. Yes, even with RoR
Ever paste several hundred lines of your clipboard to a shell prompt,
by mistake (ouch, especially if it is a production server)?
If what you paste is from the scroll buffer of your terminal,
(for example an ssh session managed by 'screen'), then a shell
prompt that starts with '#' will save you some grief,
Small error: "advocate disabling cut and paste completely" - Don't you mean "COPY and paste" ?
I don't think anyone takes issue with CUT and paste, as it's pretty essential for refactoring.
Alex on April 27, 2009 12:52 PMShows that computer geeks don't read enough literature. As a 'writer' you should know better than to co-opt the title of 'Modest Proposal' and not even attempt a Swiftian satire. Then again maybe you did, but we just never noticed >:)
http://en.wikipedia.org/wiki/A_Modest_Proposal
He's proposing a system of manual version control on code ripped from blogs to legitimize 'coding via Google', and you think it's not satire?
AndyL on April 28, 2009 6:41 AM"Not so swift" is a pretty good handle for that comment.
anon on April 28, 2009 1:06 PMGreat idea, but it'll never work. Not because it's not a great idea, it is. It won't work because the folks that are making code snippets aren't capable of generating GUIDs. ;)
Scott Hanselman on April 28, 2009 1:10 PMJeff-
I like the idea, but there are a couple of kinks to work out. For example, when I search for the sample snipet you posted here (1c125546-b87c-49ff-8130-a24a3deda659) I get a lot of results (between 32 and 68 depending on your search engine of choice), so I am not sure which is the site of authority. However, if I had to pick I would go to:
(yes that was really in the search results)
-Clarkin
Larry Clarkin on April 28, 2009 4:49 PMAssigning GUID's in some form to code snippets for copy/paste reuse is silly and laughable but certainly doesn't go the full 9 yards to make it a Swiftian satire. Judging by all the comments, it's not such an absurd idea after all to some people. Monolithic enterprisey WTF's are being dreamed up as we speak.
So this modest proposal is in no way in the same form as Jonathan Swift's 'A Modest Proposal'. This modest proposal isn't in the same ballpark as suggesting poor people sell their children for food, nor is it a comment on the state of the industry.
I think Jeff may have created a variant of Poe's Law,
Without a winking smiley or other blatant display of humor, it is impossible to create a parody of Coding Horror that won't be mistaken for the real thing.
Steve W on April 29, 2009 5:10 AMI guess if you're poor and heartless enough, you might seriously consider selling your children for food, and if you're a shortsighted enough programmer, you might think that putting GUIDs on big slabs of source code and copying them around the web is a good idea.
anon on April 30, 2009 12:05 PMRegarding the command-key on the Mac platform. They don't use Ctrl for all their key-commands, for a damn good reason:
"One advantage of this scheme, as contrasted with the Microsoft Windows mixed use of the Control and Alt keys, is that the Control key is reserved entirely for its original purpose: entering control characters in terminal applications." (from Wikipedia article on "Command key")
amatecha on May 4, 2009 11:15 AMI tried posting this a while ago, but it seems the message did not go through (or perhaps there's some moderation system in place that I didn't notice).
This explanation of the Command key on Mac systems is written on the Wikipedia article for "Command key":
"One advantage of this scheme, as contrasted with the Microsoft Windows mixed use of the Control and Alt keys, is that the Control key is reserved entirely for its original purpose: entering control characters in terminal applications. (Indeed, the very first Macintosh lacked a Control key; it was soon added to allow compatible terminal software.)"
So, in fact, the adoption of a new unique key makes complete sense, because, as you should already know, the Ctrl key is used for console applications. Makes complete sense!
amatecha on May 4, 2009 12:02 PMActually Xerox PARC would be God, Ctrl+zxcv were Xerox PARC norms that Apple copied except the use of the command key instead of Ctrl.
pataphysician on May 5, 2009 11:21 AMthat GUID stuff is ingenious.
wizzard0 on June 25, 2009 9:25 PMI think that it's nice. What needs to happen is the central system needs to have a list of derived snippits when you view one. I.e.:
1) You copy a snippit, with the GUID.
2) You edit that snippit to add error handling or what have you.
3) Paste the snippit, WITH GUID, back into the snippit site.
4) The site sees the old GUID, creates a link between the two snippits, and replaces it with a new GUID.
Now when you view the page for either GUID, you see the link to the other {parent,derivation} of the snippit you looked up.
Daniel Danopia on August 19, 2009 9:08 PM| Content (c) 2009 Jeff Atwood. Logo image used with permission of the author. (c) 1993 Steven C. McConnell. All Rights Reserved. |