Exempla Gratis (E.G.): Code Examples for Free

Celeste Barnaby, Koushik Sen, Tianyi Zhang, Elena L. Glassman, Satish Chandra

TL;DR

This paper introduces Exempla Gratis (EG), a tool that automatically generates concise, idiomatic code examples for API methods by mining large codebases. EG addresses the challenge of finding high-quality, representative API usage examples, especially for proprietary APIs, by presenting developers with preferred, succinct snippets that highlight common usage patterns.

What Problem Does This Paper Address?

Modern software development heavily relies on Application Programming Interfaces (APIs). Developers often struggle to understand and use these APIs correctly, especially proprietary ones within large organizations where documentation might be scarce or outdated. While code search engines and existing documentation tools exist, they often provide too much extraneous information, are difficult to sift through, or present atypical usage patterns. Curating high-quality, representative API usage examples is labor-intensive and difficult to maintain. This paper tackles the problem of providing developers with readily accessible, high-quality, and idiomatic code examples for API methods, thereby reducing cognitive load and improving developer productivity.

Methods

The core of EG's approach involves two main phases:

  1. Pattern Mining: EG first parses code snippets into a simplified, language-agnostic parse tree representation. It then identifies a "maximal pattern" – a common subtree that contains the queried API method and appears in a significant proportion of code snippets. This phase iteratively grows the pattern by adding neighboring nodes that maximize its support (frequency of occurrence).
  2. Example Generation: Once a common pattern is identified, EG finds a minimal subtree from the corpus that contains this pattern. This subtree is then presented as a code example. EG also employs a strategy to generate multiple diverse examples by ensuring that subsequent examples incorporate nodes not used in previously generated ones, thus showcasing different common usage variations. The output is visually enhanced with bold text for common parts and gray text for less common "filler" code.

Datasets

EG was evaluated on:

  • Open-source GitHub projects written in Python: Approximately 1.9 million Python methods were indexed.
  • Facebook's internal codebase (Hack and Python): Used for integration and evaluation of proprietary API examples.
  • ProgramCreek: A website providing code examples, used as a baseline for comparison.

Key Insights and Ideas

  • The value of "idiomatic" examples: Developers don't just need any example, but one that reflects common, best practices within their organization or community.
  • Conciseness is key: Extraneous code and details significantly hinder learning. EG's focus on minimal, representative snippets is a major advantage.
  • Visual cues enhance understanding: Highlighting common vs. uncommon code parts (bold vs. gray) significantly aids developers in quickly grasping usage patterns.
  • Proprietary APIs are a major pain point: The lack of readily available, high-quality examples for internal APIs makes tools like EG particularly valuable in enterprise settings.
  • Code search is a proxy for API learning: The paper's motivation stems from observing that developers often use code search tools not just to find code, but to learn how to use APIs.

Key Results

  • Developer Preference: In a survey with 21 Facebook developers, EG examples were preferred over random code search results in 97% of cases.
  • Quantitative Metrics: EG examples were found to be significantly more succinct (shorter length), more relevant (less extraneous code), and more representative (higher frequency of the pattern) than code search results and ProgramCreek examples.
  • For Python, EG examples were ~2.7 lines vs. ~3.9 for code search.
  • For internal Facebook methods, EG examples were ~3.5 lines vs. ~4.6 for code search.
  • Representativeness scores for EG were substantially higher across the board.
  • Live Usage: EG was integrated into Facebook's internal code search and saw daily usage, with developers copying code from EG examples an average of 30 times per day.

Limitations and Future Work

  • Language Agnosticism: While EG is designed to be language-agnostic, it requires a language-specific parser. The current implementation focuses on Hack and Python.
  • Anti-pattern Perpetuation: EG risks identifying and perpetuating common anti-patterns if the indexed codebase contains them. Future work could involve filtering by code date or indicating the age of examples.
  • Discoverability: The integration into Facebook's code search was frictionless, but discoverability of EG as a feature could be improved.
  • A/B Testing: A formal A/B test comparing EG against code search results is suggested as future work to further quantify its impact.
  • Incremental Indexing: For very large codebases, incremental indexing could improve performance.

Summary updated

Abstract

Modern software engineering often involves using many existing APIs, both open source and, in industrial coding environments, proprietary. Programmers reference documentation and code search tools to remind themselves of proper common usage patterns of APIs. However, high-quality API usage examples are computationally expensive to curate and maintain, and API usage examples retrieved from company-wide code search can be tedious to review. We present a tool, EG, that mines codebases and shows the common, idiomatic usage examples for API methods. EG was integrated into Facebook's internal code search tool for the Hack language and evaluated on open-source GitHub projects written in Python. EG was also compared against code search results and hand-written examples from a popular programming website called ProgramCreek. Compared with these two baselines, examples generated by EG are more succinct and representative with less extraneous statements. In addition, a survey with Facebook developers shows that EG examples are preferred in 97 percent of cases.
BibTeX
@inproceedings{barnaby2020exempla,
  author = {Celeste Barnaby and Koushik Sen and Tianyi Zhang and Elena L. Glassman and Satish Chandra},
  title = {{Exempla Gratis (E.G.): Code Examples for Free}},
  booktitle = {ESEC/SIGSOFT FSE},
  year = {2020},
  doi = {10.1145/3368089.3417052},
}
Figure