A recent thread on SitePoint’s forums discussed how best to learn from other people’s code. For many people learning by example is the optimal way to pick up a new programming language, idea, or technique. But finding that ideal piece of code to look at and learn from isn’t always easy. That’s where code search engines and snippet libraries come into play.
The seven listed below are the top places we could find that will help you locate the piece of code you’ve been looking for. But as always, if we’re missed any, please let us know in the comments below.
Popular open source code repository and version control web service GitHub launched a new Code Search feature today. Even though it’s the new kid on the block, GitHub has become super popular in a very short space of time and hosts a ton of public repositories. In a blog post, GitHub’s Scott Chacon classifies the amount of code searched as “a lot.”
Krugle claims to search over 2.5 billion lines of open source code, making them one of the largest source code search engines on the web. They claim to reach over a third of all developers world wide, and their enterprise search product powers code search for Amazon Web Services, IBM developerWorks, Collab.net, SourceForge.net, and Yahoo! Developer Network.
Koders, which claims to search over a billion lines of code, seems to be a favorite among Ruby programmers. In a press release announcing the company’s acquisition by Black Duck Software last month, the site revealed the Ruby searches were up 20-fold over the past four years and have passed PHP, Perl, and Python. Ruby is now the fourth most searched for language on the site behind only Java, C/C+ and C#.
Codase is the little guy on the block, weighing in at just 250 million lines of code searched, according to the site. That might be because development on the search engines appears to have stalled a couple of years ago. Codase focuses mainly on Linux C/C++ open source projects and appears out of date, so unless that’s what you’re looking for, there are better options out there.
With over 13,000 users contributing to over 5,000 code snippets, the DZone Snippets library is a great place to find good code examples. It’s certainly not the easiest site to browse, but there is some gold hidden for those who look. Exercise patience and you may find exactly what you need at DZone’s site.
Snipplr is a nicely designed code snippet library that has less users than DZone (just under 10,000), but more code snippets to crawl through — almost 8,000. And it seems to be gaining steam — more than half of the site’s snippets were added in the past year, even though the site is over 2 years old. Oh, and they have handy plugins for Textmate, Gedit, and WordPress, and bookmarklets for Firefox and Safari. Very nicely executed.
Last, but certainly not least, is Google Code Search, the grand daddy of all code search engines. While the people at Krugle and Koders may argue the point, and Google doesn’t say how many lines of code they’re searching, when you’re talking search, Google is always the team to beat. Their code search engine searches a ton of public source code repositories — including GitHub, Sourceforge, and their own Google Code site. And Google supports a ton of languages.
BONUS: O’Reilly Code Search: Book publisher O’Reilly lets you search over 123,000 examples covering over 2.6 million lines of code taken from their books. Almost 700 of them.