TL;DR
Large Language Models (LLMs) have shown impressive capabilities in code translation, but current benchmarks primarily focus on short, standalone code snippets. This paper introduces ClassEval-T, a new benchmark for class-level code translation, and finds that LLMs struggle significantly with this more complex task, revealing a substantial performance drop and highlighting the need for more sophisticated evaluation methods.
What Problem Does This Paper Address?
Automated code translation is crucial for migrating codebases across different programming languages (PLs), enhancing productivity and enabling broader platform support. While recent advancements in Large Language Models (LLMs) have led to significant improvements in code translation accuracy, most existing benchmarks evaluate LLMs on statement- or method-level code. These benchmarks often feature short, isolated code snippets that lack the intricate dependencies and contextual richness found in real-world software development. This gap means our understanding of LLMs' true capabilities in translating entire classes, which are fundamental units in object-oriented programming, remains limited. The authors aim to bridge this gap by creating a more realistic benchmark and thoroughly evaluating LLMs on this challenging task.
Methods
The authors introduce ClassEval-T, a new benchmark for class-level code translation. It's an extension of the ClassEval benchmark, which focuses on Python code generation. ClassEval-T consists of 94 parallel coding tasks across Python, Java, and C++, manually translated with meticulous attention to detail. To evaluate LLMs, they designed three translation strategies:
- Holistic Translation: Translating the entire class at once.
- Min-Dependency Translation: Translating modules one by one, providing minimal necessary dependencies.
- Standalone Translation: Translating modules one by one without any dependency information.
Evaluation metrics include:
- Compilation Success Rate (CSR): The percentage of translated code that compiles.
- Computational Accuracy (CA): The percentage of translated code that produces the same execution results as the ground truth. This is measured at both class-level (CAc) and method-level (CAm).
- Dependency Evaluation (DEP): Metrics (DEPF, DEPM, DEPL) to assess LLMs' awareness of field, method, and library dependencies.
Datasets
- ClassEval-T: A newly constructed benchmark for class-level code translation, comprising 94 parallel coding tasks across Python, Java, and C++. It's an extension of the ClassEval benchmark.
- Yang et al.'s dataset: A cleaned version of a method-level code translation dataset used for comparison.
Key Insights and Ideas
- Class-level translation is a major hurdle: LLMs perform significantly worse on class-level translation compared to method-level translation, with performance drops of 51-79% in accuracy and 3-56% in compilation success. This suggests current benchmarks are insufficient for evaluating real-world code translation capabilities.
- Commercial LLMs lead the pack: Larger, commercial LLMs (DeepSeek, GPT-40, Claude-3.5-Sonnet) consistently outperform general and code-specific LLMs on class-level translation.
- Python is still king: LLMs show a strong advantage when translating to Python, likely due to its prevalence in training data. Translations from C++ and Java to Python are significantly better than other translation pairs.
- Translation strategy matters: The "holistic" strategy (translating the whole class at once) works best for commercial LLMs, likely due to their larger context windows. For smaller LLMs, the optimal strategy varies, and the "standalone" strategy generally performs the worst.
- Dependency awareness is key: LLMs struggle with correctly handling dependencies (fields, methods, libraries) in class-level translation, impacting correctness. The holistic strategy seems to improve field dependency awareness, while min-dependency helps with library invocation.
Key Results
- LLMs' performance on ClassEval-T shows a marked decline compared to method-level benchmarks. For instance, C++ to Python translation saw CAc drop by 56.36% and CAm by 72.49%.
- Commercial LLMs achieved an average CAc of 26.91%, CAm of 36.96%, and CSR of 90.25% across various translation pairs on ClassEval-T.
- The holistic translation strategy improved LLMs' field dependency awareness by an average of 4-25%, while min-dependency improved library invocation by 5-24%.
- Syntax errors remain a primary issue, but class-related errors like function/variable usage and consistency issues are more prominent in C++/Java translations.
Limitations and Future Work
- Threats to Validity: The authors acknowledge potential threats to external validity (limited PLs, though widely used) and internal validity (manual construction and evaluation, though mitigated by rigorous processes). They also note potential data leakage and future contamination of benchmarks.
- Future Work: The authors propose enhancing LLMs for class-level code translation by integrating structural information (like Abstract Syntax Trees - ASTs) to reduce syntax errors and improve accuracy. They also plan to extend ClassEval-T to include more PL pairs, especially those with strong industrial relevance like C++ to Rust.
Summary updated