“Copy-and-paste programming” — writing code by itera-
tively searching for, copying, and modifying short blocks of
code (< 30 lines) with desired functionality [4] — appears to
be a staple of opportunistic programming. In our laboratory
study, all participants employed this practice extensively.
One high-level reason for this is obvious: When people are
working in a domain in which they are novices, copy-and-
paste programming is simply easier than trying to come up
with the code by oneself. For example, the vast majority of
participants were novices with ajax and copied-and-pasted
snippets of ajax setup code rather than try to learn to write
it from scratch. However, copy-and-paste is not simply for
novices; several participants were expert php programmers
and still employed this practice for some pieces of code, like
the one shown in Figure 2. When one participant searched
for and copied a piece of php code necessary to connect to
a MySQL database, he commented that he “had probably
written this block of code a hundred times”. Upon further
questioning, he reported that he always wrote the code by
copy-and-paste, even though he fully understood what it
did. He claimed that it was “just easier” to copy-and-paste
it than to memorize and write it from scratch.
Cognitive science research on human performance and
human error offer a deeper insight into why this may be
true [10]. Psychologists divide human performance into
three levels: skill-based (e.g., walking), rule-based (e.g.,
navigating to another office in a well-known building), and
knowledge-based performance (e.g., planning a route to a
place one has never been). We believe that copy-and-paste
programming allows developers to engage in rule-based per-
formance regardless of whether or not they are experts with
the tools they are using. Broadly speaking, the rule they
follow to accomplish the goal of “implement functionality
foo” is: 1.) search for code that does foo; 2.) evaluate qual-
ity of found code; 3.) copy code into project; 4.) modify
as necessary; 5.) test code. Because the individuals doing
opportunistic programming are programmers, it is easy for
them to come up with the high-level goals, and copy-and-
paste programming gives them a rule by which to meet those
goals, regardless of familiarity with existing tools.
This observation opens up interesting questions on how
programmers locate “promising” code. In opportunistic pro-
gramming, we believe the primary source is through web
search. Indeed, while we encouraged participants in our lab
study to bring any external resources they typically used
while programming (e.g., books), not a single participant
did so. All participants used the Internet to locate code to
copy, and all but three used this exclusively. The remain-
ing three also copied from code they had written in the past.
Strategies for locating code were quite consistent with Infor-
mation Foraging Theory [9]: First, whenever possible, par-
ticipants would look to their own code before searching on
the Internet because the former was written in their own per-
sonal style and was perceived as being easier to comprehend.
When searching the Internet, participants used a variety of
clues to gauge information “scent” (the potential for find-
ing high-quality information down a certain search path):
attractiveness and professionalism of the website, the com-
plexity and modularity of the code examples, and so forth.
If the quality of a code snippet was not satisfactory, they
would often look for confirmation by comparing against re-
lated snippets from other websites before pasting the code
into their project. Finally, once a participant was satisfied
with a snippet of code copied from a particular site, she
would often remain “loyal” to that site, copying additional
code from there without searching the whole web.