Code quality detection method and device, equipment and storage medium
Through a systematic code quality detection method, the code to be optimized is obtained, the input of the test case is generated, and the problems of high cost and incomplete coverage of traditional code optimization methods are solved, and the code quality is improved and the error risk is reduced in the optimization process.
Patent Information
- Application Number
- CN202411787835.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-05
- Publication Date
- 2025-03-28
AI Technical Summary
Traditional code optimization methods rely on manual building unit tests, which are costly, long time and difficult to cover all possible code paths and boundaries, resulting in potential error risks.
Provide a code quality detection method, which obtains the code to be optimized, performs optimization processing, generates inputs for test cases, and tests the optimized code and the optimized code based on these inputs to ensure that the optimized code quality passes detection.
This method ensures that the optimized code is functionally consistent with the original code through a systematic process, and at the same time significantly improves the code quality, reduces the risk of errors introduced during the optimization process, and improves development efficiency and the reliability of software products.
Smart Images

Figure CN119718415A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of code optimization, and in particular to a code quality detection method, device, equipment and storage medium. Background Art
[0002] Code optimization is an important part of the R&D workflow, aiming to improve the performance of the code in multiple dimensions such as timeliness, security, and compliance. After the function development is completed, it is necessary to optimize the code in a targeted manner to ensure the high quality of the software product and user satisfaction.
[0003] Deep code optimization often comes with the risk of potential errors that may destroy the functionality of the code. Traditional error detection methods rely on manually constructed unit tests, but this method is costly, time-consuming, and difficult to cover all possible code paths and edge cases.
[0004] Therefore, it is an urgent need for technicians in this field to develop an efficient and accurate code quality detection method. Summary of the invention
[0005] In response to the above problems, this application provides a code quality detection method, including the following contents:
[0006] In a first aspect, the present application provides a code quality detection method, the method comprising:
[0007] Get the code to be optimized;
[0008] Optimizing the code to be optimized to obtain optimized code;
[0009] Generate input of test cases according to the code to be optimized;
[0010] Testing the code to be optimized and the optimized code respectively based on the input of the test case;
[0011] When the test result of the code to be optimized is the same as the test result of the optimized code, the quality of the optimized code passes the test.
[0012] Optionally, the optimizing the code to be optimized includes:
[0013] The original code is input into a generative model, and the original code is optimized for one or more of timeliness, security, and compliance to obtain optimized code.
[0014] Optionally, the input of generating a test case according to the code to be optimized includes:
[0015] The original code is input into the generative model to obtain inputs of n unit test cases based on the original code.
[0016] Optionally, when the test result of the code to be optimized is different from the test result of the optimized code, the method further includes:
[0017] The erroneous results and the correct output results of the original code segment are input into the generative model, and the generative model is required to avoid the erroneous results and regenerate an optimized new code segment with reference to the normal results.
[0018] Optionally, the generative model is trained based on a machine learning or deep learning algorithm and is used to optimize the code segment.
[0019] In a second aspect, the present application provides a code quality detection device, the device comprising:
[0020] An acquisition unit, used for acquiring the code to be optimized;
[0021] A first processing unit, configured to optimize the code to be optimized to obtain an optimized code;
[0022] A generating unit, used for generating an input of a test case according to the code to be optimized;
[0023] A testing unit is used to test the code to be optimized and the optimized code respectively based on the input of the test case; when the test result of the code to be optimized is the same as the test result of the optimized code, the quality of the optimized code passes the test.
[0024] Optionally, the first processing unit is specifically configured to:
[0025] The original code is input into a generative model, and the original code is optimized for one or more of timeliness, security, and compliance to obtain optimized code.
[0026] Optionally, the generating unit is specifically used for:
[0027] The original code is input into the generative model to obtain inputs of n unit test cases based on the original code.
[0028] Optionally, the device also includes a second processing unit, which is used to input the erroneous result and the correct output result of the original code segment into the generative model when the test result of the code to be optimized is different from the test result of the optimized code, and require the generative model to avoid the erroneous result and regenerate the optimized new code segment with reference to the normal result.
[0029] Optionally, the generative model is trained based on a machine learning or deep learning algorithm and is used to optimize the code.
[0030] In a third aspect, the present application provides a device, comprising a memory and a processor, wherein the memory is used to store instructions or codes, and the processor is used to execute the instructions or codes, so that the device executes the code quality detection method introduced in any implementation of the first aspect.
[0031] In a fourth aspect, the present application provides a computer-readable storage medium, in which a code is stored. When the code is executed, a device executing the code implements the code quality detection method introduced in any implementation of the first aspect.
[0032] The present application provides a code quality detection method. When executing the method, the code to be optimized is first obtained, the code to be optimized is optimized to obtain the optimized code, the input of the test case is generated according to the code to be optimized, and the code to be optimized and the optimized code are tested respectively based on the input of the test case. When the test result of the code to be optimized is the same as the test result of the optimized code, the quality of the optimized code passes the detection. The code quality detection method ensures that the optimized code is consistent with the original code in function through a systematic process, while significantly improving the code quality, reducing the risk of introducing errors during the optimization process, and improving development efficiency and the reliability of software products. BRIEF DESCRIPTION OF THE DRAWINGS
[0033] In order to more clearly illustrate the technical solutions in this embodiment or the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0034] Figure 1 A flowchart of a code quality detection method provided in an embodiment of the present application;
[0035] Figure 2 A schematic diagram of the structure of a code quality detection device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0036] As described in the background technology of this application, code optimization is a common R&D workflow. After the functional development of a code segment is completed, it is often necessary to optimize the code in terms of timeliness, security, compliance, and other dimensions. At the same time, the optimized code modification operation cannot destroy the correctness of the original code segment, that is, the optimized code must be consistent with the original code function. In this regard, this application proposes a code quality detection method to automatically perform code optimization detection work while ensuring the correctness and functionality of the code. The method includes:
[0037] Obtain the code to be optimized, optimize the code to be optimized to obtain the optimized code, generate the input of the test case according to the code to be optimized, test the code to be optimized and the optimized code respectively based on the input of the test case, and when the test result of the code to be optimized is the same as the test result of the optimized code, the quality of the optimized code passes the inspection.
[0038] In order to make the purpose, technical solution and advantages of the embodiments of the present application clearer, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0039] Figure 1 A flowchart of a code quality detection method provided in an embodiment of the present application. Figure 1 As shown, the code quality detection method provided in the embodiment of the present application may include:
[0040] S101, obtaining code to be optimized.
[0041] Code optimization is a common R&D workflow. After the functional development of a code segment is completed, it is often necessary to perform targeted optimization on dimensions such as code timeliness, security, and compliance. At the same time, the optimized code modification operations must not destroy the correctness of the original code segment, that is, the optimized code must be consistent with the original code function.
[0042] Code optimization: Code optimization usually includes timeliness, security, compliance and other aspects. Code timeliness optimization, such as rewriting the time-consuming "recursive algorithm implementation" into a time-saving "non-recursive algorithm implementation". Compliance optimization, such as rewriting the variables in the code into "camel case naming method" to comply with the company's development specifications.
[0043] In the code quality detection method of the embodiment of the present application, code optimization is first performed to obtain the code to be optimized. For example, the code segments to be optimized are obtained from the development environment, version control system (such as Git), or from other developers. These code segments may have completed the basic functional development, but may have deficiencies in performance, security, compliance, etc., and need to be further optimized.
[0044] S102: Optimize the code to be optimized to obtain optimized code.
[0045] After obtaining the code to be optimized, the code needs to be optimized, specifically by optimizing the code to be optimized through a generative model. The generative model is trained based on machine learning or deep learning algorithms and is used to optimize code segments. It can learn from input data and generate new data.
[0046] In one implementation of the embodiment of the present application, the original code is input into a generative model, and the original code is optimized for one or more of timeliness, security, and compliance to obtain an optimized code.
[0047] Timeliness optimization refers to improving the execution efficiency of code and reducing running time by improving algorithms, reducing unnecessary calculations, optimizing data structures, etc.
[0048] Security optimization refers to improving code security and preventing potential security threats by fixing security vulnerabilities, enhancing code security checks, and using safer programming practices.
[0049] Compliance optimization means ensuring that the code complies with specific coding specifications, industry standards, or legal and regulatory requirements. For example, modifying variable names to comply with company naming specifications, or ensuring that no banned APIs are used in the code.
[0050] During the optimization process, the generative model will intelligently analyze and modify the code to be optimized based on the rules and patterns learned internally to generate optimized code. This optimized code should retain the functionality of the original code while improving one or more specified aspects (timeliness, security, compliance).
[0051] S103: Generate input of a test case according to the code to be optimized.
[0052] The input of a test case refers to the data, conditions or operations provided to the system under test or application program when performing software testing, which are used to trigger specific behaviors or functions and verify whether the system works as expected. The input may include various forms of data and conditions, depending on the characteristics of the system under test and the test objectives. In an implementation of an embodiment of the present application, when the input of the test case is generated according to the code to be optimized, the original code is input into a generative model to obtain the input of n unit test cases based on the original code.
[0053] For example, there is a piece of original code, a Python function, which requires the generative model to generate inputs for n unit test cases based on this code segment.
[0054] Original code (Python function):
[0055] Python
[0056] def add_numbers(a,b):
[0057] return a+b
[0058] For this function, the generative model might generate the following n unit test cases as input:
[0059] Test case 1: Input: a=5, b=3, expected output: 8; Test case 2: Input: a=-2, b=-5, expected output: -7.
[0060] S104, testing the code to be optimized and the optimized code respectively based on the input of the test case, and when the test result of the code to be optimized is the same as the test result of the optimized code, the quality of the optimized code passes the test.
[0061] After obtaining the test case input in the above steps, use the same test case input to test the "code to be optimized" and "optimized code". That is, apply the same input data to the two versions of the code and observe their output and performance.
[0062] If the "code to be optimized" and the "optimized code" produce the same output under the same test case input, the two versions of the code are considered to be functionally equivalent. When it is confirmed that the optimized code is functionally equivalent to the original code and meets the optimization goals (such as performance improvement, resource consumption reduction, etc.), it can be considered that the quality of the optimized code has passed the test. This means that the optimization process has not introduced new errors or problems, and at the same time achieved the expected optimization effect.
[0063] The above process can ensure that code optimization will not destroy the original functions, and verify the optimization effect. By comparing the performance of the code before and after optimization under the same test cases, it can be ensured that the quality of the code is maintained or improved during the optimization process.
[0064] In the code optimization and verification process described above, when it is found that the test result of the code to be optimized is different from the test result of the optimized code, an additional step can be taken to further improve and optimize the code. When the test result of the code to be optimized is different from the test result of the optimized code, the method further includes: inputting the erroneous result and the correct output result of the original code segment into the generative model, requiring the generative model to avoid the erroneous result and regenerate the optimized new code segment with reference to the normal result.
[0065] Specifically, record the error results generated by the optimized code, and obtain or review the correct output results of the original code segment under the same test case. Provide the error results and the correct output results of the original code segment as input data to the generative model. The purpose of this step is to let the generative model understand the problems introduced during the optimization process and learn how to avoid these problems. Then instruct the generative model to avoid the previous errors based on the provided error results and correct outputs, and refer to the correct output to regenerate the optimized new code segment. Use the same test case to test the newly generated code segment. Make sure that the new code segment not only fixes the previous errors, but also still maintains the performance and other improvements after optimization. If the test results of the new code segment are still unsatisfactory, you can repeat the above process until you get satisfactory test results and optimization effects.
[0066] Through this process, the code can be gradually corrected and optimized without destroying the functionality of the original code segment until the expected optimization effect is achieved. This process emphasizes the important role of generative models in code optimization and error correction, as well as the method of continuously improving code quality through iteration and feedback.
[0067] The code quality detection method provided in the above embodiments of the present application ensures the effectiveness and security of the code optimization process through a systematic process, and its significant beneficial effects are mainly reflected in the following aspects:
[0068] Improve code quality: By optimizing the code to be optimized and strictly comparing the test results of the code before and after optimization, this method can ensure that the optimized code maintains the integrity of the original functions while further improving the quality of the code. This includes code readability, maintainability, performance and other aspects.
[0069] Reduce optimization risks: In the process of code optimization, the risk of introducing new errors or destroying original functions is objective. However, this method can timely discover and correct problems that may be introduced during the optimization process through comparative testing, thereby effectively reducing the risks brought by optimization.
[0070] Enhance code reliability:
[0071] With the help of test case input and comparative testing, this method can fully cover various possible execution paths and boundary conditions of the code, thereby enhancing the reliability and stability of the code. This helps to reduce possible abnormal situations in actual applications and improve user experience.
[0072] Promote continuous optimization: This method not only focuses on the effect of a single optimization, but also provides a solid foundation for continuous optimization through a repeatable testing process. Developers can continuously optimize the code based on the test results and gradually improve the overall quality of the code.
[0073] Improve development efficiency: By automatically generating test cases and comparing test results, this method can reduce the workload of manual inspection and verification, thereby improving development efficiency. At the same time, this also helps developers focus more on the implementation of code logic and functions, rather than spending a lot of time on testing and verification.
[0074] In summary, this code quality detection method effectively improves the quality, reliability and security of the code through systematic processes and strict testing standards, reduces optimization risks, promotes continuous optimization, and improves development efficiency. This is of great significance for ensuring the quality of software products and user satisfaction.
[0075] The above are some specific implementations of a code quality detection method provided in the embodiment of the present application. Based on this, the present application also provides a corresponding device. The device provided in the embodiment of the present application will be introduced from the perspective of functional modularization.
[0076] Figure 2 A schematic diagram of the structure of a code quality detection device provided in an embodiment of the present application. Figure 2 As shown, the code quality detection device 200 provided in the embodiment of the present application includes:
[0077] An acquisition unit 210 is used to acquire the code to be optimized;
[0078] A first processing unit 220 is used to optimize the code to be optimized to obtain an optimized code;
[0079] A generating unit 230, configured to generate an input of a test case according to the code to be optimized;
[0080] The testing unit 240 is used to test the code to be optimized and the optimized code respectively based on the input of the test case; when the test result of the code to be optimized is the same as the test result of the optimized code, the quality of the optimized code passes the test.
[0081] In an implementation manner of the embodiment of the present application, the first processing unit is specifically configured to:
[0082] The original code is input into a generative model, and the original code is optimized for one or more of timeliness, security, and compliance to obtain optimized code.
[0083] In one implementation of the embodiment of the present application, the generating unit is specifically used for:
[0084] The original code is input into the generative model to obtain inputs of n unit test cases based on the original code.
[0085] In one implementation of the embodiment of the present application, the device also includes a second processing unit, which is used to input the erroneous result and the correct output result of the original code segment into the generative model when the test result of the code to be optimized is different from the test result of the optimized code, and require the generative model to avoid the erroneous result and regenerate the optimized new code segment with reference to the normal result.
[0086] In one implementation of the embodiment of the present application, the generative model is trained based on a machine learning or deep learning algorithm and is used to optimize the code.
[0087] The embodiments of the present application also provide corresponding devices and computer storage media for implementing the solutions provided by the embodiments of the present application.
[0088] The device includes a memory and a processor, the memory is used to store instructions or codes, and the processor is used to execute the instructions or codes so that the device executes the method described in any embodiment of the present application.
[0089] The computer storage medium stores codes, and when the codes are executed, a device executing the codes implements the method described in any embodiment of the present application.
[0090] Through the description of the above implementation methods, it can be known that those skilled in the art can clearly understand that all or part of the steps in the above-mentioned embodiment method can be implemented by means of software plus a general hardware platform. Based on such an understanding, the technical solution of the present application can be embodied in the form of a software product, which can be stored in a storage medium, such as a read-only memory (ROM) / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network communication device such as a router) to execute the methods described in each embodiment of the present application or some parts of the embodiments.
[0091] It should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or device including the elements.
[0092] It should also be noted that the various embodiments in this specification are described in a progressive manner, and the same and similar parts between the various embodiments can refer to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the device and apparatus embodiments, since they are basically similar to the method embodiments, the description is relatively simple, and the relevant parts can refer to the partial description of the method embodiments. The device and apparatus embodiments described above are merely schematic, wherein the units described as separate components may or may not be physically separated, and the components indicated as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.
[0093] The above is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any changes or substitutions that can be easily thought of by a person skilled in the art within the technical scope disclosed in the present application should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.
Claims
1. A code quality detection method, characterized in that: The method comprises: Get the code to be optimized; Optimizing the code to be optimized to obtain optimized code; Generate input of test cases according to the code to be optimized; The code to be optimized and the optimized code are tested respectively based on the input of the test case; when the test result of the code to be optimized is the same as the test result of the optimized code, the quality of the optimized code passes the test.
2. The method according to claim 1, characterized in that The optimizing process of the code to be optimized comprises: The original code is input into a generative model, and the original code is optimized for one or more of timeliness, security, and compliance to obtain optimized code.
3. The method according to claim 1, characterized in that The input of generating a test case according to the code to be optimized includes: The original code is input into the generative model to obtain inputs of n unit test cases based on the original code.
4. The method according to claim 1, characterized in that: When the test result of the code to be optimized is different from the test result of the optimized code, the method further includes: The erroneous results and the correct output results of the original code segment are input into the generative model, and the generative model is required to avoid the erroneous results and regenerate an optimized new code segment with reference to the normal results.
5. The method according to any one of claims 2 to 4, characterized in that: The generative model is trained based on a machine learning or deep learning algorithm and is used to optimize the code.
6. A code quality detection device, characterized in that: The device comprises: An acquisition unit, used for acquiring the code to be optimized; A first processing unit, configured to optimize the code to be optimized to obtain an optimized code; A generating unit, used for generating an input of a test case according to the code to be optimized; A testing unit is used to test the code to be optimized and the optimized code respectively based on the input of the test case; when the test result of the code to be optimized is the same as the test result of the optimized code, the quality of the optimized code passes the test.
7. The device according to claim 6, characterized in that The first processing unit is specifically configured to: The original code is input into a generative model, and the original code is optimized for one or more of timeliness, security, and compliance to obtain optimized code.
8. The device according to claim 6, characterized in that The generating unit is specifically used for: The original code is input into the generative model to obtain inputs of n unit test cases based on the original code.
9. A computing device, characterized in that The computing device includes: a memory and a processor; The memory is used to store computer programs; The processor is configured to implement the method according to any one of claims 1 to 5 when executing the computer program.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method according to any one of claims 1 to 5 is implemented.