Commenting Higher-level Code Unit: Full Code, Reduced Code, or Hierarchical Code Summarization

Weisong Sun, Yiran Zhang, Jie Zhu, Zhihui Wang, Chunrong Fang, Yonglong Zhang, Yebo Feng, Jiangping Huang, Xingya Wang, Zhi Jin, Yang LiuarXiv 2025 · DOI

Introduction

Automated Code Summarization (ACS) has long been a cornerstone of software engineering research, aiming to assist developers in understanding complex codebases. However, the vast majority of existing literature focuses on the method or function level. While method-level summaries clarify micro-logic, they often fail to provide the macro-level context necessary for architectural understanding. Developers frequently need to know what an entire file does or how various files interact within a module.

Hierarchy of code units and the focus of this research compared to traditional studies.

This research addresses the gap by exploring summarization strategies for higher-level code units: files and modules. The primary challenge lies in the sheer volume of code; simply inputting a large module into a Large Language Model (LLM) often exceeds context window limits or leads to performance degradation and high API costs.

Summarization Strategies

The study evaluates three distinct prompting strategies across three LLMs (GPT-4, CodeLlama-7B, and CodeGemma-7B):

  1. Full Code Summarization (FFCS / FMCS): The baseline approach where the entire raw code is passed to the LLM. This provides maximum context but consumes the most tokens.
  2. Reduced Code Summarization (RFCS / RMCS): A strategy designed to save tokens by stripping implementation details. Method bodies are removed, leaving a "skeleton" consisting of package information, imports, class signatures, and method signatures.
  3. Hierarchical Code Summarization (HFCS / HMCS): A bottom-up abstraction method. For modules, the LLM first summarizes individual files and then generates a module-level summary based on those file summaries. For files, it summarizes method clusters before generating the final file summary.
Illustration of the Reduced Code strategy, showing how full file code is condensed into a structural skeleton to save tokens.

Experimental Setup

The researchers utilized a dataset derived from 10 prominent open-source Java projects, including Elasticsearch, Hadoop, and Skywalking. The evaluation involved 200 files and 50 modules. To assess the quality of the generated summaries, the study employed both human expert grading (on a 1-5 scale) and an "LLM-as-a-judge" approach using GPT-4.

Key Findings

The results indicate that the optimal strategy depends heavily on the level of the code unit being summarized:

  • File-Level Performance: Full Code Summarization (FFCS) is the clear winner for individual files, achieving the highest human evaluation scores (4.46/5). However, Reduced Code (RFCS) proved to be a highly efficient alternative, reducing token usage by approximately 74.65% while suffering only a ~23% drop in summary quality.
  • Module-Level Performance: At the module level, the Hierarchical strategy (HMCS) outperformed the others, scoring roughly 9-10% higher. When presented with the full code of a module, LLMs often struggled with the excessive context, sometimes leading to hallucinations or irrelevant outputs. By forcing the model to step up the ladder of abstraction, the hierarchical approach better captures architectural intent.
  • Automated Evaluation: The study confirmed that GPT-4 can serve as a viable substitute for human evaluators, showing a moderate-to-strong correlation (0.42 to 0.74) with human scores. Interestingly, GPT-4 was found to be a stricter grader, often docking points for stylistic preferences despite factual accuracy.

Conclusion

As codebases grow in complexity, the need for high-level architectural summaries becomes paramount. This research demonstrates that while full context is beneficial for file-level summaries, a hierarchical approach is essential for capturing the essence of larger modules. Furthermore, the "Reduced Code" strategy offers a promising path for cost-effective summarization without significant loss of structural context. Future work will look toward refining these abstraction heuristics and applying them to non-object-oriented languages where structural boundaries are less rigid.

Summary updated

BibTeX
@misc{sun2025commenting,
  author = {Weisong Sun and Yiran Zhang and Jie Zhu and Zhihui Wang and Chunrong Fang and Yonglong Zhang and Yebo Feng and Jiangping Huang and Xingya Wang and Zhi Jin and Yang Liu},
  title = {{Commenting Higher-level Code Unit: Full Code, Reduced Code, or Hierarchical Code Summarization}},
  year = {2025},
  eprint = {2503.10737},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2503.10737},
}
Figure