Learning Natural Coding Conventions

Miltiadis Allamanis, Earl T. Barr, Charles Sutton · DOI

Abstract Software development is governed by both explicit "laws" (linters and style guides) and implicit "mores" (emergent, project-specific conventions). While laws are easily automated, mores—such as nuanced naming choices and local formatting preferences—remain a significant burden in manual code reviews. This paper presents NATURALIZE, a machine learning framework that treats source code as a "natural" language to automatically learn and enforce these subtle conventions. By utilizing n-gram language models and a novel "Sympathetic Uniqueness Principle," NATURALIZE achieves 94% accuracy in identifier suggestions. Its real-world utility is demonstrated through the acceptance of 14 automated patches into major open-source repositories including JUnit and libgdx.

Introduction

Every software project develops a unique coding style over time. These conventions typically fall into two categories: laws, which are explicitly stated rules enforced by static analysis tools, and mores, which are the unspoken, emergent practices that developers internalize through experience.

While "laws" are straightforward to automate, "mores" are notoriously difficult to codify manually. Research indicates that roughly one-third of all code reviews contain feedback regarding style and naming, representing a significant expenditure of developer time. Furthermore, as a codebase evolves, these unspoken conventions shift, rendering static, rule-based formatters obsolete. The authors address this "coding convention inference problem" by proposing a system that can automatically learn these subtle, local patterns directly from the source code itself.

The NATURALIZE Framework

NATURALIZE is built on the "naturalness" hypothesis, which posits that code is a repetitive and predictable medium, much like human speech. The framework employs several key components to capture and suggest stylistic improvements:

  • N-gram Language Models: The core engine uses n-grams to calculate the "naturalness" of a code snippet. If a variable name or formatting choice has a low probability compared to the rest of the project, the system flags it as a potential inconsistency.
  • Proposers and Scorers: The framework uses "proposers" to generate alternative candidates (e.g., different variable names or whitespace configurations) and a "scoring function" to rank them based on the learned model.
  • Sympathetic Uniqueness Principle (SUP): To avoid suggesting overly generic names, SUP ensures that rare, descriptive names are preserved when they appear in unique contexts, only suggesting changes when a name is statistically out of place.
  • Formatting as Tokens: To learn visual style, NATURALIZE converts whitespace (tabs, spaces, newlines) into special tokens, allowing the same n-gram models used for text to process the visual layout of the code.
The NATURALIZE pipeline: from raw code to ranked suggestions based on n-gram models and scoring functions.

Experimental Setup and Datasets

The researchers leveraged a massive corpus of 10,968 open-source Java projects from GitHub to train and validate the system. For detailed evaluation, they focused on 10 popular, high-quality projects, including JUnit, libgdx, and elasticsearch. Using "leave-one-out" cross-validation, the model was trained on the majority of a project's files to predict the conventions in the remaining files, testing its ability to adapt to specific project "vibes" without manual configuration.

Key Results and Real-World Impact

The framework demonstrated remarkable precision across multiple domains:

  • High Accuracy: NATURALIZE achieved 94% accuracy in its top suggestions for identifier names and 96% accuracy for formatting suggestions.
  • Confidence Gaps: To minimize user annoyance, the tool measures the "confidence gap" between the current code and the best suggestion. It only prompts the user when the suggested change is significantly more "natural" than the existing code.
  • Cross-Project Learning: The authors found that by training on a large "global" corpus of high-quality projects, NATURALIZE could suggest effective names even in brand-new or small projects where local data is sparse.

Beyond laboratory metrics, the authors tested the tool's efficacy by submitting 18 style-fix patches to 5 major open-source projects. Maintainers accepted 14 of these patches, confirming that the tool's suggestions are indistinguishable from human-written code and provide genuine value to professional developers.

A summary of the projects used for evaluation and the status of automated pull requests submitted by the NATURALIZE framework.

Limitations and Future Work

While highly effective at the syntactic level, NATURALIZE currently focuses on local conventions. It does not yet address higher-level architectural "mores," such as object relationships or complex design patterns. Additionally, while the framework is language-agnostic, the current implementation is specialized for Java and C#. Future work aims to incorporate deeper semantic features and expand the system to support multi-language environments, such as web applications that mix Java, CSS, and JavaScript.

Conclusion

NATURALIZE demonstrates that statistical NLP techniques are remarkably effective at capturing the stylistic essence of a codebase. By treating code as a natural medium, the framework bridges the gap between rigid, manual linting and the fluid, emergent conventions that define high-quality software development.

Summary updated

BibTeX
@inproceedings{allamanis2014learning,
  author = {Miltiadis Allamanis and Earl T. Barr and Charles Sutton},
  title = {{Learning Natural Coding Conventions}},
  booktitle = {SIGSOFT FSE},
  year = {2014},
  doi = {10.1145/2635868.2635883},
}
Figure