Abstract
Official documentation for third-party libraries often lacks practical, diverse usage examples, forcing developers to manually search for snippets in large repositories. This paper presents CLAMS (Clustering for API Mining of Snippets), an automated tool designed to generate concise and readable API examples. By integrating sequence-level clustering with a novel summarization algorithm, CLAMS produces snippets that are significantly smaller and more readable than raw client code, effectively bridging the gap between automated mining and human-written documentation.
Introduction
Modern software development relies heavily on third-party libraries. However, the utility of these libraries is often limited by inadequate documentation. When official guides fail to provide clear examples, developers turn to "API mining" to extract patterns from existing codebases. Existing tools typically fall into two categories: sequence miners, which lack code context, and snippet miners, which often produce noisy, redundant, or language-specific results.
CLAMS addresses these challenges by providing a data-driven, syntactically focused approach to snippet generation. It aims to provide the "connective tissue" of real code—such as variable types and control flow—while maintaining the conciseness required for quick comprehension.
The CLAMS Architecture
The CLAMS pipeline is designed to be language-agnostic and robust, focusing on structural patterns rather than heavy semantic analysis.
The process consists of four primary modules:
- Preprocessing and Clustering: Instead of analyzing raw source code, CLAMS extracts API call sequences. These sequences are grouped using the Longest Common Subsequence (LCS) distance metric. By employing k-medoids and HDBSCAN, the tool ensures that the resulting examples cover a diverse range of API use cases.
- Summarization: To transform raw client code into clean examples, CLAMS uses static, flow-insensitive slicing. This removes project-specific logic, replaces literals with generic types, and adds descriptive comments to improve clarity.
- Snippet Selection: Within each cluster, the tool identifies the most "representative" snippet using the AP-TED (Tree Edit Distance) algorithm on Abstract Syntax Trees (ASTs).
- Ranking: Finally, snippets are ranked based on their "support" within the dataset, ensuring that the most reliable and common patterns are presented first.
Experimental Evaluation
The effectiveness of CLAMS was evaluated using six prominent Java libraries mined from GitHub, representing a diverse set of application domains.
The dataset comprised over 1.5 million lines of client code. To establish a baseline for quality, the authors compared the generated snippets against a "gold standard" of approximately 148,000 lines of handwritten examples provided by the library developers.
Key Findings
The evaluation revealed several critical insights into the performance of CLAMS:
- Readability and Conciseness: The summarization algorithm successfully doubled the readability scores of mined snippets compared to raw code. On average, snippets were reduced in size by 40%, with most final examples fitting within 10 lines of code.
- The Information Gap: The study quantified the value of snippets over simple call sequences. Snippets were found to provide 2.75 times more useful information (such as types and error handling) that matched human-written documentation compared to raw sequences.
- Diversity and Coverage: By clustering sequences before selection, CLAMS avoids "mode collapse"—a common issue where tools repeatedly show the same common pattern. CLAMS outperformed existing state-of-the-art tools like MAPO and UP-Miner in terms of API coverage.
- Documentation Augmentation: In several instances, CLAMS identified useful usage patterns that were entirely absent from the official documentation, demonstrating its potential as a tool for automatically augmenting developer guides.
Conclusion and Future Work
CLAMS demonstrates that a data-driven, clustering-based approach can produce high-quality API examples that rival human-written documentation. While currently focused on intra-procedural analysis within Java, future work aims to extend the tool's scope to multi-method contexts and adapt the AST schema for other imperative languages like C# or Python. Additionally, the authors propose a two-stage clustering process to provide even more granular structural variations of API usage.
Summary updated