CSAIL Publications and Digital Archive header
bullet Research Abstracts Home bullet CSAIL Digital Archive bullet Research Activities bullet CSAIL Home bullet

link to publications.csail.mit.edu link to www.csail.mit.edu horizontal line


Research Abstracts - 2007
horizontal line

horizontal line

vertical line
vertical line

Finding, Using, and Sharing Source Code Snippets

Max Goldman & Robert Miller


We are interested in a variety of ongoing attempts to 'decriminalize' copy-and-paste as a tool in the hands of programmers. Programmers copy-and-paste frequently for different reasons, and there are an increasing range of tools and systems to support this activity and address the issues that have made copy-and-paste a 'criminal' activity in the minds of disciplined programmers.

When working with large APIs, programmers often write 'glue code.' This code implements common patterns of objects creation and use, or of filling in all the right parameters and choosing appropriate options to achieve desired effects within a range of possibilities provided by the API. Contrast this sort of programming with the historical stereotype: never-before-seen custom designed code that may use known standards and large-scale patterns, but which is essentially unique. 'Glue code' is easier to write by altering a copy than rewriting from scratch.

Regardless of the quality of a programming language, there will still exist abstractions not supported by that language, or features whose abstraction is difficult and unwieldy. For example, a certain sequence of conditional control flow may be duplicated across several related methods. Abstracting such control flow sequences in the most widely-used object-oriented programming languages is possible (consider the Strategy design pattern), but only with verbose, obtuse results. Copying-and-pasting may be the preferable alternative.

There are many existing web sites devoted to the collection, sharing, and even improvement of source code snippets, the sort of code that is copied-and-pasted. Tools that start to integrate snippets into the programmer's workflow have begun to appear. Many open questions, however, remain.


We begin by understanding how snippets are shared and reused by different software projects. Some sequence of lines of code, a particular object structure, or a micro-level design pattern that today appears as a snippet in multiple projects might have started as code written for a specific task in a particular project that was republished by another programmer as a template for others to copy; or it might have begun as an incomplete example from some API documentation that was expanded with comments and alternatives by successive programmers working with that API.

We are also examining how code snippets can be extracted automatically from existing source. Existing approaches have focused on, for example, finding code to retrieve an instance of a particular type [1]. Snippets whose principal results are side effects, that are not in themselves complete code but rather templates to be concretized by a programmer, or whose output is more than simply some unspecified instance of a particular type — these snippets are a far greater in number, richer in variety, and are more difficult to identify and extract whole.

Finally, we are building a repository of code snippets and designing a user interface, implemented as an Eclipse plugin, that allows developers to work with snippets as first-class tools in their repertoire of organization and abstraction devices. A compelling snippet user interface must allow programmers to find snippets, incorporate them into existing code, connect inputs and outputs appropriately, modify them, refactor them, or remove them. With a connection to a central snippet repository, the interface must provide not only a search tool that pulls snippets into a programmer's project, but means to update a snippet's code when it is improved or when bugs are fixed, and ways for programmers to push back to the repository changes, refactorings, or fixes they wish to share.

A snippet on the web and in the Eclipse IDE

[1] Sahavechaphan, N. and Claypool, K. XSnippet: Mining For Sample Code. In Proceedings of OOPSLA 2006, pp. 413--430.


vertical line
vertical line
horizontal line

MIT logo Computer Science and Artificial Intelligence Laboratory (CSAIL)
The Stata Center, Building 32 - 32 Vassar Street - Cambridge, MA 02139 - USA
tel:+1-617-253-0073 - publications@csail.mit.edu