AI/ llms · code-generation · software-quality · ai-research

Training LLMs to Write Cleaner Code, Not Just Correct Code

Researchers built a preference-learning method that nudges code-generating LLMs toward cleaner, more maintainable output without sacrificing correctness.

AI coding assistants are getting graded on cleanliness, not just whether the code runs.

Researchers built a training framework called quality-aware preference learning that fine-tunes code-generating LLMs to favor style-compliant, maintainable output over merely working output. They constructed a dataset of matched code pairs, each pairing a version that violates a coding standard with a repaired version that fixes it, then trained models to prefer the compliant one. An adaptive token-weighting mechanism emphasizes the code regions that actually determine quality, and a hybrid loss combining ranking loss, language-modeling loss, and KL divergence keeps the model from drifting into gibberish. Tested on DeepSeek-Coder and Qwen2.5-Coder, the method lifted a "Quality Reciprocal Score" by 75.7% relative on the MBPP-sanitized benchmark for Qwen2.5-Coder, without hurting functional correctness, and fine-tuning a 7B model took under three hours.

Most code-generation benchmarks reward exactly one thing: does the code pass its unit tests. That leaves room for LLMs to ship code that runs fine today and becomes a maintenance headache tomorrow - inconsistent naming, ignored style guides, functions nobody wants to touch. A fine-tuning method cheap enough to run in an afternoon suggests style compliance could get baked into the model instead of bolted on later with linters and code review.

"Quality Reciprocal Score" is the paper's own metric, not an industry standard, so a 75.7% relative gain measures progress against itself, not against how actual engineers judge readable code.

TR

The Revision

Written by an AI system from the public sources credited above. How we write →