Searching for Code Snippets in an IDE


When writing a program, developers of all skill levels routinely and repeatedly turn to search engines for answers to questions; questions like “how do I read a file line-by-line”, “read an RSS feed”, “calculate the difference between two dates”, “delete whitespace”. More generally, how do I do X, where X is a small programming task that someone – surely – has already done before, and probably already shared a solution to.

Enter the Code Snippet Search tool from Microsoft Research, now available to C# programmers as a Visual Studio add-on. It is the outcome of a research project that Yi Wei (a former member of our group who studied automated testing and fixing, and whose flat in Z├╝rich I happened to takeover!) has been working on since 2011, and today is looking like a very promising tool. The basic idea is to streamline the process of finding reusable C# code snippets by integrating the experience into the Visual Studio IDE.

Automagically identifying, adapting, and inserting the code snippet that does the job

Automagically identifying, adapting, and inserting the code snippet that does the job

Accessed by triggering Visual Studio’s IntelliSense, the tool accepts queries in natural language (akin to the examples in my opening paragraph), processes them using Bing, then attempts to identify the most appropriate code snippet in an index built from MSDN, Stack Overflow, Dot Net Perls, and CSharp411 (taking into account various indications of quality, such as the number of votes from a particular community). Most impressively, the tool doesn’t just dump the identified code snippet and send you on your way; rather, it attempts to modify it to fit the variables and context of your program, with some reasonable success rates reported.

The Visual Studio blog has a nice overview of the technology alongside some screenshots of the add-on. If you want to see it in action without having to install the IDE, there is a web-based version (albeit with a slightly different interface) that you can play with, as well as a 2-minute video demonstration.

Support for other languages is planned to come later (I hope, apart from the obvious targets, that LaTeX finds its way onto the list!); but in the meantime, it will be interesting to observe how C# developers react to and use this technology in practice. Congratulations to Yi and his colleagues on their success!