Detecting and Ranking API Usage Pattern in Large Source Code Repository: A LFM Based Approach

Jitong Zhao, Yan LiuCD-MAKE 2017

TL;DR

This paper introduces LFM-OUPD, a novel graph-based approach to automatically detect and rank common API usage patterns from large code repositories. By modeling API method calls as a graph and applying a modified community detection algorithm, LFM-OUPD helps developers find relevant and effective code examples, reducing the effort required to understand and utilize APIs.

What Problem Does This Paper Address?

Programmers frequently rely on third-party APIs to build software, but understanding how to use these APIs effectively can be challenging. Official documentation often lacks sufficient or clear code examples, forcing developers to manually sift through vast amounts of online resources like GitHub, technical blogs, and Q&A forums. This process is time-consuming and inefficient, especially given the exponential growth of APIs and their complexity. The paper aims to alleviate this burden by providing a systematic way to discover and recommend common API usage patterns, thereby helping developers learn and apply APIs more efficiently.

Methods

The core of LFM-OUPD is a graph-based approach that models API usage as a network.

  • Method Call Sequence Graph Construction: The process begins by extracting API method call sequences from client code. These sequences represent how methods are invoked together to achieve a specific functionality. These sequences are then used to construct a directed weighted graph where nodes are API methods and edges represent sequential calls, weighted by their co-occurrence frequency.
  • API Usage Pattern Detection (LFM-OUPD): The paper adapts the Local Fitness Measure (LFM) algorithm, originally designed for community detection, to identify "usage patterns" within this graph. LFM-OUPD focuses on detecting overlapping communities, which correspond to common API usage patterns. It uses a directed degree centrality to identify "core seed nodes" and iteratively merges neighboring nodes to maximize a fitness function that quantifies the density and cohesiveness of a potential usage pattern.
  • Candidate API Usage Recommender: Once usage patterns are detected, they are ranked using three metrics:
  • Cohesiveness: Measures how frequently API methods within a pattern are used together.
  • Availability: Assesses how universally a pattern demonstrates a specific API usage scenario across different code snippets.
  • Representation: Evaluates the proportion of third-party API calls within a pattern, favoring patterns that rely less on custom code. A final score is computed by combining these metrics, and the top-ranked patterns are presented to the user.

Datasets

The study utilizes a dataset provided by Sawant and Bacchelli [16], which contains method invocation and annotation reference information for five APIs from 3013 projects. For their case study, the authors specifically focused on the Google Guava API, leveraging its public methods as utilized across these projects.

Key Insights and Ideas

  • API Usage as a Network: The fundamental insight is that API method calls can be effectively represented as a directed, weighted graph, allowing graph mining techniques to uncover usage patterns.
  • Leveraging Overlapping Communities: Adapting community detection algorithms, particularly those that handle overlapping communities like LFM, is a powerful way to identify recurring API usage patterns that might not be strictly sequential or exclusive.
  • Multi-faceted Ranking: Relying on a single metric for ranking API usage patterns is insufficient. Combining cohesiveness, availability, and representation provides a more robust evaluation, leading to more relevant and useful recommendations.
  • Bridging the Documentation Gap: The approach directly addresses the common problem of insufficient API documentation by automatically extracting and organizing usage knowledge from actual code.

Key Results

The paper presents a case study comparing LFM-OUPD with LFM and MAPO using the Google Guava CacheBuilder class.

  • LFM-OUPD successfully detected 8 valid usage pattern candidates for CacheBuilder, whereas LFM only identified 2.
  • The ranked API usage patterns from LFM-OUPD demonstrated higher cohesiveness, availability, and representation compared to those from LFM.
  • The approach was able to recommend code snippets that effectively demonstrated specific API usage mechanisms, such as setting eviction policies or initialization strategies.
  • The study suggests that LFM-OUPD significantly enhances the ability to detect and recommend API usage patterns, offering more representative and comprehensible examples to developers.

Limitations and Future Work

  • Language Specificity: The current implementation and evaluation are focused on Java. Extending the approach to other programming languages would be a valuable future direction.
  • Method Overloading: The paper mentions that method overloading is not considered in the current approach to simplify the process. Future work could incorporate handling method overloading for more comprehensive analysis.
  • Scalability: While the approach is designed for large repositories, further investigation into the scalability of the graph construction and community detection algorithms for extremely massive codebases might be beneficial.
  • User Interaction: The paper focuses on detecting and ranking patterns. Future work could explore more sophisticated user interaction mechanisms to refine recommendations based on individual developer needs or preferences.

Summary updated

BibTeX
@inproceedings{zhao2017detecting,
  author = {Jitong Zhao and Yan Liu},
  title = {{Detecting and Ranking API Usage Pattern in Large Source Code Repository: A LFM Based Approach}},
  booktitle = {CD-MAKE},
  year = {2017},
  doi = {10.1007/978-3-319-66808-6\_4},
}
Figure