Live API Documentation

Live API Documentation

Live API Documentation Siddharth Subramanian, Laura Inozemtseva, and Reid Holmes School of Computer Science University of Waterloo Waterloo, ON, Canada s23subra,lminozem,[email protected] ABSTRACT evant API documentation. Unfortunately, these approaches Application Programming Interfaces (APIs) provide power- have several limitations. Some systems explicitly ignored ful abstraction mechanisms that enable complex functional- external references, meaning that the target system for an ity to be used by client programs. However, this abstraction analyzed document must be specified [9]. Others only re- does not come for free: understanding how to use an API can turned partially qualified names, which are insufficient for be difficult. While API documentation can help, it is often documentation linking [15]. None of the previous approaches insufficient on its own. Online sites like Stack Overflow and worked for dynamically-typed languages. Github Gists have grown to fill the gap between traditional Our paper extends previous work in this area by using a API documentation and more example-based resources. Un- constraint-based technique to uniquely identify fine-grained fortunately, these two important classes of documentation type references, method calls, and field references in source are independent. code snippets with high precision. We demonstrate the gen- In this paper we describe an iterative, deductive method erality of the approach by providing implementations for of linking source code examples to API documentation. We both typed (Java) and dynamic (JavaScript) languages. We also present an implementation of this method, called Baker, also evaluate the ability of the approach to correctly link that is highly precise (0.97) and supports both Java and code to documentation. JavaScript. Baker can be used to enhance traditional API More specifically, the contributions of this paper are as documentation with up-to-date source code examples; it can follows: also be used to incorporate links to the API documentation • A constraint-based, iterative approach for determin- into the code snippets that use the API. ing the fully qualified names of code elements in code snippets. This approach works with both statically and dynamically typed languages. 1. INTRODUCTION • A prototype tool that implements this approach and Using third-party libraries can greatly reduce the effort uses the results to automatically create bi-directional required to develop a new system. Unfortunately, under- links between documentation and source code exam- standing how to use these libraries correctly can be diffi- ples by marking up HTML using a web browser exten- cult. While the application programming interface (API) sion. documentation can be a valuable means of understanding the library, it can be insufficient on its own. One of the Section 2 presents a scenario that motivates our approach main issues is that the documentation is often out of date. and demonstrates the kinds of links it can identify. The ap- Recent work has confirmed the popular belief that writing proach and our implementation of it are explained in detail documentation and keeping it up to date is very difficult [8, in Sections 3 and 4. Section 5 then presents our evaluation of 9]; consequently, developers ignore the documentation that Baker. The documentation linking prototype is described in does exist and declare that \code is king" [16]. Section 6, followed by discussion. Related work is described As a result of this situation, developers often turn to online in Section 8; Section 9 concludes the paper. resources such as Stack Overflow. Parnin et al. have pre- viously studied online resources and have found that APIs 2. SCENARIO are well covered by them [14]. In fact, they found that 87% of Android classes were referenced in Stack Overflow an- Consider the Java code snippet shown in Figure 1. This swers. Unfortunately, there are rarely links between online snippet (pertaining to a library called GWT) was posted to resources and official API documentation: the official doc- Stack Overflow to assist a developer who did not understand umentation does not link to the examples that could help how to manipulate the state of History objects. The figure developers, and the examples rarely link to the documenta- contains a number of bolded elements. These are the types tion. and methods that our tool, Baker, can uniquely link to the Previous work has tried to identify source code references API; i.e., the elements for which it can determine a fully- within non-code resources (e.g., [2, 13, 9, 15]). Detecting qualified name. With this information we can automatically these references is a first step toward linking them to the rel- augment the HTML version of the official API documenta- tion for History by dynamically injecting the code example University of Waterloo Technical Report CS-2013-17 into the web page. We can also inject the links to the official API into the Stack Overflow post; these two additions to the parsing full files because code snippets can be ambiguious. documentation would make it easier for developers to learn Dagenais and Robillard highlighted four kinds of ambiguity how to use this class. that can hamper the identification of elements [9]; two of these were specific to the plain-text analysis they were per- 1 public FirstPanel() { forming, while the other two were more generally relevant. 2 History.addHistoryListener(this); These two were declaration ambiguity and external reference 3 String token = History.getToken(); ambiguity. 4 if (token.length() == 0) { 5 History.newItem(INIT_STATE); Declaration Ambiguity. Snippets are, by definition, in- 6 } else { complete fragments of code. That is, snippets might not be 7 History.fireCurrentHistoryState(); embedded in methods or classes, they may reference fields 8 } whose declaration is not included, and their identifiers are 9 .. rest of code largely unqualified. In source code examples this is often 10 } exacerbated by authors ending lines with `. ' or using code comments to describe parts of the functionality that Figure 1: Java code snippet representing a Java API are elided. usage. Baker can associate each of the bolded terms External Reference Ambiguity. Source code examples with a fully qualified name; this information can be frequently refer to external identifiers; for example, Java used to include the code example in the API docu- snippets frequently reference types from the JDK. While mentation. a previous study [9] dealt with external references by elid- ing everything that was not from a pre-specified library, we Next, consider the JavaScript snippet in Figure 2, where designed Baker to handle these kinds of ambiguities. We a developer is trying to develop a web app that can take do this by using an oracle: a large database containing in- a photo and inject it into an element in an HTML docu- formation about the code elements in popular APIs. When ment. This example interacts with the JavaScript DOM Baker encounters an ambiguous code element, such as the (getElementById), takes a photo using the Cordova project History class in Figure 1, it uses the oracle to identify the (getPicture), and uses JQuery to detect when the the possible types of the code element. In this case, there are photo should be taken ($ and on). For each of these method 58 History classes in the oracle, but by using information references Baker can can identify the API that it is from. from other parts of the code snippet, we can identify which of the 58 is the correct one. Section 4 will present more 1 $("#addphoto").on(’click’, information about how the oracle is constructed, what it 2 function() { useGetPicture();} contains, and how much of a problem ambiguity really is. 3 ); 4 function useGetPicture() { 3.1 Deductive linking 5 var cameraOptions = { ... }; 6 navigator.camera.getPicture( onCameraSuccess, Baker handles declaration ambiguity and external refer- 7 onCameraError, cameraOptions); ence ambiguity through a process we call deductive linking. 8 } At a high level, it generates an incomplete abstract syntax 9 function onCameraSuccess(imageData) { tree (AST) for the code snippet being analyzed, then uses 10 var image = document.getElementById(".."); information from the oracle to deduce facts about the AST. 11 image.src = "data:image/jpeg" + imageData; We perform this deduction step iteratively since each phase 12 } 13 function onCameraError(message) { can reveal new facts that can be used in subsequent phases. 14 alert("Failed: " + message); More specifically, in each phase, Baker performs a depth- 15 } first traversal of the AST and examines all nodes involved in declarations, invocations, and assignments. When an AST node of interest is encountered, Baker builds a list that rep- Figure 2: JavaScript code snippet containing Cor- resents the potential matches for that element from the or- dova, JQuery and JavaScript DOM API usage. Each acle. As the traversal takes place, we track the scope of all of the bolded elements can be linked back to the rel- data being used to ensure that nodes are only combined if evant API documentation. allowed by the scoping rules of the language. Once the entire AST has been traversed, the process starts The code snippets in Figures 1 and 2 were both submit- again; information uncovered in the previous iteration can ted as the correct solution to problems developers posted now be used to further restrict the lists of candidate ele- on Stack Overflow. Since Stack Overflow posts are ranked, ments. In theory, this iteration continues until either all and accepted answers are known to have solved a real prob- elements are associated with a single fully qualified name lem, it is a good source of high quality code snippets that (FQN), or an iterations fails to improve the results for any demonstrate the correct usage of many APIs. Increasing element. In practice we find that few iterations are typically the integration between these examples and the official API needed; in fact we have a hard-coded limit that prevents documentation will make documentation maintenance eas- our approach from iterating more than four times through ier and increase the visibility and accessibility of the official a given AST.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    9 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