Automated Synthesis of Compilable Code Snippets from Q&A Sites

Automated Synthesis of Compilable Code Snippets from Q&A Sites

ifact t * r * Comple t te A A n * te W E is s * e C n l l o D C A o * * c T u e m S s E u e S e n I R CSNIPPEX: Automated Synthesis of t v e o d t y * s E a * a l d u e a Compilable Code Snippets from Q&A Sites t Valerio Terragni, Yepang Liu and Shing-Chi Cheung Department of Computer Science and Engineering The Hong Kong University of Science and Technology Hong Kong, China {vterragni, andrewust, scc}@cse.ust.hk ABSTRACT This crowd knowledge is useful to recent software develop- Popular Q&A sites like StackOverflow have collected nu- ment practice [25] because many posts contain high quality merous code snippets. However, many of them do not have code snippets representing solutions of programming tasks, complete type information, making them uncompilable and bug fixes [15, 20, 27] or API usage examples [32, 33]. These inapplicable to various software engineering tasks. This paper posts are continually rated, discussed and updated by the crowd, providing valuable resources for code reuse and anal- analyzes this problem, and proposes a technique CSnippEx to automatically convert code snippets into compilable Java ysis [42]. For example, to boost productivity, developers source code files by resolving external dependencies, gener- often reuse or draw inspiration from these code snippets [25]. ating import declarations, and fixing syntactic errors. We Recent surveys reported that developers search for code snip- pets in Q&A sites frequently and extensively [34, 40, 41, 42]. implemented CSnippEx as a plug-in for Eclipse and evalu- ated it with 242,175 StackOverflow posts that contain code As a result, the popular Q&A site StackOverflow receives ∼500M views per month [5]. Besides code reuse, this large snippets. CSnippEx successfully synthesized compilable Java files for 40,410 of them. It was also able to effectively volume of crowd-generated code snippets can be leveraged to recover import declarations for each post with a precision of accomplish various software engineering goals. For example, 91.04% in a couple of seconds. API library developers may leverage those snippets that use their API to test their revised API implementation or to collect possible usage profiles. Such profiles can be useful for CCS Concepts mining temporal specifications [26, 8]. Moreover, developers •Human-centered computing ! Social networking may analyze these snippets for debugging [15, 27] or bug sites; •Software and its engineering ! Source code fixing [20]. generation; Software libraries and repositories; Compilers; However, there is a major obstacle in reusing or analyzing Runtime environments; Q&A code snippets. The majority of Q&A posts (91.59% of the 491,906 posts we collected from StackOverflow) contain Keywords uncompilable code snippets [15, 45], indicating that they are non-executable and semantically incomplete for precise static Crowd-generated snippets, developer social networks, pro- analysis [26]. This phenomenon occurs because code snippets gram synthesis on Q&A sites are written for illustrative purposes, where compilability is not a concern. In fact, Q&A sites rarely check 1. INTRODUCTION the syntax of submitted snippets. The problem is further Increasing number of software developers collaborate and exacerbated by the fact that submitted snippets are often share experience over social networks. In particular, Ques- concisely written without implementation details in order to tion & Answer websites (Q&A) [44], such as StackOverflow, convey solution ideas at high level [29]. Although the missing CodeRanch and CodeProject, are those frequently visited by implementation details could be synthesized manually, it developers. On these Q&A websites, developers collaborate is tedious and often requires substantial familiarity with by raising questions, sharing their solutions and rating re- various libraries. Such manual synthesis effort is, however, lated contents. Over time, these sites have collected large not scalable for those crowd-based software engineering goals volumes of crowd knowledge. For example, as of January that typically involve many code snippets [26, 15, 27, 20]. 2016, StackOverflow had indexed 11 million questions and Analyzing the uncompilable snippets collected from 450,557 18 million answers [6]. StackOverflow posts, we observed that the most common compilation error (38%) is compiler.error.cant.resolve, which is a consequence of missing declarations of program en- tities (e.g., classes, variables and methods). This observation is in line with Chen et al.'s finding [15]. Two technical challenges have to be addressed to automati- cally resolve errors of this kind. First, it is difficult to resolve This is the author’s version of the work. It is posted here for your personal use. Not for redistribution. The definitive version was published in the following publication: external dependencies, e.g., inferring an appropriate type for ISSTA’16, July 18–20, 2016, Saarbrücken, Germany referred classes [46, 38, 18]. This is because the majority of c 2016 ACM. 978-1-4503-4390-9/16/07... code snippets refer to library classes using simple names [18]. http://dx.doi.org/10.1145/2931037.2931058 118 Input Output In summary, this paper makes four major contributions: + import org.library1.A; + import org.library1.B; text text text text text + import org.library1.sub.C; • A technique CSnippEx to automatically convert Q&A static C m1(B b) { + public class Class { site code snippets into compilable Java source code files A a = new A(); static C m1(B b) { by fixing missing declarations of classes, methods and C c = a.call(b); A a = new A(); variables. return c; C c = a.call(b); } return c; • We implemented CSnippEx as an Eclipse plug-in and text text text text text } text text text text text + public static void main(String[] args) { evaluated it by extensive controlled experiments. The System.out.println(m1(b)); + B b = null; results confirmed the effectiveness, efficiency of CSnippEx. System.out.println(m1(b)); + } } • We conducted statistical analysis on ∼18M real-world Java Figure 1: Example of a Q&A code snippet synthesis source code files [19] to demonstrate that the clustering hypothesis exploited by our technique is valid in practice. Without fully qualified names (e.g., org.library1.sub.C), • We release CSnippEx and our synthesized code snip- naming ambiguities can easily arise, as many simple class pets [3]. Our tool and dataset can facilitate future research names across different libraries collide with one another [46, on analyzing crowd-generated big data by various static 38]. Recently, Subramanian et al. have attempted to address and dynamic code analysis techniques. this issue by leveraging fields, method signatures and scope to recover the links between source code in Q&A sites and API documentations [46]. However, the technique rarely 2. BACKGROUND AND MOTIVATION provides sufficient information to help users choose from a set of candidates, since method name conflicts are also 2.1 Problem Formulation common. For example, Dagenais et al. showed that 89% of The input of CSnippEx is a Q&A post (a unit of text) method names in widely-used libraries are ambiguous and on that contains one or more code snippets, i.e., a collection average each method has name conflicts with 13 others [18]. of Java source code lines. A Q&A post could contain de- Second, it is also hard to automatically partition multiple velopers' communications in the form of natural language. code snippets in a post into an appropriate number of source On popular Q&A sites, the code snippets are mostly en- files. Simple heuristics such as always or never merging the closed between dedicated HTML tags (e.g., <pre><code> on code snippets in a post into a single source file can hardly StackOverflow), and thus can be unambiguously identified. cope with the complex real-world situations. The output of CSnippEx is one or more compilation This paper presents a code snippet synthesis framework units. A compilation unit (c-unit for short) is a legitimate CSnippEx (Code Snippet Extractor) and its implementa- Java source code file, which can be accepted by standard tion for the Java language. CSnippEx can automatically compilers. A c-unit declares Java reference types whose im- synthesize a working context by converting the code snippets plementation is given by the source code lines in the code extracted from a post into Java source files, properly includ- snippets (see Figure 1). Every c-unit and all its declared ing library JARs in the build path, and generating import types belong to a namespace or package. There are two ways declarations for referred classes. Once the working context in which the code of a c-unit can refer to a Java type: by its is set up, CSnippEx leverages Eclipse Quick Fix to correct simple name (e.g., List), or by its fully qualified name (e.g., bad syntax, typos, and undeclared variables. CSnippEx java.util.List), which is the concatenation of its package addresses the above-mentioned challenges as follows. name, a \." token, and its simple name. Types in the same First, CSnippEx adopts a feedback-directed approach that package can refer to each other by simple names. To refer to leverages compiler error messages to automatically infer how external types defined in other packages (e.g., a third-party to partition code snippets into multiple Java source files and library), import declarations are needed to allow Java compil- whether the dependency resolution is correct. Second, to ers to unambiguously resolve the referred types. It is worth efficiently find the correct import combinations in a myriad of mentioning that Java allows two kinds of (non-static) im- candidate solutions, CSnippEx leverages a key observation port declarations: the single-type-import declaration, which confirmed by analyzing ∼18 million real-world Java source imports a single type into a c-unit by specifying its fully files.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    12 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us