This post was previously published in the TechRepublic Software Engineering Blog in November 2011.
Member Mat O'Hare posted this great topic idea on TechRepublic's Facebook page:
"Another suggestion for the TR blog: What code-sharing facilities are good. I signed on to The Code Project to get something. It didn't suit me so I wrote my own. Now I'd like to put it somewhere. It seems Google has a similar facility. I've also seen others that are specific to programming platforms. What are good things to consider for places to share code?"
I haven't found any great sites for sharing code. Microsoft used to run a great site that made it really easy to upload a discrete piece of code with some attached descriptions. There was also something a long time ago called "Code Jack," which was like a P2P tool (think Gnutella or BitTorrent) that let you put up "artifacts" (ranging from small snippets of code to entire applications) and search them locally and on other users' machines. Both of these options were just right, but are since long gone.
There are sites that let you easily answer questions and include code in the response (or question). Some sites are like online magazines that are optimized for programming topics. And there are plenty of public code repositories. You could, of course, post code to your own website or blog.
To me, the ideal code sharing site has the following characteristics:
- Powerful search that is optimized for inspecting code, and preferably language aware
- High-quality system for viewing code online in a visual format that is easy to understand
- Mechanisms for keeping the code up-to-date, but also maintain version history (version control)
- A good system for writing about the code, not just posting it with a basic description; code annotations and such
- The ability to parse the code for embedded documentation and automatically generate it and post it to the site when new code is posted
- Can handle snippets just as readily as fully pieces of code
- Gives you control over the license used
With these criteria in mind, let's look at our options.
If you don't mind trying to work within the Q&A format, the only site worth considering is Stack Overflow. They have a critical mass of usage that cannot be beat. It is not great for working with code bigger than a snippet, and at best, you are going to be posting up your own question and then answering it with your own code. But it has high discoverability via search, and it is someplace that other people often visit. Unfortunately, Stack Overflow gives you no control over what license is used.
While not a true magazine like Dr. Dobb's or MSDN Magazine, the best bet here is The Code Project. They do not have facilities for code management, but if you want to get a small piece of code online with a detailed explanation of how it works, The Code Project is a good choice. There are, however, a couple of problems here. For one thing, the site is designed for articles and not big chunks of code. People are going to be rating your article based on its usefulness, and just trying to put up a lot of code won't do well. In addition, you have no control over the license.
The better online code repositories make it easy to browse for code and examine the source, as well as organize an actual project. On top of that, they allow you a version control system, user management, and project management facilities. In addition, you control the license more often than not.
One drawback is the facilities for writing about the code are not so hot. Another is that in many cases they are more complex than a simple "post, describe, and save" process. Your best bets in this space are SourceForge, Bitbucket, GitHub, CodePlex, and Google Code.
Self hosting is what I have chosen to do. I already have a small website for my personal business, so it is not a big deal for me to throw snippets of code, entire applications, etc. up there with the writing about it at my discretion. And, I have total control over the license.
The downside is I don't have versioning or code browsing support. I suppose if that was important to me (I almost never update the code there — it's small stuff that's good to go), I would use a code repository (probably Bitbucket) to hold the code, and then link to it from my site's write up. For what I do (one or two pieces of code a year, outside of what gets posted here on TechRepublic in articles), it is just fine.
My wish for a long-term solution
I'd like to see something like a Wikipedia for code, where the topics are the need being addressed and sections would show how to handle them in different languages, with links to full, version controlled code in the descriptions. I'm mildly surprised that no one has done this; it's just a matter of combining the open source Wikipedia code with different Web front ends for code repositories, and as time goes on, blending the two to be more seamless. This sounds like a great project for someone with more time than I have at the moment.
The route you take is up to you and your needs. I look forward to reading members' suggestions in the comments.
Justin James is an OutSystems MVP, architect, and developer with expertise in SaaS applications and enterprise applications.