CodeStylist: A System for Performing Code Style Transfer Using Neural Networks

Chih-Kai Ting, Karl Munson, Serenity Wade, Anish Savla, Kiran Kate, Kavitha SrinivasAAAI 2023

Abstract

CodeStylist is a novel system that utilizes neural networks to automatically perform complex code style transfers, such as converting for loops to list comprehensions or wrapping standalone functions into classes. By fine-tuning the CodeT5 language model on parallel code corpora, the system addresses semantic style changes that traditional IDEs cannot easily automate. This paper presents the architecture, data generation methodology, and qualitative results of the CodeStylist system, demonstrating the viability of using Large Language Models (LLMs) for automated code refactoring and style compliance.

Introduction

In software engineering, "code style" encompasses more than just visual formatting; it dictates the readability, maintainability, and performance of a codebase. While standard tools like Black or Prettier are effective for indentation and line length, they cannot handle deeper, semantic style requirements. For instance, enterprise guidelines may require "Pythonic" idioms or specific object-oriented encapsulations. CodeStylist addresses this gap by treating complex code refactoring as a neural "style transfer" problem, preserving underlying meaning while altering structural representation.

Methods

The system is built upon CodeT5, a pre-trained encoder-decoder model for code. To overcome the lack of parallel "bad-to-good" code datasets, the authors employed a synthetic data generation strategy. They mined open-source GitHub repositories for target styles and programmatically "de-styled" them into source styles, creating a parallel corpus for training.

The CodeStylist workflow: from parallel corpora generation to multi-task prompting and styled script generation.

CodeStylist supports both task-specific models and a multi-task model. The multi-task variant uses natural language prompts (e.g., <nl> replace for loop with list comprehension </nl>) to guide the transformation process. This approach allows a single model to handle diverse refactoring tasks by prepending instructions to the input code.

Key Results and Insights

CodeStylist demonstrates qualitative success across several complex transformations, including PEP 8 casing compliance, automatic docstring generation, and structural refactoring. A key insight from the research is that code style transfer is akin to natural language style transfer, requiring semantic understanding rather than just syntax manipulation.

A complex transformation example showing simultaneous conversion of functions to classes and loops to list comprehensions.

As shown in the example above, the system can simultaneously handle multiple refactoring goals, such as encapsulating logic into a class while modernizing loop structures. While task-specific models generally outperform the multi-task model in isolation, the multi-task approach provides a unified and flexible interface for developers.

Limitations and Future Work

While CodeStylist serves as a strong proof-of-concept, several challenges remain. The authors acknowledge that ensuring semantic equivalence—guaranteeing the code still runs exactly as it did before—is difficult for generative neural models. Future work will likely focus on improving the reliability of these outputs and expanding the range of supported transformations to further bridge the gap between manual refactoring and automated style compliance.

Summary updated

BibTeX
@inproceedings{ting2023codestylist,
  author = {Chih-Kai Ting and Karl Munson and Serenity Wade and Anish Savla and Kiran Kate and Kavitha Srinivas},
  title = {{CodeStylist: A System for Performing Code Style Transfer Using Neural Networks}},
  booktitle = {AAAI Conference on Artificial Intelligence},
  year = {2023},
  doi = {10.1609/aaai.v37i13.27087},
}
Figure