NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Mining Idioms from Source Code

Miltiadis Allamanis, Charles Sutton · DOI

Abstract: We present the first method for automatically mining code idioms from a corpus of previously written, idiomatic software projects. We take the view that a code idiom is a syntactic fragment that recurs across projects and has a single semantic role. Idioms may have metavariables, such as the body of a for loop. Modern IDEs commonly provide facilities for manually defining idioms and inserting them on demand, but this does not help programmers to write idiomatic code in languages or using libraries with which they are unfamiliar. We present HAGGIS, a system for mining code idioms that builds on recent advanced techniques from statistical natural language processing, namely, nonparametric Bayesian probabilistic tree substitution grammars. We apply HAGGIS to several of the most popular open source projects from GitHub. We present a wide range of evidence that the resulting idioms are semantically meaningful, demonstrating that they do indeed recur across software projects and that they occur more frequently in illustrative code examples collected from a Q&A site. Manual examination of the most common idioms indicate that they describe important program concepts, including object creation, exception handling, and resource management.

1. Introduction

Experienced developers write "idiomatic" code—patterns that feel natural to others in that language or ecosystem. However, learning these idioms for a new language or a complex library (like Android or Hibernate) is a major hurdle for beginners. While modern IDEs offer "snippets," these must be created manually. Previous automated attempts usually fell into two traps: frequent tree mining, which returns trivial fragments like bare if statements, and clone detection, which finds verbatim copies rather than abstract patterns.

This paper introduces HAGGIS, a system designed to automatically mine code idioms—recurrent syntactic patterns that serve a specific semantic purpose—from large-scale source code repositories. By moving beyond simple frequency counts to a probabilistic "explanation" model, it identifies semantically meaningful patterns like resource management or API-specific boilerplate.

Figure 2: An example of a code idiom (a), its abstract template (b), and the corresponding AST fragment (c) used for mining.

2. Methodology

HAGGIS treats idiom mining as a statistical natural language processing problem. The core of the system is a Probabilistic Tree Substitution Grammar (pTSG). Unlike standard grammars that expand one symbol at a time, a pTSG allows for "rules" that are entire tree fragments (AST subtrees). This allows the model to represent a complex pattern, such as a try-finally block for closing a database cursor, as a single unit.

Figure 3: Illustration of the Tree Substitution Grammar approach, where complex subtrees (shaded regions) are treated as elementary building blocks.

To find the optimal balance between generic and overfit fragments, HAGGIS uses nonparametric Bayesian inference (specifically a Dirichlet Process). This provides a "rich-get-richer" effect: if a fragment is seen often, the model becomes more likely to use it to explain other parts of the code. The system parses Java code into Abstract Syntax Trees (ASTs), binarizes them to reduce sparsity, and replaces specific variable names with metavariables (e.g., $name, $BODY$) to capture structural essence regardless of specific identifiers.

3. Key Results and Evaluation

The authors evaluated HAGGIS using millions of lines of code from the top 13 Java projects on GitHub and a collection of files importing 15 popular libraries. Key findings include:

  • Cross-Project Validation: 67% of the idioms mined from GitHub projects were also found in an independent StackOverflow dataset of 108,000 snippets, proving these patterns are universal and not project-specific quirks.
  • Superiority over Clone Detectors: HAGGIS significantly outperformed Deckard (a state-of-the-art clone detector) in finding recurrent patterns. The overlap between the two was less than 0.5%, confirming that idioms and clones are distinct phenomena.
  • Semantic Consistency: Mined idioms are highly correlated with package imports, suggesting that IDEs could proactively suggest relevant idioms based on a developer's import statements.

4. Conclusion

HAGGIS demonstrates that code idioms can be effectively discovered using advanced statistical models. By shifting the focus from simple frequency to probabilistic explanation, the system identifies patterns that are both semantically meaningful and practically useful for developers. Future work will focus on improving computational complexity and incorporating higher-level semantic context like data-flow and variable scoping.

Summary updated

BibTeX
@inproceedings{allamanis2014mining,
  author = {Miltiadis Allamanis and Charles Sutton},
  title = {{Mining Idioms from Source Code}},
  booktitle = {SIGSOFT FSE},
  year = {2014},
  doi = {10.1145/2635868.2635901},
}
Figure