Analyzing and supporting adaptation of online code examples

Tianyi Zhang, Di Yang, Cristina V. Lopes, Miryung Kim

Introduction

Stack Overflow (SO) has become an indispensable resource for software developers, providing a vast repository of code snippets for almost any programming task. However, a significant gap exists between the "illustration-only" snippets found online and the robust, production-ready code required in real-world applications. These snippets often lack error handling, use outdated APIs, or contain security vulnerabilities. Consequently, developers must manually adapt these examples—a process that is frequently repetitive and error-prone. This paper investigates the patterns of these adaptations and introduces EXAMPLESTACK, a tool designed to bridge this gap by leveraging the collective intelligence of the developer community.

Methodology and Datasets

To understand how code is adapted in practice, the researchers conducted a large-scale study linking SO snippets to real-world usage on GitHub. They utilized two primary datasets derived from 312,000 SO posts and 5.8 million Java files:

  1. Adaptations Dataset: 629 SO examples with explicit GitHub attributions.
  2. Variations Dataset: 14,124 SO examples with GitHub clones, representing a broader set of likely reuse.
Distribution of GitHub clones for Stack Overflow examples, illustrating the scale of code reuse across the ecosystem.

The team developed a taxonomy of 24 adaptation types across six categories, such as Code Hardening and Logic Customization. They then built an automated classifier using GumTree, an AST-based differencing tool, which achieved 98% precision in identifying these changes.

The Nature of Code Adaptation

The study revealed that adaptation is rarely a minor task. Most snippets require substantial modification to fit into a production environment.

Distribution of AST edits required to adapt Stack Overflow examples into GitHub projects, showing that most snippets undergo significant structural changes.

Key findings include:

  • The "Hardening" Gap: While SO examples focus on the "happy path," GitHub implementations frequently add safety checks (e.g., null checks), resource management (e.g., closing streams), and proper exception handling.
  • Logic Customization: The most frequent adaptations involve changing method calls and renaming variables, highlighting the need to tailor generic examples to specific project contexts.
  • Recurring Patterns: The same types of adaptations often recur across different developers for the same snippet, suggesting that these modifications are not random but follow predictable "best practices."

EXAMPLESTACK: Supporting Developers

Based on these insights, the authors developed EXAMPLESTACK, a Chrome extension that enhances Stack Overflow posts. The tool "lifts" a template from an SO snippet, preserving the core logic while identifying "hot spots" where other developers have made modifications. Users can interact with these spots to see a menu of real-world variations, such as different variable types or added safety checks.

In a user study with 16 programmers, EXAMPLESTACK users performed 3x more code hardening and 2x more logic customization than a control group. Participants reported that the tool helped them "reach consensus" on implementation details and "grasp the essence" of examples more quickly, effectively acting as a form of asynchronous pair programming.

Conclusion

The transition from an online example to production code is a non-trivial engineering task. By quantifying the types of adaptations developers make and surfacing them through tools like EXAMPLESTACK, we can transform Stack Overflow from a static repository into a dynamic, community-driven guide for writing robust code. Future work will look to expand these techniques beyond Java and integrate these insights directly into modern IDEs.

Summary updated

BibTeX
@inproceedings{zhang2019analyzing,
  author = {Tianyi Zhang and Di Yang and Cristina V. Lopes and Miryung Kim},
  title = {{Analyzing and supporting adaptation of online code examples}},
  booktitle = {ICSE},
  year = {2019},
  url = {https://openreview.net/forum?id=wKH5SpXbcZv},
}
Figure