Enhancing Python Code Maintainability Through Large Language Model-Based Approaches
摘要
While Large Language Models (LLMs) increasingly assist in code generation, concerns persist regarding the maintainability of the code they produce–an aspect often overshadowed by functional correctness. Overlooking maintainability can contribute to technical debt and inflate long-term software costs. This research investigates whether targeted fine-tuning can enhance an LLM’s ability to generate more maintainable Python code. We developed a approach involving the curation of custom datasets (from COMMITPACKFT and Code Alpaca Python subsets) specifically annotated for maintainability using metrics like Source Lines of Code (SLOC), Halstead Effort, and Maintainability Index (MI). A weak-to-strong generalization strategy was employed, using a smaller model (Phi 4 14B) to generate maintainability-focused examples for fine-tuning a larger model (QwenCoder2.5 32B Instruct) with parameter-efficient techniques. Evaluations revealed the fine-tuned model significantly reduced code complexity (Halstead Effort) and length (SLOC) compared to the original code samples. While the model preserved high functional similarity (verified by CodeBERTScore), results for the Maintainability Index metric were inconclusive in this evaluation. Performance on standard functional correctness benchmarks (HumanEval+, MBPP+) was largely comparable to the base model. Nevertheless, expert user feedback confirmed the fine-tuned model’s utility as a practical AI companion for code refactoring to improve maintainability.