Code odor automatic reconstruction method and device based on code large model

Through the automatic reconstruction method based on the code big model, combined with SonarQube and the large language model, the code odor is automatically recognized and repaired, and the problem of error-prone in manual operations in the existing technology is solved, and efficient and accurate code quality improvement is achieved.

CN120540657APending Publication Date: 2025-08-26SCHOOL OF SOFTWARE ZHEJIANG UNIV (NINGBO) MANAGEMENT CENT (NINGBO SOFTWARE EDUCATION CENT)
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510461612.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-04-14
Publication Date
2025-08-26

AI Technical Summary

Technical Problem

Although existing Code Smell detection methods can identify problems in the code, they lack guiding suggestions for subsequent optimization or reconstruction, rely on manual operations and are susceptible to human errors, resulting in increased code quality and maintenance costs.

Method used

The automatic reconstruction method based on the code big model is adopted, combined with the SonarQube detection tool and the large language model, and through data preprocessing, code quality analysis, reconstruction algorithm and multiple rounds of optimization, code odor is automatically identified and repaired, and code quality and maintainability are improved.

Benefits of technology

It improves the efficiency and accuracy of code reconstruction, reduces manual intervention, simplifies the difficulty of code understanding and maintenance, reduces optimization costs, and improves the readability and maintainability of the code.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120540657A_ABST
    Figure CN120540657A_ABST
Patent Text Reader

Abstract

The invention discloses an automatic code odor reconstruction method and device based on a code large model. The method is characterized by comprising the following steps: preprocessing a data set; code peculiar smell existing in the data set is detected through a SonarQube tool; reconstructing each code peculiar smell one by one by using a code large model, and obtaining a new data set through multi-round reconstruction optimization; inputting the optimized new data set into SonarQube for quality detection again, analyzing a code odor report to extract the number of code odor before and after reconstruction of five grades, calculating the code odor reconstruction and restoration proportion of each grade, and if the code odor reconstruction and restoration proportion of each grade reaches a preset threshold value, judging that automatic reconstruction of the code odor is completed, otherwise, judging that automatic reconstruction of the code odor is completed. The method has the advantages that the code peculiar smell in the codes can be effectively eliminated, the readability and maintainability of the codes are improved, the manual reconstruction amount is reduced, and the development period is shortened.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of code generation based on deep learning, and in particular to a method and device for automatically reconstructing code smell based on a large code model. Background Art

[0002] As the scale of software development continues to expand, code quality issues are becoming increasingly apparent. Code smell (Code Smell in English) is a flaw in code design, a poor design or implementation that may have an adverse impact on software maintenance. Although it may not directly lead to program errors, it will affect the readability and maintainability of the code, thereby increasing the complexity of subsequent maintenance. Code Smell is generally considered to be a bad practice performed by developers during the development process, such as redundant comments, invalid code, long methods, long parameter lists, etc. They are not vulnerabilities or errors, and the code can be compiled and executed as expected, but in the long run, these Code Smells will damage the maintainability of the software and may cause performance or security issues in the future. Therefore, identifying and eliminating Code Smell in code datasets is particularly important for ensuring code quality and reducing maintenance costs.

[0003] Traditional Code Smell detection methods primarily rely on manual code review and static analysis techniques. Static analysis tools such as PMD and Checkstyle can detect some code smells, but their detection capabilities and accuracy are often limited by the scope of their rule coverage. To address this issue, modern tools such as SonarQube have become widely used in code quality testing in recent years. SonarQube is a powerful static code analysis platform capable of analyzing multiple programming languages ​​and providing comprehensive Code Smell detection capabilities. Based on a rule base and customized detection algorithms, it automatically scans code and flags potential design flaws, duplicate code, complex code blocks, and other code smell issues. However, while SonarQube performs well in detecting Code Smell, its role is limited to identifying issues within the code and lacks guidance for subsequent optimization or refactoring. The remediation process still relies on subjective judgment and manual effort by developers, which can increase time and labor costs, and is susceptible to human error and the risk of subjective misjudgment.

[0004] In order to improve code quality, automatic code optimization technology has emerged. Tools such as SonarQube can detect code smell problems, but they cannot directly generate optimization suggestions or automatically repair code. In recent years, with the rapid development of deep learning and natural language processing technologies, pre-trained large code models have performed well in software engineering tasks such as code repair and are able to understand and process complex code scenarios. With the help of deep learning models, especially language models that have been pre-trained on a large amount of code (such as the GPT series, DeepSeek-Coder, Codex, etc.), complex code structures and logical relationships can be understood on a larger scale, and optimized code can be generated. At present, there are no public research reports on the Code Smell automatic refactoring method and device based on large code models. Summary of the Invention

[0005] The technical problem to be solved by the present invention is to provide a method and device for automatically refactoring code smell based on a large code model to improve the reconstruction efficiency, make the code more efficient and easier to maintain, reduce potential errors and performance bottlenecks, and improve code quality.

[0006] The technical solution adopted by the present invention to solve the above technical problems is: a method for automatically refactoring code smell based on a large code model, comprising the following steps:

[0007] (1) Extract Python function body code and comments from the public dataset CodeSearchNet hosted on GitHub and perform data preprocessing. After removing several abnormal files, the initial dataset is obtained.

[0008] (2) Scan and analyze the initial data set obtained in step (1) using the SonarQube code quality detection tool to detect all Code Smell types and their locations in the initial data set and obtain a SonarQube quality report;

[0009] (3) Based on the SonarQube quality report obtained in step (2), for each code smell detected, a code refactoring algorithm based on a large language model is used to embed the code snippets to be refactored into corresponding prompt word templates according to different code smell types. The templates are used as inputs of the code large model, and the refactored code that conforms to the quality optimization principle is generated through the code large model.

[0010] (4) Repeat step (3) to reconstruct each type of code smell one by one; after multiple rounds of reconstruction and repair, the optimized smell-cleaned dataset is obtained, that is, the odor-free version of the dataset;

[0011] (5) Input the optimized Smell-cleaned data set obtained in step (4) into the SonarQube code quality detection tool for quality detection again, parse the code smell report to extract the number of code smells before and after refactoring at five levels: fatal, severe, moderate, general, and mild, and calculate the refactoring repair ratio of code smells at each level. If the refactoring repair ratio of fatal and severe code smells reaches a threshold of 95-100%, the refactoring repair ratio of moderate code smells reaches a threshold of 80%, and the refactoring repair ratio of general and mild code smells reaches a threshold of 60%, then it is determined that the automatic refactoring of the code smell is completed;

[0012] (6) If the code smell refactoring repair ratio of any level obtained in step (5) does not reach the preset threshold, the Smell-cleaned dataset obtained in step (4) is scanned and analyzed using the SonarQube code quality detection tool to detect all Code Smell types and their locations in the Smell-cleaned dataset, and a new SonarQube quality report is obtained. The newly obtained SonarQube quality report repeats steps (3)-(5) until the code smell refactoring repair ratio of each level reaches the preset threshold, and it is determined that the code smell automatic refactoring is completed.

[0013] Furthermore, in step (1), the method obtains the CodeSearchNet dataset from GitHub and parses the Python data therein into several independent functions to form the original dataset for code quality exploration. CodeSearchNet is a benchmark dataset collected from real projects on GitHub, containing two million code comment and method pairs, covering multiple programming languages ​​(such as Python, JavaScript, Ruby, Java, etc.). Many LLMs, including CodeT5, StarCoder, and SantaCoder, are trained and / or evaluated on this dataset.

[0014] Furthermore, in step (2), Code Smell refers to poor designs or implementations that may have an adverse impact on software maintenance. These are not vulnerabilities or errors, but rather violations of basic code design / development principles, which may ultimately lead to a decline in code quality and the accumulation of technical debt. In other words, the code may be compiled and executed as expected, but in the long run, Code Smell may damage the maintainability of the software and cause performance or security issues in the future. The method is based on the widely used code quality management and static code analysis tool SonarQube to perform quality inspection of the original data set. It can automatically detect bugs, Code Smell, and security issues in the code, and provide detailed analysis reports to facilitate developers to optimize the code in a targeted manner. In the experiment, the Code Smell detected by the method using SonarQube includes common smells such as High Cognitive Complexity, Long Method, Commented Code, Naming Convention, Empty Nested Code Blocks, Collapsible if Statements, Dead Code, Self-assigned Variables, Return and Yield, and Identical Expressions.

[0015] Furthermore, the step (3) is specifically as follows: a large language model is constructed based on deep learning technology, combined with natural language processing technology and code refactoring tasks, which can automatically identify and understand different types of Code Smell and generate code snippets that conform to best coding practices; for partial refactoring involving code replacement, the method first generates the optimal result based on the code large model and records the differences between the previous and next codes, and then uses regular expressions to complete the global replacement; the method further includes a code optimization process, which improves the quality and execution efficiency of the code by optimizing the code at the syntax and semantic levels while refactoring the Code Smell. The quality optimization principles include the optimization principles of functional consistency and high readability, easy maintenance, and conciseness and efficiency.

[0016] Furthermore, in step (4), based on the Code Smell types and locations in the SonarQube report obtained in step (2), the method repeats step (3) with customized prompt input, completing the reconstruction of ten common types of Code Smell one by one, thereby achieving the goal of improving data quality by eliminating Code Smell. After completing the automated reconstruction of all Code Smell, the original dataset is optimized into a smell-free version of the smell-cleaned dataset.

[0017] Furthermore, in step (5), the five levels are Blocker, Critical, Major, Minor, and Trivial. If the refactoring repair ratio of different levels of smells does not reach the established threshold, the system will trigger multiple rounds of refactoring until the threshold requirement is met and the code automatic refactoring process is completed.

[0018] To achieve the above object, the present invention further provides a Code Smell automatic refactoring device based on a large code model, comprising one or more processors for implementing the above Code Smell automatic refactoring method based on a large code model.

[0019] To achieve the above-mentioned purpose, the present invention also provides an electronic device, including a memory and a processor, wherein the memory is coupled to the processor; wherein the memory is used to store program data, and the processor is used to execute the program data to implement the above-mentioned Code Smell automatic reconstruction method based on the code big model.

[0020] To achieve the above objectives, the present invention further provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the above-mentioned Code Smell automatic reconstruction method based on a large code model.

[0021] Compared with the existing technology, the advantages of the present invention are: the method and device for automatic code smell refactoring based on the code big model of the present invention, compared with the rule base or static analysis, the code big model can automatically learn the best practices in the large-scale expected library and adapt to different types of Code Smell refactoring tasks. By repairing different types of CodeSmell in an automated way, it can provide an important supplement to traditional tools and greatly improve the efficiency of code refactoring. The Code Smell refactoring technology based on the code big model no longer relies on fixed rules, but automatically summarizes and applies effective refactoring strategies through learning and training with a large amount of real code data, reducing the necessity of human intervention and improving the degree of automation and effect of code refactoring. By combining the efficient detection capabilities of SonarQube and the intelligent refactoring advantages of the code big model, it is possible to greatly improve the efficiency of Code Smell refactoring and improve the quality of code data sets on the basis of ensuring code accuracy and functional consistency.

[0022] In summary, the present invention is based on a method and device for automatically refactoring code smell based on a large code model. By combining the capabilities of SonarQube and a large code model, it is possible to automatically detect and refactor code smell problems. By eliminating various types of smells in the code, the code structure is simplified, while reducing the difficulty of understanding and maintaining the code, improving the readability and maintainability of the code, and thus improving the code quality. Through automated detection and refactoring, the workload of manually refactoring code smells is greatly reduced, the labor cost of code optimization is reduced, and development efficiency is improved. The present invention is applicable to the fields of code quality control, automatic code optimization, and software maintenance in the software development process, and is also applicable to automated optimization and maintenance scenarios of large-scale code bases. BRIEF DESCRIPTION OF THE DRAWINGS

[0023] Figure 1 It is the overall framework diagram of the method of the present invention;

[0024] Figure 2 This is an example diagram of the Code Smell test report of the present invention;

[0025] Figure 3 It is a structural schematic diagram of the device of the present invention;

[0026] Figure 4 It is a schematic diagram of an electronic device of the present invention. DETAILED DESCRIPTION

[0027] The following is a clear and complete description of the technical solutions in the embodiments of the present invention, in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. All other embodiments obtained by ordinary technicians in this field based on the embodiments of the present invention without making any creative efforts shall fall within the scope of protection of the present invention.

[0028] See also Figure 1 The present invention provides a method for automatically refactoring code smell based on a large code model, comprising the following steps:

[0029] Step 1: The present invention first obtains the open source dataset CodeSearchNet from the open source code hosting platform GitHub, and extracts Python function body code and function annotations, and obtains the initial dataset after preprocessing. CodeSearchNet is a benchmark dataset collected from real projects on GitHub, containing two million code annotations and method pairs, covering a variety of programming languages ​​​​(such as Python, JavaScript, Java, etc.). The code is at the method level granularity, and the annotation is a natural language description of the code. Many popular large code models, including CodeT5, StarCoder and SantaCoder, are trained and / or evaluated on this dataset. The present invention selects the CodeSearchNet-Python dataset for subsequent experiments, which contains 450K Python functions and their corresponding descriptions.

[0030] Specifically, the present invention first queries the CodeSearchNet dataset on the GitHub platform, then clones it locally through the git tool, and then extracts all Python functions in the dataset through the json parsing tool, records the function code and function comments, and each independent function forms an independent Python file. Then all function codes are put into the static analysis tool SonarQube to perform Code Smell detection in the Python code, and at the same time complete the feasibility detection of the Python file. SonarQube is a widely used Code Smell detection tool for improving code quality. According to the error report in the SonarQube detection log, the present invention eliminated 87 abnormal function files and finally obtained an experimental data set consisting of 457,383 function files.

[0031] During the processing, based on SonarQube's detection and review, the following cases were eliminated:

[0032] (a) The file content cannot be correctly parsed as Python code;

[0033] (b) Code files with syntax errors;

[0034] (c) The function document contains special symbols, such as http: / / ;

[0035] (d) The function documentation language contains non-English characters.

[0036] Step 2: Use SonarQube to detect all CodeSmells present in the CodeSearchNet-Python dataset.

[0037] The present invention performs static analysis on the entire data set to obtain a code quality analysis report for the entire data set. Static analysis refers to analyzing the control flow, data flow, pointer pointing and other information of the program when it is running without running the program to determine whether the program meets certain properties that people are interested in, such as whether there are defects or security vulnerabilities in the program. This embodiment completes code quality detection based on SonarQube from seven dimensions: complexity distribution, duplicate code, unit test statistics, code rule checking, annotation rate and potential bugs, and obtains a quality detection report that records the total number of Code Smells, the detailed type of Code Smell and its error cause, the location of Code Smell, violation of rule entries and other information, such as Figure 2 This paper detects over 200,000 code smells in the CodeSearchNet-Python dataset. The top ten code smell types include common smells such as High Cognitive Complexity, Long Method, Commented Code, Naming Convention, Empty Nested Code Blocks, Collapsible if Statements, DeadCode, Self-assigned Variables, Return and Yield, and Identical Expressions. A description of each code smell type is shown in Table 1.

[0038] Table 1. Top-10 Code Smells and their descriptions in the CodeSearchNet-Python dataset

[0039]

[0040]

[0041] Step 3: For all Code Smells detected in Step 2, the present invention constructs an LLM-based Code Smell removal tool, namely SmellCC, to remove various types of Code Smells in the existing dataset.

[0042] The fundamental principle of SmellCC is prompt engineering, which uses natural language to guide the LLM to complete specific tasks. Since the LLM is not designed for refactoring, this paper uses three strategies: role assignment, thought chain reasoning, and few-shot learning to leverage the knowledge of the LLM for automated Code Smell refactoring.

[0043] In prompt engineering, role assignment is a method for assigning a specific role to an LLM to solve a specific task. Assigning a role to an LLM provides context for the question, resulting in more accurate and relevant responses. This invention designs the LLM role as "professional software engineer." After assigning the role, this invention explicitly informs the LLM of the task to be performed as follows: "You are an expert software engineer with deep Python language expertise. Your task is to refactor the code to eliminate the code smell while the code function remains unchanged." This detailed task description can stimulate the LLM's programming knowledge to perform this code refactoring task. To control the accuracy of output content and facilitate subsequent code processing, this invention imposes conditional restrictions on the output content, informing the LLM of the following rules to be followed during output: "Do not generate any explanation text nearby. No other symbols, comments, etc!" This means in Chinese: "Do not generate any irrelevant content such as step-by-step explanations, comments, special symbols, etc." This provision enables LLM to only generate reconstructed code when executing tasks, and not present intermediate outputs and other content in the final code, making it easier to subsequently process other types of Code Smell.

[0044] Chain-of-Thought (CoT) is an important strategy in hint engineering, which enables LLM to decompose complex tasks into several relatively simple steps and generate a series of intermediate results, leading to reasonable answers. The task of removing different Code Smells requires logical thinking to understand the code, and a series of coherent intermediate steps to eliminate Code Smell. In order to construct a reasonable chain of thought, the present invention refers to the core steps of manually fixing 10 types of Code Smell by two developers with 6 and 9 years of Python programming experience respectively, and summarizes the chain of thought reasoning steps for each code smell through testing. The present invention takes Collapsible if Statements Smell as an example to demonstrate the thought chain reasoning process (as shown in Table 2). First, through "Collapsible if statements mean that when two "if" statements are nested, we can improve the code's readability and reduce complexity by merging them. Please refactor it following the listing steps:", the LLM is informed of the Code Smell type to be processed (i.e., Collapsible if Statements) and its definition, the overall reconstruction method (i.e., merging nested if conditions), and the specific reconstruction steps are introduced. Then, 3-4 specific reconstruction steps are formulated according to the specific type of Code Smell and the LLM is informed of the step-by-step reconstruction method. For Collapsible if Statements Smell, the reconstruction process is summarized as follows: ① Understand the task requirements; ② Analyze the content of the two conditions; ③ Determine the conjunctions; ④ Merge the conditions. This step-by-step thinking method can guide the LLM to clear Code Smell in a similar way to developers.

[0045] With the enhancement of LLM capabilities, contextual learning has been widely adopted, such as zero-shot learning and few-shot learning. Few-shot learning is to enhance the context by adding some expected input and output examples to the context, which helps the model stimulate the specific knowledge and abstract concepts required to complete a specific task. For Collapsibleif Statements Smell, the present invention provides an example of Code Smell input (such as if condition1:if condition2:#code) and expected output (for example, if condition1 and condition2:#code). With the help of these representative examples, LLM can enhance the understanding of Smell target types and their reconstruction effects.

[0046] For the large code model, we selected the DeepSeek-Coder-V2 model. It should be noted that DeepSeek-Coder is a series of code language models developed by DeepSeek AI to assist with programming and code generation. This model has multiple versions with different parameter sizes, including 1.3B, 5.7B, 6.7B, 33B, and 236B. A larger model parameter size improves performance, but also requires more memory for training and inference. We selected the latest version before our experiment, the DeepSeek-Coder-V2 model, with a parameter size of 236B. DeepSeek-Coder has demonstrated excellent capabilities in programming tasks, achieving performance comparable to GPT-4o. Furthermore, compared to GPT-4o, the DeepSeek-Coder model is more cost-effective, with an input token cost of only 2.8% of GPT-4o and an output token cost of only 1.9%. Given the relatively large dataset, we chose DeepSeek-Coder for our experiments because of its powerful functionality and cost-effectiveness. When using LLM, the temperature parameter is a key setting that controls the randomness of the model output. This paper determined the temperature to be 0.7 through a series of pilot experiments. According to the DeepSeek-Coder project documentation, the max_tokens parameter can reach 8192. When processing CodeSmells such as HighCognitiveComplexity and LongMethod, this embodiment sets max_tokens to 8192 to handle the large amount of input code in HighCognitiveComplexity and LongMethod; for other cases, this embodiment sets it to 2048 to save time and resources.

[0047] Table 2. Example of prompt word template

[0048]

[0049]

[0050] Step 4: Repeat step 3, using LLM's customized prompts for each code smell, and reconstruct each type of CodeSmell one by one according to the error location reported by SonarQube. After multiple rounds of reconstruction, the original version of the training set will be cleaned into a smell-free version by the SmellCC tool, resulting in a smell-cleaned dataset.

[0051] Step 5: Input the reconstructed dataset into SonarQube for secondary testing to obtain the quality inspection report of the reconstructed dataset. By comparing before and after, we can verify whether the CodeSmell in the initial report has been effectively removed from the Smell-cleaned dataset or still exists.

[0052] Then, the code smell report is parsed to extract the number of code smells before and after refactoring at five levels: fatal, severe, moderate, general, and minor. The refactoring repair ratio of code smells at each level is calculated. If the refactoring repair ratio of fatal and severe code smells reaches the threshold of 95-100%, the refactoring repair ratio of moderate code smells reaches the threshold of 80%, and the refactoring repair ratio of general and minor code smells reaches the threshold of 60%, then the automatic refactoring of code smells is determined to be completed.

[0053] The formula for calculating the code smell refactoring repair ratio is as follows:

[0054]

[0055] Among them, Smell before Indicates the number of a certain type of code smell before refactoring, Smell after Indicates the number of refactorings of the same type of code smell.

[0056] Among the five levels, the fatal level is Blocker, the serious level is Critical, the moderate level is Major, the general level is Minor, and the mild level is Trivial.

[0057] Step 6: If the calculated refactoring repair ratio of any level of code smell does not reach the preset threshold, the Smell-cleaned dataset obtained in Step 4 is scanned and analyzed using the SonarQube code quality detection tool to detect all CodeSmell types and their locations in the Smell-cleaned dataset, and a new SonarQube quality report is obtained. The newly obtained SonarQube quality report is repeated with steps 3-5 until the refactoring repair ratio of code smell at each level reaches the preset threshold, and the code smell automatic refactoring is determined to be completed.

[0058] The initial dataset and the optimized Smell-cleaned dataset were used to fine-tune the large code model respectively. The performance changes of the two fine-tuned large code models on the two downstream tasks of code generation and code retrieval were used as measurement indicators. The corresponding indicator data of the downstream tasks completed based on the initial dataset was used as a control group. Whether the two downstream tasks completed based on the optimized Smell-cleaned dataset can achieve improvement in indicator data is used to verify the effectiveness of improving code quality after automatic code smell refactoring. The details are as follows:

[0059] The present invention uses the original dataset and the Smell-cleaned dataset to fine-tune the code model respectively, allowing it to learn the code style with Code Smell and the optimized high-quality code practice respectively, and then let the two fine-tuned code models complete the two downstream tasks of code generation and code retrieval respectively. The performance changes on the downstream tasks are used to evaluate whether the reconstructed dataset has improved the performance of the downstream tasks, thereby reflecting whether the quality of the dataset has improved. In order to avoid the bias that may be caused by sample selection or parameter adjustment, the present invention uses full fine-tuning technology to determine the impact of odorless / odorous datasets. Four NVIDIA A800 SXM4 80GB GPUs are used for experiments, and the recommended hyperparameter settings (except for the training cycle) are adopted. The present invention sets the training cycle to 2 to save training time and computing resources.

[0060] Specifically, to verify whether removing Code Smell truly improves the quality of the dataset—that is, to verify whether using the smell-cleaned dataset as a training set can improve the code generation quality of large models—we fine-tuned DeepSeek-Coder using the original CodeSearchNet dataset and the smell-cleaned dataset, respectively. This yielded two fine-tuned models: DSC-Origin (DeepSeek-Coder fine-tuned using the original CodeSearchNet) and DSC-Clean (DeepSeek-Coder fine-tuned using the smell-cleaned dataset). After completing model fine-tuning, we conducted verification experiments on two widely used software engineering tasks: code generation and code search.

[0061] Code generation is one of the most popular and important tasks in software development, attracting the attention of both academic and industry practitioners since the emergence of LLMs. The code generation task is defined as a natural language description for which a model is required to generate code based on the natural language intent (e.g., the task description). After removing code smell from the training dataset, we investigated whether code smell-free code can reduce the learning difficulty of LLMs, thereby improving their effectiveness in code generation. We used four widely used benchmarks, HumanEval, MBPP, HumanEval+, and MBPP+, to comprehensively evaluate the performance of our models on code generation tasks. HumanEval and MBPP contain 164 and 500 programming problems, respectively. Each problem in the benchmark includes a problem description (e.g., a docstring) and a set of test cases to check the correctness of the code. For more rigorous evaluation, the EvalPlus framework achieved 80x / 35x more test results for both datasets and constructed the HumanEval+ and MBPP+ benchmarks. This paper uses two previously fine-tuned models (i.e., DSC-Origin and DSC-Clean) and then fine-tunes the models using the problem description prompt to generate the target code. The Pass@1 metric is used to evaluate the accuracy of the generated code. It measures the percentage of correct code instances generated on the first attempt, reflecting the model's accuracy in understanding and implementing user requirements. Compared to the Pass@1 of the DSC-Origin model, the DSC-Clean model has a higher Pass@1, indicating improved performance on the code generation task.

[0062] Code search is one of the most common activities in modern software development. Code search refers to retrieving relevant code snippets from a code repository based on the intent of a search query. The core idea behind using deep learning techniques or LLMs to perform code search tasks is to encode the search query and code snippet into high-dimensional embedding vectors and then calculate the similarity score between the vectors to estimate their matching scores. This paper uses DeepSeek-Coder and CodeT5 as base models and two datasets (CodeSearchNet and Smell-cleaned) to perform code search tasks. It explores whether using the Smell-cleaned dataset can enable the model to achieve better performance on code search tasks. After filtering out comments that are too short (e.g., less than 10 words), too long (e.g., more than 200 words), or contain URLs, the paper randomly extracts 1,000 comment-code pairs as a test set. Annotations are treated as search queries, and their paired code is the target code to be retrieved. For each annotation, the paper randomly selects 100 code snippets (including the target code) to construct a code candidate pool as the code repository. DeepSeek-Coder and CodeT5 are then used to encode the search query and each candidate code into vector representations, and the matching score between the two vectors is calculated based on their cosine similarity. The present invention uses MRR (Mean Reciprocal Rank) and NDCG (Normalized Discounted Cumulative Gain) indicators to evaluate model performance, which measure the frequency of successful retrieval of the target code snippet among other code snippet candidates. Taking the MRR and NDCG results of the two models DeepSeek-Coder and CodeT5 on the original dataset CodeSearchNet as the control group, the MRR and NDCG values ​​of these two models on the Smell-cleaned dataset are higher, indicating that the performance on the code search task has been improved.

[0063] MRR measures the position of the first relevant result for a query and is calculated as follows:

[0064]

[0065] Among them, |Q| represents the total number of queries, rank i Represents the ranking of the first relevant result for the i-th query. A larger MRR indicates a higher quality search result.

[0066] NDCG is an indicator that takes into account the relevance of search results and the ranking of results. It first calculates DCG (Discounted Cumulative Gain) and then normalizes it to get NDCG. Its calculation formula is:

[0067]

[0068] Among them, k represents the number of returned results, rel i represents the relevance score of the i-th result (usually a value between 0 and 1), Represents the relevance score of the i-th result under ideal ranking. NDCG reflects the quality of the overall ranking. A larger NDCG value indicates a higher quality ranking result, meaning that results with higher relevance are ranked higher in the ranking.

[0069] Corresponding to the aforementioned embodiment of the Code Smell automatic refactoring method based on a large code model, the present invention also provides an embodiment of a Code Smell automatic refactoring device based on a large code model.

[0070] See also Figure 3 The Code Smell automatic refactoring device based on a large code model provided in an embodiment of the present invention includes one or more processors for implementing the Code Smell automatic refactoring method based on a large code model in the above embodiment.

[0071] The embodiment of the Code Smell automatic reconstruction device based on the code big model of the present invention can be applied to any device with data processing capabilities, and the device with data processing capabilities can be a device or apparatus such as a computer. The device embodiment can be implemented through software, or through hardware or a combination of software and hardware. Taking software implementation as an example, as a device in a logical sense, it is formed by the processor of any device with data processing capabilities in which it is located reading the corresponding computer program instructions in the non-volatile memory into the memory for execution. From the hardware level, if Figure 3 The figure shows a hardware structure diagram of any device with data processing capability where the Code Smell automatic reconstruction device based on the code big model of the present invention is located. Figure 3 In addition to the processor, memory, network interface, and non-volatile memory shown, any device with data processing capabilities in which the apparatus in the embodiment is located may also include other hardware, generally based on the actual functions of the device with data processing capabilities, which will not be described in detail.

[0072] The implementation process of the functions and effects of each unit in the above-mentioned device is specifically described in the implementation process of the corresponding steps in the above-mentioned method, and will not be repeated here.

[0073] For the device embodiments, since they basically correspond to the method embodiments, the relevant parts can be referred to the partial description of the method embodiments. The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed across multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the present invention. A person of ordinary skill in the art can understand and implement the present invention without inventive work.

[0074] Corresponding to the embodiment of the Code Smell automatic refactoring method based on the code big model described above, the embodiment of the present application further provides an electronic device, comprising: one or more processors; a memory for storing one or more programs; when the one or more programs are executed by the one or more processors, the one or more processors implement the Code Smell automatic refactoring method based on the code big model described above. Figure 4 As shown in the figure, it is a hardware structure diagram of any device with data processing capability where the Code Smell automatic reconstruction method based on the code large model provided by the embodiment of the present application is located. Figure 4 In addition to the processor, memory, DMA controller, disk, and non-volatile memory shown, any device with data processing capabilities in which the apparatus in the embodiment is located may also include other hardware, generally based on the actual functions of the device with data processing capabilities, which will not be described in detail.

[0075] Corresponding to the embodiment of the Code Smell automatic refactoring method based on the aforementioned code large model, an embodiment of the present invention also provides a computer-readable storage medium on which a program is stored. When the program is executed by a processor, the Code Smell automatic refactoring method based on the code large model in the above embodiment is implemented.

[0076] The computer-readable storage medium may be an internal storage unit of any device with data processing capabilities described in any of the aforementioned embodiments, such as a hard disk or memory. The computer-readable storage medium may also be any device with data processing capabilities, such as a plug-in hard disk, a smart media card (SMC), an SD card, a flash card, etc. equipped on the device. Furthermore, the computer-readable storage medium may also include both an internal storage unit of any device with data processing capabilities and an external storage device. The computer-readable storage medium is used to store the computer program and other programs and data required by any device with data processing capabilities, and may also be used to temporarily store data that has been output or is to be output.

[0077] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the scope of protection of the present invention.

[0078] The above embodiments are intended only to illustrate the design concepts and features of the present invention. Their purpose is to enable those skilled in the art to understand the contents of the present invention and implement them accordingly. The scope of protection of the present invention is not limited to the above embodiments. Therefore, any equivalent changes or modifications made based on the principles and design concepts disclosed in the present invention are within the scope of protection of the present invention.

Claims

1. A method for automatically refactoring code smell based on a large code model, characterized in that: The following steps are involved: (1) Extract Python function body code and function annotation data from the public dataset CodeSearchNet hosted on GitHub, perform data preprocessing, and remove several abnormal files to obtain the initial dataset; (2) Using the SonarQube code quality detection tool to scan and analyze the initial data set obtained in step (1), detect all Code Smell types and their locations in the initial data set, and obtain a SonarQube quality report; (3) Based on the SonarQube quality report obtained in step (2), for each code smell detected, a code refactoring algorithm based on a large language model is used to embed the code snippets to be refactored into corresponding prompt word templates according to different code smell types. The templates are used as inputs of the code large model, and the refactored code that conforms to the quality optimization principle is generated through the code large model. (4) Repeat step (3) to reconstruct each type of code smell one by one; After multiple rounds of reconstruction and repair, the optimized Smell-cleaned dataset is obtained, that is, the odor-free version of the dataset; (5) Input the optimized Smell-cleaned data set obtained in step (4) into the SonarQube code quality detection tool for quality detection again, parse the code smell report to extract the number of code smells before and after refactoring at five levels: fatal, severe, moderate, general, and mild, and calculate the refactoring repair ratio of code smells at each level. If the refactoring repair ratio of fatal and severe code smells reaches a threshold of 95-100%, the refactoring repair ratio of moderate code smells reaches a threshold of 80%, and the refactoring repair ratio of general and mild code smells reaches a threshold of 60%, then it is determined that the automatic refactoring of the code smell is completed; (6) If the code smell refactoring repair ratio of any level obtained in step (5) does not reach the preset threshold, the Smell-cleaned dataset obtained in step (4) is scanned and analyzed using the SonarQube code quality detection tool to detect all Code Smell types and their locations in the Smell-cleaned dataset, and a new SonarQube quality report is obtained. The newly obtained SonarQube quality report repeats steps (3)-(5) until the code smell refactoring repair ratio of each level reaches the preset threshold, and it is determined that the code smell automatic refactoring is completed.

2. The method for automatically refactoring code smell based on a large code model according to claim 1 is characterized in that: In step (1), the method obtains the CodeSearchNet dataset from GitHub and parses the Python data into several independent functions to form the original dataset for code quality exploration.

3. The method for automatically refactoring code smell based on a large code model according to claim 1, characterized in that: In step (2), the Code Smell types include High Cognitive Complexity, LongMethod, CommentedCode, Naming Convention, Empty Nested Code Blocks, Collapsible if Statements, Dead Code, Self-assigned Variables, Return and Yield, and Identical Expressions.

4. The method for automatically refactoring code smell based on a large code model according to claim 1, characterized in that: In step (3), the quality optimization principles include functional consistency and high readability, easy maintenance, and refined and efficient optimization principles.

5. The method for automatically refactoring code smell based on a large code model according to claim 1, characterized in that: In step (4), according to the code smell types and locations in the SonarQube report obtained in step (2), the method will combine customized prompt input and repeat the operation of step (3) to complete the reconstruction of ten common types of Code Smell one by one, so as to achieve the purpose of improving data quality by eliminating code smells.

6. The method for automatically refactoring code smell based on a large code model according to claim 1, characterized in that: In the step (5), among the five levels, the fatal level is the Blocker level, the serious level is the Critical level, the medium level is the Major level, the general level is the Minor level, and the mild level is the Trivial level.

7. A code smell automatic reconstruction device based on a large code model, characterized in that: The method comprises one or more processors for implementing the method for automatically refactoring code smell based on a large code model according to any one of claims 1 to 6.

8. An electronic device comprising a memory and a processor, characterized in that: The memory is coupled to the processor; wherein the memory is used to store program data, and the processor is used to execute the program data to implement the code smell automatic refactoring method based on the code large model as described in any one of claims 1 to 6.

9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the method for automatically refactoring code smell based on a large code model as described in any one of claims 1 to 6 is implemented.