Abstract
Developers spend much of their time reading and browsing source code, raising new opportunities for summarization methods. Indeed, modern code editors provide code folding, which allows one to selectively hide blocks of code. However this is impractical to use as folding decisions must be made manually or based on simple rules. We introduce the autofolding problem, which is to automatically create a code summary by folding less informative code regions. We present a novel solution by formulating the problem as a sequence of AST folding decisions, leveraging a scoped topic model for code tokens. On an annotated set of popular open source projects, we show that our summarizer outperforms simpler baselines, yielding a 28% error reduction. Furthermore, we find through a case study that our summarizer is strongly preferred by experienced developers. More broadly, we hope this work will aid program comprehension by turning code folding into a usable and valuable tool.
Introduction
Programmers spend significantly more time reading code than writing it. When navigating large, unfamiliar repositories, developers often "skim"—quickly scrolling through files to grasp core logic. While modern IDEs offer code folding, it remains a blunt instrument: users must either fold everything manually (requiring they read the code first) or rely on rigid rules like "fold all methods."
This research introduces TASSAL (Tree-based Autofolding Software Summarization ALgorithm), a tool that automatically creates code summaries by folding less informative regions based on their actual content. By addressing the "first-look problem," TASSAL helps developers quickly understand the purpose of a new source file while preserving the "code-ness" of the source—a preference over keyword lists or natural language paraphrases.
Methodology: The TASSAL Approach
The authors formulate autofolding as an optimization problem on the code's Abstract Syntax Tree (AST).
The Foldable Tree
TASSAL extracts a "foldable tree" from the AST, where nodes represent regions that can be collapsed, such as method bodies, Javadoc blocks, or import lists. This structure ensures that the resulting summary maintains the structural integrity of valid code.
Scoped Topic Model
To determine "informativeness," the researchers developed a novel topic model operating at three distinct levels:
- Background: Common language keywords (e.g.,
public,class). - Project-specific: Terms common across a library (e.g.,
beanin Spring). - File-specific: Terms unique to the current file (e.g.,
connectionin a database utility).
The model identifies file-specific tokens as the most informative. Using a greedy optimization algorithm, TASSAL iteratively unfolds nodes that provide the most information per line of code added, given a specific "budget" (e.g., 50% compression).
Evaluation and Results
The system was evaluated on 96 Java files from six popular open-source projects, including Storm, Elasticsearch, and Spring Framework. To establish a ground truth, experienced developers manually annotated these files to show what they considered the most important parts for a "first look."
Key findings include:
- Quantitative Performance: TASSAL achieved a 28% error reduction in matching human folding decisions compared to standard "best practice" baselines.
- Developer Preference: In a user study, TASSAL’s summaries were rated significantly higher for conciseness and usefulness than other automated methods.
- Efficiency: The system is highly performant, summarizing an average file in under five seconds on standard hardware.
Conclusion and Future Work
TASSAL demonstrates that treating source code as a communication medium allows for the adaptation of powerful NLP techniques like topic modeling. By ensuring structural integrity, it offers a practical tool for program comprehension. Future work may address semantic "obviousness" (e.g., recognizing that standard getters are often uninteresting) and finer granularity, such as folding at the statement level. Ultimately, turning code folding into an automated summary provides a promising path toward making complex codebases more navigable.
Summary updated