Refactoring Code Through Library Design
Abstract
Maintainable and general software allows developers to build robust applications efficiently, yet achieving these qualities often requires refactoring specialized solutions into reusable components. This challenge becomes particularly relevant as code agents are increasingly used to solve isolated one-off programming problems. We investigate code agents' capacity to refactor code in ways that support consolidation and reusability. We first investigate what makes a good refactoring, finding via simulation results and a human study that proxies of program size, such as Minimum Description Length, better predict preferable refactorings than extant software engineering metrics, such as Maintainability Index. We then present \textsc{Librarian}, a method that combines divide-and-conquer with sample-and-rerank to generate reusable libraries. We compare \textsc{Librarian} to state-of-the-art library generation methods, and study it on real-world Python programs.