Are Human Rules Necessary? Generating Reusable APIs with CoT Reasoning and In-Context Learning
Yubo Mai, Zhipeng Gao, Xing Hu, Lingfeng Bao, Yu Liu, Jianling Sun
Abstract
Nowadays, more and more developers resort to Stack Overflow for solutions (e.g., code snippets) when they encounter technical problems. Although domain experts provide huge amounts of valuable solutions in Stack Overflow, these code snippets are often difficult to reuse directly. Developers have to digest the information within relevant posts and make necessary modifications, and the whole solution-seeking process can be time-consuming and tedious. To facilitate the reuse of Stack Overflow code snippets, Terragni et al. first explored transforming a code snippet in Stack Overflow into a well-formed method API ( A pplication P rogram I nterface) by using a rule-based approach, named APIzator. The reported performance of their approach is promising, however, after our in-depth analysis of their experiment results, we find that (1) 92.5% of APIs generated by APIzator are pointless and thus are difficult to use in practice. This is because the method name generated by APIzator (extracting verb + object ) can rarely represent the method’s functionality, which can hardly be claimed as meaningful/reusable APIs. (2) The authors manually summarized a number of rules to identify parameter variables and return statements for Java methods. These hand-crafted rules are extremely complex and sophisticated, and the manual rule design process is labor-intensive and error-prone. Moreover, since these rules are designed for Java, they can hardly be extended to other programming languages. Inspired by the great potential of Large Language Models (LLMs) for solving complex coding tasks, in this paper, we propose a novel approach, named Code 2API, to automatically perform APIzation for Stack Overflow code snippets. Code 2API does not require additional model training or any manual crafting rules and can be easily deployed on personal computers without relying on other external tools. Specifically, Code 2API guides the LLMs through well-designed prompts to generate well-formed APIs for given code snippets. To elicit knowledge and logical reasoning from LLMs, we used c hain- o f- t hought (CoT) reasoning and few-shot in-context learning, which can help the LLMs fully understand the APIzation task and solve it step by step in a manner similar to a developer. Our evaluations show that Code 2API achieves a remarkable accuracy in identifying method parameters (65%) and return statements (66%) equivalent to human-generated ones, surpassing the current state-of-the-art approach, APIzator, by 15.0% and 16.5% respectively. Moreover, compared with APIzator, our user study demonstrates that Code 2API exhibits superior performance in generating meaningful method names, even surpassing the human-level performance, and developers are more willing to use APIs generated by our approach, highlighting the applicability of our tool in practice. Finally, we successfully extend our framework to the Python dataset, achieving a comparable performance with Java, which verifies the generalizability of our tool.
Ask Lune
Know this paper in minutes, not an afternoon.
Lune can read every page of it and the top-tier papers that cite it, and gives you its claims, its evidence and its limits, each one cited to the paper it comes from. No skimming, no AI slop.
5 top-tier papers have cited it since. Know what they found before your reviewers do.
Lune can draw on
the full paper · the top-tier papers that cite it · related work
Free to start. No credit card required.
Lineage
- SelfPiCo: Self-Guided Partial Code Execution with LLMs cites CodeBERT: A Pre-Trained Model for Programming and Natural Languages
- SelfPiCo: Self-Guided Partial Code Execution with LLMs cites Chain-of-Thought Prompting Elicits Reasoning in Large Language Models
- Coding in a Bubble? Evaluating LLMs in Resolving Context Adaptation Bugs during Code Adaptation cites Language Models are Few-Shot Learners
- Coding in a Bubble? Evaluating LLMs in Resolving Context Adaptation Bugs during Code Adaptation cites Chain-of-Thought Prompting Elicits Reasoning in Large Language Models
- Towards Better Answers: Automated Stack Overflow Post Updating cites SelfPiCo: Self-Guided Partial Code Execution with LLMs
- Towards Better Answers: Automated Stack Overflow Post Updating cites CodeBERT: A Pre-Trained Model for Programming and Natural Languages
- Towards Better Answers: Automated Stack Overflow Post Updating cites Chain-of-Thought Prompting Elicits Reasoning in Large Language Models
- Large Language Models are Zero-Shot Reasoners cites Language Models are Few-Shot Learners
- Large Language Models are Zero-Shot Reasoners cites Chain-of-Thought Prompting Elicits Reasoning in Large Language Models
- Large Language Models are Zero-Shot Reasoners cites Training language models to follow instructions with human feedback
- Large Language Model-Aided Partial Program Dependence Analysis cites CodeBERT: A Pre-Trained Model for Programming and Natural Languages
- Actionable Warning Is Not Enough: Recommending Valid Actionable Warnings with Weak Supervision cites SelfPiCo: Self-Guided Partial Code Execution with LLMs
- Actionable Warning Is Not Enough: Recommending Valid Actionable Warnings with Weak Supervision cites Towards Better Answers: Automated Stack Overflow Post Updating
- Chain-of-Thought Prompting Elicits Reasoning in Large Language Models cites Training language models to follow instructions with human feedback
- Training language models to follow instructions with human feedback cites Language Models are Few-Shot Learners
Cited by top-tier papers5
- SelfPiCo: Self-Guided Partial Code Execution with LLMsZhipeng Xue, Zhipeng Gao, Shaohua Wang, Xing Hu et al.ISSTA 2024 · 8 citations
- Towards Better Answers: Automated Stack Overflow Post UpdatingYubo Mai, Zhipeng Gao, Haoye Wang, Tingting Bi et al.ICSE 2025 · 4 citations
- Large Language Model-Aided Partial Program Dependence AnalysisXiaokai Rong, Aashish Yadavally, Tien N. NguyenICSE 2026 · 1 citation
- Coding in a Bubble? Evaluating LLMs in Resolving Context Adaptation Bugs during Code AdaptationTanghaoran Zhang, Xinjun Mao, Shangwen Wang, Yuxin Zhao et al.FSE 2026
- Actionable Warning Is Not Enough: Recommending Valid Actionable Warnings with Weak SupervisionZhipeng Xue, Zhipeng Gao, Tongtong Xu, Xing Hu et al.ICSE 2026
Builds on14
- Language Models are Few-Shot LearnersTom B. Brown, Benjamin Mann, Nick Ryder, Melanie Subbiah et al.NeurIPS 2020 · 64,255 citations
- Training language models to follow instructions with human feedbackLong Ouyang, Jeffrey Wu, Xu Jiang, Diogo Almeida et al.NeurIPS 2022 · 24,707 citations
- Chain-of-Thought Prompting Elicits Reasoning in Large Language ModelsJason Wei, Xuezhi Wang, Dale Schuurmans, Maarten Bosma et al.NeurIPS 2022 · 22,562 citations
- Large Language Models are Zero-Shot ReasonersTakeshi Kojima, Shixiang Shane Gu, Machel Reid, Yutaka Matsuo et al.NeurIPS 2022 · 8,168 citations
- CodeBERT: A Pre-Trained Model for Programming and Natural LanguagesZhangyin Feng, Daya Guo, Duyu Tang, Nan Duan et al.EMNLP Findings 2020 · 2,828 citations
Related papers
- CKTyper: Enhancing Type Inference for Java Code Snippets by Leveraging Crowdsourcing Knowledge in Stack OverflowAnji Li, Neng Zhang, Ying Zou, Zhixiang Chen et al.FSE 2025 · 1 citation
- APIzation: Generating Reusable APIs from StackOverflow Code SnippetsValerio Terragni, Pasquale SalzaASE 2021 · 12 citations
- ExploraCoder: Advancing Code Generation for Multiple Unseen APIs via Planning and Chained ExplorationYunkun Wang, Yue Zhang, Zhen Qin, Chen Zhi et al.ACL 2025 · 13 citations
- Question Answering as Programming for Solving Time-Sensitive QuestionsXinyu Zhu, Cheng Yang, Bei Chen, Siheng Li et al.EMNLP 2023 · 6 citations
- Instruct or Interact? Exploring and Eliciting LLMs' Capability in Code Snippet Adaptation Through Prompt EngineeringTanghaoran Zhang, Yue Yu, Xinjun Mao, Shangwen Wang et al.ICSE 2025 · 3 citations
