Abstract
This paper investigates the application of neural networks to the task of Code Style Transfer in Python. By defining a quantitative style space based on 22 syntactic and structural features, the authors demonstrate that pre-trained models like CodeT5 possess a latent understanding of coding signatures. While generative adversarial networks (GANs) struggled with the rigid syntax of code, a Sequence-to-Sequence (Seq2Seq) approach successfully performed complex stylistic rewrites—such as converting loops to list comprehensions or modifying identifier casing—while maintaining functional correctness.
Introduction
In natural language processing, style transfer—the ability to change the "tone" of a sentence (e.g., from formal to casual) without altering its core meaning—is a well-studied domain. In programming, "style" is equally critical for readability and maintainability, yet it remains notoriously difficult to quantify. Most research in AI for code focuses on generation or translation; however, there is a significant gap in understanding how models perceive the human notion of code aesthetics. This paper addresses two primary challenges: mathematically defining a subjective "coding style" and executing style rewrites that preserve both syntax and semantics.
Quantifying and Clustering Style
The researchers first established a framework to represent style numerically. They identified 22 specific Python features across four main categories: casing conventions, documentation density, function/class structures, and language-specific features like list comprehensions.
Using these features, the team employed the HDBSCAN algorithm to group code samples into "style buckets." This approach allowed them to identify distinct coding signatures associated with different authors or projects. To validate these buckets, they tested whether pre-trained models like CodeT5 and PLBART could identify which style cluster a snippet belonged to. Remarkably, CodeT5-Base achieved a 92% accuracy in classification, suggesting that pre-training on vast amounts of code provides these models with an inherent, latent understanding of stylistic variation.
Methodology and Model Architecture
The study explored two primary architectures for the transfer task:
- Style Transfer GAN: Initially, the authors attempted a Generative Adversarial Network approach, common in text and image processing. However, this failed to produce valid, runnable code. The strict, brittle syntax of programming languages means that even minor "hallucinations" by a GAN generator result in unparsable output.
- Seq2Seq Transfer: Pivoting to a Sequence-to-Sequence (Seq2Seq) approach proved far more robust. By fine-tuning CodeT5 on parallel corpora, the model learned to perform specific, logic-preserving transformations.
Datasets and Parallel Corpora
The models were trained using the Py150k and BigQuery datasets, comprising over 1.4 million Python files. A key contribution of this work is the generation of synthetic parallel corpora. For instance, to train a "commenting style" transfer, the authors created pairs of the same code—one version with comments and one without—allowing the model to learn the mapping between styles in a supervised fashion.
Key Results
- Atomic Transfers: The Seq2Seq models were highly successful at individual tasks. For example, converting for-loops into list comprehensions achieved a CodeBLEU score of 0.982 and an 86.9% parsability rate.
- Multi-Tasking and Zero-Shot Capabilities: The researchers successfully trained a "joint model" capable of handling multiple style changes simultaneously (e.g., adding docstrings while changing casing) via natural language prompts. This model demonstrated stylistic generalization, performing combinations of transfers it had not explicitly seen during training.
- The Importance of Identifiers: Analysis revealed that user-defined identifiers (variable and function names) carry significantly more stylistic information than standard language keywords like
iforwhile.
Limitations and Future Work
While successful, the models occasionally produced overly simplistic docstrings that failed to capture the full semantic depth of a function. Furthermore, the failure of the GAN architecture highlights the ongoing difficulty of unsupervised style transfer in domains with rigid syntax. Future research may explore larger models like Codex or BLOOM, alongside advanced prompting techniques, to capture higher-level architectural styles beyond simple syntactic changes.
Summary updated