Code odor detection method and system based on large language model
By constructing a prompt word and hierarchical adjustment strategy for large language models, the misjudgment and information loss of code odor detection in the existing technology is solved, and accurate detection and efficient analysis of complex code structures are achieved.
Patent Information
- Application Number
- CN202510338368.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-03-21
- Publication Date
- 2025-08-01
AI Technical Summary
In the prior art, Feature Envy detection method has misjudgment or misjudgment when facing complex code structures, making it difficult to accurately model the dynamic behavior of the code. In addition, large language models rely on ordinary prompt words in code odor detection, resulting in information loss or incomplete analysis.
By extracting the context information of the code to be detected, building a collection of methods and class information, combining the thinking chain and thinking tree to build a prompt word for a large language model, and using a hierarchical code replacement strategy, dynamically adjusting the context code information and inputting it to the large language model for code odor detection.
Improves the accuracy and efficiency of code odor detection, reduces misjudgment and misjudgment, ensures that long code snippets are not limited by context window size, and provides comprehensive analysis results.
Smart Images

Figure CN120407366A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of code smell detection, and particularly to a code smell detection method and system based on a large language model. Background Art
[0002] In the field of software engineering, code quality has an important impact on the maintainability, readability, and scalability of software. With the growth of software scale, the problem of code smells has become increasingly prominent. Among them, Feature Envy is a common code smell, which means that a certain method has less dependence on its own class and more dependence on other classes, thus affecting the modularity and cohesion of the code. Feature Envy may lead to problems of high coupling and low cohesion, making the code difficult to maintain and reducing the evolution ability of the software.
[0003] Traditional Feature Envy detection methods mainly rely on static analysis tools or rule-based methods, which may have misjudgments or missed judgments when facing complex code structures, and it is difficult to accurately model the dynamic behavior of the code, resulting in limited accuracy of the detection results.
[0004] In addition, with the rapid development of large language models, they have shown great potential in code understanding, automated analysis, code completion, etc. Large language models can utilize their powerful context reasoning ability to perform complex logical analysis based on natural language and code inputs, providing a new solution for code smell detection.
[0005] However, large language models usually rely on ordinary prompts for end-to-end reasoning in code smell detection, without introducing a clear reasoning structure. When dealing with long code segments, large language models may be limited by the context window size, resulting in information loss or incomplete analysis. Summary of the Invention
[0006] In view of the above deficiencies of the prior art, the purpose of the embodiments of the present invention is to provide a code smell detection method based on a large language model, which can solve the technical problems in the prior art that traditional Feature Envy detection methods mainly rely on static analysis tools or rule-based methods, may have misjudgments or missed judgments when facing complex code structures, are difficult to accurately model the dynamic behavior of the code, resulting in limited accuracy of the detection results, and large language models usually rely on ordinary prompts for end-to-end reasoning in code smell detection, without introducing a clear reasoning structure, and large language models may be limited by the context window size when dealing with long code segments, resulting in information loss or incomplete analysis.
[0007] In the first aspect of the embodiments of the present invention, a code smell detection method based on a large language model is proposed, including:
[0008] S1: Extract the context code information of the code to be detected, where the context code information includes method code and class code;
[0009] S2: Respectively extract the key information of the method code and class code, and construct a method information set and a class information set;
[0010] S3: Construct a large language model;
[0011] S4: Based on the chain of thought and the tree of thought, construct the prompt words of the large language model;
[0012] S5: Construct a hierarchical code replacement strategy;
[0013] S6: According to the code replacement strategy, use the method information set and the class information set to dynamically adjust the context code information;
[0014] S7: Input the prompt words and the dynamically adjusted context code information into the large language model for code smell detection, and output the code smell detection result of the code to be detected.
[0015] In the second aspect of the embodiments of the present invention, a code smell detection system based on a large language model is proposed, including: a processor and a memory;
[0016] The memory stores a program or instruction that can run on the processor, and when the program or instruction is executed by the processor, the steps of the code smell detection method based on the large language model as described in the first aspect are implemented.
[0017] In the third aspect of the embodiments of the present invention, a readable storage medium is proposed, and a program or instruction is stored on the readable storage medium, and when the program or instruction is executed by a processor, the steps of the code smell detection method based on the large language model as described in the first aspect are implemented.
[0018] The beneficial effects brought by the technical solutions provided by the embodiments of the present invention at least include:
[0019] In the embodiments of the present invention, by constructing a large language model, it no longer depends on static analysis tools or rule-based methods, reduces the phenomenon of misjudgment or missed judgment when facing complex code structures, can accurately model the dynamic behavior of the code, and improves the accuracy of the detection results. By constructing the prompt words of the large language model based on the chain of thought and the tree of thought, the large language model no longer depends on ordinary prompt words for end-to-end reasoning in code smell detection, and can introduce a clear reasoning structure. By constructing a hierarchical code replacement strategy, and according to the code replacement strategy, using the method information set and the class information set to dynamically adjust the context code information, it will not be restricted by the context window size when processing long code fragments, and avoids the phenomenon of information loss or incomplete analysis. Description of the Drawings
[0020] The drawings are only for the purpose of showing specific embodiments and are not considered as a limitation of the present invention. Throughout the drawings, the same reference numerals represent the same components. Obviously, the drawings in the following description are only some embodiments described in the embodiments of the present invention. For those of ordinary skill in the art, other drawings can be obtained based on these drawings without creative efforts.
[0021] Figure 1 is a schematic flowchart of a code smell detection method based on a large language model provided by an embodiment of the present invention;
[0022] Figure 2 is a schematic structural diagram of a code smell detection system based on a large language model provided by an embodiment of the present invention. Detailed Embodiments
[0023] In order to enable those skilled in the art to better understand the technical solutions in the embodiments of the present invention, the technical solutions of the present invention will be clearly and completely described below with reference to the drawings. Obviously, the described embodiments are some, but not all, of the embodiments of the present invention. It should be understood that these descriptions are only exemplary and are not used to limit the scope of the present invention. All other embodiments obtained by those of ordinary skill in the art based on the embodiments of the present invention without creative efforts shall fall within the scope of protection of the present invention.
[0024] The following will, with reference to the drawings, explain in detail the code smell detection method based on a large language model provided by the embodiments of the present invention through specific embodiments and their application scenarios.
[0025] Refer to the appended Figure 1 , which shows a schematic flowchart of a code smell detection method based on a large language model provided by an embodiment of the present invention.
[0026] The embodiments of the present invention provide a code smell detection method based on a large language model, which may include the following steps:
[0027] S1: Extract the context code information of the code to be detected, where the context code information includes method code and class code.
[0028] In the embodiments of the present invention, extracting context code information helps to comprehensively analyze the structure and behavior of the code, provide a complete context for detecting code smells, and improve accuracy.
[0029] In a possible implementation, S1 specifically includes sub-steps S101 to S103:
[0030] S101: Clone the target code repository from the open-source code repository to the local side.
[0031] Optionally, clone the target code repository from the open-source code repository GitHub to the local side.
[0032] It should be noted that GitHub is a code hosting platform based on Git, which allows developers to store and manage code projects in the cloud. It supports version control, can track the historical changes of code, and makes it more convenient for multiple people to collaborate. Developers can create code repositories, commit code, perform code merges, and manage the progress and issues of projects through GitHub. In addition, GitHub also provides functions for open-source project sharing and collaboration, and is widely used in open-source software development and team collaboration.
[0033] S102: Parse the target code repository and build a database corresponding to the target code repository.
[0034] Optionally, use the SciTools Understand static analysis tool to parse the target code repository and build a database corresponding to the target code repository to support code analysis and information extraction.
[0035] It should be noted that SciTools Understand is a static code analysis tool mainly used for analyzing and understanding source code. It supports multiple programming languages, including C / C++, Java, Python, etc. Through detailed static analysis of the code, SciTools Understand can generate a structured view of the code to help developers understand the various components of the code and their relationships. It provides various functions, such as code dependency analysis, call graph, class diagram, function hierarchy structure, etc., and also supports generating code documentation and reports. Developers can use SciTools Understand to efficiently conduct code reviews, refactoring, debugging, and optimization, especially useful in large code repositories.
[0036] S103: Extract the context code information of the code to be detected from the database according to the name of the code to be detected:
[0037] CodeSet = f extract (R,M)
[0038] = {Method code, Class code}
[0039] Among them, CodeSet represents the context code information, f extract() represents the context code information extraction function, R represents the open-source code repository, M represents the name of the code to be detected, Method code represents the method code, and Class code represents the class code.
[0040] Optionally, according to the name of the code to be detected, use the SciTools Understand static analysis tool to extract the context code information of the code to be detected from the database.
[0041] In the embodiment of the present invention, by extracting and parsing the context information of the code to be detected from the open-source code repository, combined with GitHub for version control and collaboration, the code history can be traced and multi-person cooperation can be supported. Using SciTools Understand for static code analysis can generate a detailed code structure view, helping developers understand the dependencies between codes and accurately discover potential code smells. This method supports multiple programming languages and is especially suitable for large and complex code libraries, improving the accuracy of analysis. Through the automated analysis process, manual intervention can be reduced, the detection efficiency can be improved, and ultimately the code quality and maintainability can be enhanced.
[0042] In a possible implementation manner, after S1 and before S2, it further includes:
[0043] S1A: Perform code preprocessing on the context code information:
[0044] CodeSet' = CodeSet \ Comments(CodeSet) \ Whitespace(CodeSet)
[0045] Where CodeSet' represents the context code information after code preprocessing, CodeSet represents the context code information, \ represents the difference operator, Comments(CodeSet) represents the comment content in the context code information, and Whitespace(CodeSet) represents the whitespace characters in the context code information.
[0046] In the embodiment of the present invention, preprocessing the context code information can remove unnecessary comments and whitespace characters, reduce redundant information, and improve the efficiency and accuracy of model analysis.
[0047] In a possible implementation manner, S1A specifically includes sub-steps S1A01 to S1A03:
[0048] S1A01: Delete the comment content in the context code information.
[0049] Specifically, single-line comments, multi-line comments, and documentation comments in the context code information are deleted. Although these comment contents are helpful for understanding the code, their importance is relatively low under the condition of limited input, so they can be removed to save input space.
[0050] S1A02: Delete the white space characters in the context code information after deleting the comment contents.
[0051] Specifically, delete the meaningless spaces, line breaks, and tab characters in the context code information to ensure consistent code format and reduce the interference of unnecessary characters on the model.
[0052] S1A03: Standardize the context code information after deleting the white space characters.
[0053] Specifically, uniformly standardize the identifiers, keywords, and formats in the code (such as unifying the indentation style) to improve the consistency and readability of the code.
[0054] In the embodiments of the present invention, deleting unnecessary comment contents can save input space and improve processing efficiency. Removing meaningless white space characters ensures consistent code format and reduces interference with model analysis. By standardizing the identifiers, keywords, and formats of the code, the consistency and readability of the code are improved, helping the large language model better understand the code structure. The overall preprocessing process optimizes the input of the model and improves the accuracy and efficiency of analysis.
[0055] S2: Extract the key information of the method code and the class code respectively, and construct a method information set and a class information set.
[0056] Optionally, the method information set is specifically:
[0057]
[0058] Among them, MIS represents the method information set, Class Name represents the name of the class to which the code to be detected belongs, InvokedMethods represents the set of other codes called inside the code to be detected, Accessed Fields represents the set of fields accessed by the code to be detected, Instantiated Objects represents the set of types of newly created objects by the code to be detected, MethodParameter Types represents the set of input parameter types of the code to be detected, and Return Type represents the type of the return value of the code to be detected.
[0059] The class information set is specifically:
[0060]
[0061] Among them, CIS represents the set of class information, Fields represents the set of fields defined by the class, Methods represents the set of code defined by the class, Constructors represents the set of constructor code of the class, Interfaces represents the set of interfaces implemented by the class, and Superclass represents the name of the parent class inherited by the class.
[0062] In the embodiments of the present invention, by extracting core information such as method calls, field accesses, parameter types, etc., it can help large language models more accurately understand the structure and behavior of the code, and improve the accuracy of code smell detection. This can not only reduce redundant information, improve analysis efficiency, but also ensure that the model can handle complex code structures while simplifying the input data, support more comprehensive analysis, and thus provide more accurate detection results.
[0063] S3: Build a large language model.
[0064] It should be noted that large language models (LLMs) refer to models trained with a large amount of data and capable of understanding and generating natural language text. They are based on the Transformer architecture and are widely used in natural language processing tasks, with powerful reasoning and generation capabilities. In code smell detection, large language models help identify potential code smell problems (such as Feature Envy) by understanding the code context and analyzing the code structure, and improve code quality and maintainability through fine-tuning for specific tasks.
[0065] It should be noted that those skilled in the art can set the types of large language models according to actual needs, and the present invention does not make any limitations here.
[0066] Optionally, build a large language model based on GPT.
[0067] It should be noted that GPT (Generative Pre-trained Transformer) is a natural language processing model based on deep learning developed by OpenAI. GPT uses the Transformer architecture and understands and generates natural language text through large-scale data pre-training. Its basic principle is to perform pre-training on a large amount of text data and then fine-tune to adapt to specific tasks such as text generation, question answering, translation, and summarization. The strength of GPT lies in its ability to generate coherent and logical text based on the given context and can handle various language understanding and generation tasks, and is widely used in multiple fields such as dialogue systems, content creation, and programming assistance.
[0068] In the embodiments of the present invention, building a large language model helps to accurately understand the code context, identify code smell problems, and improve code quality and maintainability.
[0069] S4: Based on the Chain-of-Thought and the Tree-of-Thought, construct prompts for the large language model.
[0070] It should be noted that the Chain-of-Thought (CoT) is a technique that guides the large language model to perform reasoning. It gradually analyzes the problem through linear steps and finally reaches a conclusion. The key to the Chain-of-Thought is to decompose complex problems into a series of simple reasoning steps, with each step based on the conclusion of the previous step, ensuring clear and consistent logic when the model generates answers.
[0071] It should be noted that the Tree-of-Thought (ToT) is a multi-path reasoning method. It simulates multiple experts to independently analyze problems from different perspectives and finally summarizes the conclusions from each perspective to obtain the final answer. Different from the linear reasoning of the Chain-of-Thought, the Tree-of-Thought allows multiple reasoning paths to proceed in parallel, enabling a more comprehensive understanding and solution of complex problems, especially suitable for scenarios that require multi-perspective analysis.
[0072] Optionally, the content of the Chain-of-Thought specifically includes: one role setting, one task description, one label definition, and one input clarification.
[0073] One role setting is specifically: Set the large language model as the first code analysis assistant for detecting code smells.
[0074] One task description is specifically: Through the first code analysis assistant, gradually detect the interaction relationship between the context code information and the external class code to determine whether there is a code smell. If so, answer yes. Otherwise, answer no.
[0075] One label definition is specifically: Provide the first label defining the code smell.
[0076] One input clarification is specifically: Input the context code information into the large language model.
[0077] Specifically, based on the Chain-of-Thought prompt, adopt a linear reasoning method to guide the large language model to gradually analyze the code. First, the model is defined as an intelligent assistant specialized in detecting code smells. Next, the model gradually analyzes the interaction between the context code information and the external class, and finally provides a binary "yes" or "no" judgment. Then, clearly provide the definition of the code smell to ensure the consistency of the evaluation criteria. Finally, input the context code information to provide a comprehensive basis for the analysis.
[0078] Optionally, the content of the Tree-of-Thought specifically includes: two role settings, two task descriptions, two label definitions, and two input clarifications.
[0079] The secondary role setting is specifically: setting the large language model as a second code analysis assistant composed of multiple experts.
[0080] The second task is described as follows: Use the second code analysis assistant to comprehensively determine whether the contextual code information contains code smells from the perspectives of various experts. If so, answer yes. Otherwise, answer no.
[0081] The secondary tag definition specifically provides a second tag that defines code smells.
[0082] Secondary input clarification is specifically: inputting context code information into the large language model.
[0083] Specifically, based on the mind tree prompt, a multi-perspective reasoning approach is employed to simulate the expert discussion process. This guides the model to independently analyze the code along different logical paths, then synthesizes the conclusions from multiple perspectives to reach a final decision. While the overall process is similar to that of the mind chain-based prompt word, the key difference lies in the task description phase. In the mind tree, the prompt word creates multiple virtual "experts," each representing an independent reasoning path and analyzing the code from a different perspective. Through this multi-path reasoning approach, the model is able to gain a more comprehensive understanding of code characteristics in complex environments.
[0084] In an embodiment of the present invention, the Chain of Thought (CoT) analyzes the problem step by step through linear reasoning to ensure that each step of reasoning is clear and consistent, thereby improving the accuracy of code smell detection and the stability of the results. It is suitable for relatively simple and direct tasks, and guides the model to perform effective reasoning through clear role settings, task descriptions, label definitions, and input clarifications. In contrast, the Tree of Thought (ToT) adopts a multi-perspective reasoning method, which simulates multiple experts to independently analyze the code from different perspectives, and finally integrates the conclusions of each perspective to provide a more comprehensive understanding and judgment. This method is particularly suitable for complex smell detection tasks, and can reduce the deviation caused by single-path reasoning, and ensure the accuracy and robustness of the analysis results from multiple perspectives. The combination of the two can comprehensively improve the understanding and analysis capabilities of large language models for code smells.
[0085] S5: Build a layer-based code replacement strategy.
[0086] Optionally, the levels include a first level, a second level, and a third level.
[0087] In an embodiment of the present invention, a hierarchical code replacement strategy is constructed to dynamically adjust the input content according to the code length, ensuring that a large language model can be efficiently processed and maintain analysis accuracy in different situations.
[0088] S6: According to the code replacement strategy, the context code information is dynamically adjusted using the method information set and the class information set.
[0089] In an embodiment of the present invention, the context code information can be dynamically adjusted according to the code replacement strategy, which can optimize the input content and ensure the efficiency and accuracy of the large language model when processing long codes.
[0090] In a possible implementation, S6 specifically includes sub-steps S601 to S607:
[0091] S601: Calculate the first comprehensive code length through an addition operation according to the code lengths of the method code and the class code.
[0092] Specifically, add the code length of the method code and the code length of the class code to obtain the first comprehensive code length.
[0093] S602: Determine whether the first comprehensive code length is less than or equal to the preset code length of the large language model. If so, proceed to step S603. Otherwise, proceed to step S604.
[0094] It should be noted that those skilled in the art can set the size of the preset code length according to actual needs, and the present invention does not limit this here.
[0095] S603: According to the first level, keep the method code and the class code unchanged to make a first adjustment to the context code information:
[0096] CodeSet1 = Method code + Class code L = L1
[0097] Where, CodeSet1 represents the context code information after the first adjustment, Method code represents the method code, Class code represents the class code, L represents the level, and L1 represents the first level.
[0098] S604: According to the second level, replace the class code with the class information set to make a second adjustment to the context code information:
[0099] CodeSet2 = Method code + CIS L = L2
[0100] Where, CodeSet2 represents the context code information after the second adjustment, CIS represents the class information set, and L2 represents the second level.
[0101] S605: Calculate the second comprehensive code length through an addition operation according to the code lengths of the method code and the class information set.
[0102] Specifically, add the code length of the method code and the code length of the class information set to obtain the second comprehensive code length.
[0103] S606: Determine whether the length of the second comprehensive code is less than or equal to the preset code length of the large language model. If so, keep the context code information after the second adjustment unchanged. Otherwise, proceed to step S607:
[0104] S607: According to the third level, replace the method code with a set of method information to perform a third adjustment on the context code information:
[0105] CodeSet3 = MIS + CIS L = L3
[0106] Among them, CodeSet3 represents the context code information after the third adjustment, and L3 represents the third level.
[0107] In the embodiments of the present invention, first, dynamically adjusting the input content can optimize the code simplicity while ensuring that sufficient context information is retained to adapt to the input length limit of the large language model. Second, by adjusting the input content according to the levels, the performance degradation caused by too long input is avoided, and the model processing efficiency is improved. At the same time, using a set of method information and a set of class information to replace the complete code can enhance the model's analysis ability, reduce redundant information, and ensure the accuracy of the analysis. Finally, according to the different code lengths, through a flexible level strategy, the model can efficiently process codes of various scales, improving the detection accuracy and robustness.
[0108] S7: Input the prompt word and the context code information after dynamic adjustment into the large language model for code smell detection, and output the code smell detection result of the code to be detected.
[0109] Specifically, according to the following formula, input the prompt word and the context code information after dynamic adjustment into the large language model for code smell detection, and output the code smell detection result of the code to be detected:
[0110]
[0111] Among them, D represents the code smell detection result, f LLM represents the code smell detection function of the large language model, P represents the prompt word, and CodeSet i represents the context code information adjusted according to the i-th level.
[0112] In the embodiments of the present invention, first of all, this method can improve the detection accuracy. By combining the prompt words and the adjusted context code information, it ensures that the large language model can comprehensively understand the context of the code. Secondly, the dynamically adjusted input guarantees that the input length is suitable for the processing ability of the large language model, optimizing the analysis efficiency and avoiding performance bottlenecks. In addition, according to the adjustment strategies at different levels, it can improve the consistency and robustness of the analysis, comprehensively understand the code smell problem from multiple dimensions, and enhance the accuracy and efficiency of the detection.
[0113] The beneficial effects brought by the technical solutions provided in the embodiments of the present invention at least include:
[0114] In the embodiments of the present invention, a large language model is constructed, no longer relying on static analysis tools or rule-based methods, reducing the phenomenon of misjudgment or missed judgment when facing complex code structures, being able to accurately model the dynamic behavior of the code, and improving the accuracy of the detection results. By constructing the prompt words of the large language model based on the chain of thought and the tree of thought, the large language model no longer relies on ordinary prompt words for end-to-end reasoning in code smell detection, and can introduce a clear reasoning structure. By constructing a hierarchical code replacement strategy, and according to the code replacement strategy, using the method information set and the class information set to dynamically adjust the context code information, it will not be restricted by the context window size when processing long code segments, avoiding the phenomenon of information loss or incomplete analysis.
[0115] Refer to the attached Figure 2 drawings, which shows a schematic structural diagram of a code smell detection system based on a large language model provided in the embodiments of the present invention.
[0116] The embodiments of the present invention provide a code smell detection system 20 based on a large language model, including: a processor 201 and a memory 202;
[0117] The memory 202 stores programs or instructions that can run on the processor 201. When the programs or instructions are executed by the processor 201, they implement the steps of the above-mentioned code smell detection method based on a large language model and can achieve the same technical effects. To avoid repetition, the present invention will not elaborate further.
[0118] It should be understood that the processor 201 in the embodiments of the present invention may be a central processing unit (CPU), and the processor may also be other general-purpose processors, digital signal processors (DSPs), application specific integrated circuits (ASICs), field programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor may be a microprocessor or the processor may also be any conventional processor, etc.
[0119] It should also be understood that the memory 202 in the embodiments of the present invention may be a volatile memory or a non-volatile memory, or may include both volatile and non-volatile memories. Among them, the non-volatile memory may be a read-only memory (ROM), a programmable ROM (PROM), an erasable PROM (EPROM), an electrically erasable PROM (EEPROM), or a flash memory. The volatile memory may be a random access memory (RAM), which is used as an external cache. By way of example but not limitation, many forms of random access memory (RAM) are available, such as static random access memory (SRAM), dynamic random access memory (DRAM), synchronous dynamic random access memory (SDRAM), double data rate synchronous dynamic random access memory (DDR SDRAM), enhanced SDRAM (ESDRAM), synchlink DRAM (SLDRAM), and direct rambus RAM (DR RAM).
[0120] The above embodiments can be implemented in whole or in part through software, hardware (such as circuits), firmware, or any other combination. When implemented using software, the above embodiments can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions or computer programs. When the computer instructions or computer program are loaded or executed on a computer, the processes or functions described in accordance with the embodiments of the present invention are generated in whole or in part. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another. For example, the computer instructions can be transmitted from one website, computer, server, or data center to another website, computer, server, or data center via a wired method (such as infrared, wireless, microwave, etc.). The computer-readable storage medium can be any available medium that can be accessed by a computer or a data storage device such as a server or data center that contains a collection of one or more available media. The available medium can be a magnetic medium (such as a floppy disk, hard disk, or magnetic tape), an optical medium (such as a DVD), or a semiconductor medium. The semiconductor medium can be a solid-state drive.
[0121] It should be understood that in various embodiments of the present invention, the size of the serial numbers of the above-mentioned processes does not mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present invention.
[0122] Those skilled in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professionals and technicians can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of the present invention.
[0123] Those skilled in the art will clearly understand that, for the convenience and brevity of description, the specific working processes of the above-described equipment, devices and units can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.
[0124] In several embodiments provided by the present invention, it should be understood that the disclosed devices, apparatuses, and methods can be implemented in other ways. For example, the device embodiments described above are merely illustrative. For example, the division of the units is only a logical function division. In actual implementation, there may be other division methods. For example, multiple units or components can be combined or integrated into another device, or some features can be ignored or not executed. Another point is that the displayed or discussed couplings or direct couplings or communication connections to each other can be through some interfaces. The indirect couplings or communication connections of the devices or units can be in electrical, mechanical, or other forms.
[0125] The units described as separate components may or may not be physically separated. The components displayed as units may or may not be physical units, that is, they can be located in one place or distributed to multiple network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0126] In addition, in each embodiment of the present invention, the functional units can be integrated in a processing unit, or each unit can exist physically alone, or two or more units can be integrated in one unit.
[0127] If the functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, or a part of this technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions for causing a computer device (which can be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the methods described in each embodiment of the present invention. The aforementioned storage medium includes: USB flash drives, mobile hard disks, read-only memories (ROM), random access memories (RAM), magnetic disks, or optical discs, etc., all kinds of media that can store program codes.
[0128] The embodiments of the present invention provide a readable storage medium including: programs or instructions are stored on the readable storage medium. When the programs or instructions are executed by a processor, the steps of the above-mentioned code smell detection method based on a large language model are implemented, and the same technical effects can be achieved. To avoid repetition, the present invention will not be elaborated herein.
[0129] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the embodiments of the present invention, rather than to limit them. Although the present invention has been described in detail with reference to the foregoing embodiments, those of ordinary skill in the art should understand that they can still modify the technical solutions recorded in the foregoing embodiments, or perform equivalent replacements on some of the technical features; and these modifications or replacements do not make the essence of the corresponding technical solutions deviate from the spirit and scope of the technical solutions of the embodiments of the present invention. Any changes or replacements that can be easily thought of by those skilled in the art within the technical scope disclosed by the present invention should be covered within the protection scope of the present invention.
Claims
1. A method for detecting code smells based on large language models, characterized in that, Including: S1: Extract the context code information of the code to be detected, where the context code information includes method code and class code; S2: Extract the key information of the method code and the class code respectively, and construct a method information set and a class information set; S3: Construct a large language model; S4: Based on the chain of thought and the tree of thought, construct the prompt words of the large language model; S5: Construct a hierarchical code replacement strategy; S6: According to the code replacement strategy, use the method information set and the class information set to dynamically adjust the context code information; S7: Input the prompt words and the dynamically adjusted context code information into the large language model for code smell detection, and output the code smell detection result of the code to be detected.
2. The method for detecting code smells based on a large language model according to claim 1, wherein The specific content of S1 includes: S101: Clone the target code library from the open source code repository to the local end; S102: Parse the target code library and construct a database corresponding to the target code library; S103: According to the name of the code to be detected, extract the context code information of the code to be detected from the database: Among them, CodeSet represents context code information, f extract () represents the context code information extraction function, R represents the open source code repository, M represents the name of the code to be detected, Method code represents the method code, and Class code represents the class code.
3. The method for detecting code smells based on a large language model according to claim 1, wherein Before S2 and after S1, it also includes: S1A: Perform code preprocessing on the context code information: CodeSet' = CodeSet \ Comments(CodeSet) \ Whitespace(CodeSet); Among them, CodeSet' represents the context code information after code preprocessing, CodeSet represents the context code information, \ represents the difference operator, Comments(CodeSet) represents the comment content in the context code information, and Whitespace(CodeSet) represents the whitespace characters in the context code information.
4. The method for detecting code smells based on a large language model according to claim 3, wherein The specific content of S1A includes: S1A01: Delete the comment content in the context code information; S1A02: Delete the whitespace characters in the context code information after deleting the comment content; S1A03: Standardize the context code information after deleting the whitespace characters.
5. The method for detecting code smells based on a large language model according to claim 1, wherein The method information set is specifically: Among them, MIS represents the method information set, Class Name represents the name of the class to which the code to be detected belongs, InvokedMethods represents the set of other codes invoked inside the code to be detected, Accessed Fields represents the set of fields accessed by the code to be detected, Instantiated Objects represents the set of types of new objects created by the code to be detected, MethodParameter Types represents the set of input parameter types of the code to be detected, and Return Type represents the type of the return value of the code to be detected; The class information set is specifically: Among them, CIS represents the set of class information, Fields represents the set of fields defined by the class, Methods represents the set of code defined by the class, Constructors represents the set of constructor code of the class, Interfaces represents the set of interfaces implemented by the class, and Superclass represents the name of the superclass inherited by the class.
6. The method for detecting code smells based on a large language model according to claim 1, characterized in that, The content of the chain of thought specifically includes: one role setting, one task description, one label definition, and one input clarification; The one role setting is specifically: setting the large language model as the first code analysis assistant for detecting code smells; The one task description is specifically: through the first code analysis assistant, gradually detect the interaction relationship between the context code information and the external class code to determine whether there is a code smell; if so, answer yes; otherwise, answer no; The one label definition is specifically: providing the first label for defining the code smell; The one input clarification is specifically: inputting the context code information into the large language model.
7. The method for detecting code smells based on a large language model according to claim 1, characterized in that, The content of the tree of thought specifically includes: two role settings, two task descriptions, two label definitions, and two input clarifications; The two role settings are specifically: setting the large language model as the second code analysis assistant composed of multiple experts; The two task descriptions are specifically: through the second code analysis assistant, comprehensively judge from the perspectives of each expert whether there is a code smell in the context code information; if so, answer yes; otherwise, answer no; The two label definitions are specifically: providing the second label for defining the code smell; The two input clarifications are specifically: inputting the context code information into the large language model.
8. The method for detecting code smells based on a large language model according to claim 1, wherein, The hierarchy includes the first level, the second level, and the third level; S6 specifically includes: S601: According to the code lengths of the method code and the class code, calculate the first comprehensive code length through addition operation; S602: Judge whether the first comprehensive code length is less than or equal to the preset code length of the large language model; if so, go to step S603; otherwise, go to step S604; S603: According to the first level, keep the method code and the class code unchanged to make a primary adjustment to the context code information: CodeSet1 = Method code + Class code L = L1; Among them, CodeSet1 represents the context code information after the primary adjustment, Method code represents the method code, Classcode represents the class code, L represents the level, and L1 represents the first level; S604: According to the second level, replace the class code with the set of class information to make a secondary adjustment to the context code information: CodeSet2 = Method code + CIS L = L2; Among them, CodeSet2 represents the context code information after the secondary adjustment, CIS represents the set of class information, and L2 represents the second level; S605: Calculate a second comprehensive code length through the addition operation according to the method code and the code lengths of the class information set. S606: Determine whether the second comprehensive code length is less than or equal to the preset code length of the large language model; if so, keep the context code information after the secondary adjustment unchanged; otherwise, proceed to step S607: S607: Replace the method code with the method information set according to the third level to perform a tertiary adjustment on the context code information: CodeSet3 = MIS + CIS L = L3; where CodeSet3 represents the context code information after the tertiary adjustment, and L3 represents the third level.
9. A code smell detection system based on a large language model, characterized in that Comprising: a processor and a memory; The memory stores a program or instructions that can be run on the processor, and when the program or instructions are executed by the processor, the steps of the large language model-based code smell detection method according to any one of claims 1 to 8 are implemented.
10. A readable storage medium, characterized in that, A program or instructions are stored on the readable storage medium, and when the program or instructions are executed by the processor, the steps of the large language model-based code smell detection method according to any one of claims 1 to 8 are implemented.
Citation Information
Cited By
Dockerfile peculiar smell iterative detection and restoration method and system based on large language model
CN120973691A
Code odor mixed detection method for CAE open source project
CN121918836A