Beyond Functional Correctness: Investigating Coding Style Inconsistencies in Large Language Models

Yanlin Wang, Tianyue Jiang, Mingwei Liu, Jiachi Chen, Mingzhi Mao, Xilin Liu, Yuchi Ma, Zibin Zheng

TL;DR

Large language models (LLMs) are revolutionizing code generation, but their output often clashes with human coding styles, impacting readability and maintainability. This paper introduces a comprehensive taxonomy of coding style inconsistencies and empirically analyzes these differences between LLMs and human developers, revealing key areas for improvement.

What Problem Does This Paper Address?

While LLMs excel at generating functionally correct code, their output frequently deviates from established human coding conventions. This "coding style inconsistency" is a critical but under-explored aspect of LLM-generated code quality. These inconsistencies can hinder code readability, maintainability, and even introduce subtle bugs, despite the code passing automated tests. Previous research has either focused on specific aspects of coding style (like formatting) or offered broad taxonomies. This paper aims to bridge this gap by providing a detailed, empirically validated taxonomy and a comparative analysis of how mainstream LLMs stack up against human developers in terms of coding style.

Methods

The authors developed a novel, comprehensive taxonomy of coding style inconsistencies through an iterative open-coding process involving three experienced Python programmers. This taxonomy categorizes inconsistencies into five dimensions: Formatting, Semantic, Expression/Statement, Control Flow, and Fault Tolerance, encompassing 24 distinct types. They then manually annotated code samples generated by five LLMs (CodeLlama-7B, StarCoder2-7B, DeepSeekCoder-1.3B, DeepSeekCoder-6.7B, and GPT-4) against ground truth code from the CoderEval benchmark. Beyond identifying inconsistencies, they also evaluated the generated code on readability, conciseness, and robustness. Finally, they explored the impact of prompt engineering on improving coding style.

Datasets

The primary dataset used for evaluation was CoderEval, a benchmark consisting of 230 Python code generation tasks derived from real-world open-source projects. Each task includes a function signature, description, ground truth solution, and unit tests. The authors also generated a larger set of code samples from the selected LLMs for their analysis. To ensure the generalizability of their taxonomy, they also created 84 additional ground truths by having annotators rewrite existing ones.

Key Insights and Ideas

  • LLMs have distinct coding styles: While LLMs are improving, they still exhibit significant deviations from human coding styles, particularly in statement/expression and formatting aspects.
  • API Preference is a major pain point: The most frequent inconsistency type observed was "API Preference Inconsistency," suggesting LLMs struggle to consistently adopt human-like choices for library functions and built-in methods.
  • Formatting and semantics matter: Inconsistencies in code formatting (like spacing and indentation) and semantic aspects (like variable naming) are also prevalent.
  • Human code is often more concise: LLMs sometimes sacrifice conciseness for readability by splitting simple statements or using more verbose constructs.
  • Prompt engineering offers limited gains: While certain prompt strategies can slightly improve readability and robustness, they don't fully resolve style issues and can sometimes decrease functional correctness.

Key Results

  • 24 Inconsistency Types: The paper introduces a detailed taxonomy with 24 types of coding style inconsistencies across five dimensions.
  • High Inconsistency Rates: All LLMs showed coding style inconsistencies with human code, ranging from 66.2% (CodeLlama-7B) to 90.1% (GPT-4) of samples.
  • Readability, Conciseness, Robustness Comparison: On average, LLM-generated code was comparable to or better than human code in 89.5% of cases for readability, 76.5% for conciseness, and 94.2% for robustness. DeepSeekCoder-6.7B produced the most readable code, while GPT-4 was less concise but more robust.
  • Prompting Impact: Enhanced prompts showed mixed results, with some improving readability and robustness but often at the cost of conciseness or even functional correctness.

Limitations and Future Work

  • Data Quality: While efforts were made to ensure data quality (unit testing, manual filtering, using CoderEval), the inherent limitations of benchmark datasets remain a potential threat.
  • LLM Utilization: The study used specific versions and settings of LLMs, and future work could explore how different configurations or newer models impact coding style.
  • Taxonomy Generalizability: While the taxonomy was tested for generalizability, it's primarily based on Python code. Future work could explore its applicability to other programming languages.
  • Prompt Engineering Nuances: The paper suggests that prompt engineering alone is insufficient for perfect coding style. Future research could explore more sophisticated methods for guiding LLMs towards better coding practices.
  • Deeper Cause Analysis: Understanding the root causes of these style differences (e.g., training data biases, architectural choices) is a crucial area for future investigation.

Summary updated

BibTeX
@inproceedings{wang2024beyond,
  author = {Yanlin Wang and Tianyue Jiang and Mingwei Liu and Jiachi Chen and Mingzhi Mao and Xilin Liu and Yuchi Ma and Zibin Zheng},
  title = {{Beyond Functional Correctness: Investigating Coding Style Inconsistencies in Large Language Models}},
  booktitle = {Proc. ACM Softw. Eng},
  year = {2024},
}
Figure