Code processing method and device, equipment, storage medium and program product
By identifying and deleting redundant logic switches in the code and utilizing reflection mechanisms and virtual machine traversal annotations, the problems of poor readability and storage space occupation caused by redundant logic switches in the code are solved, thereby improving code maintenance efficiency and readability.
Patent Information
- Application Number
- CN202510147249.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-02-10
- Publication Date
- 2025-09-23
AI Technical Summary
Redundant logic switches in code affect code readability, increase maintenance workload, occupy storage space, and interfere with search and analysis. Existing technologies make it difficult to quickly and accurately identify and remove them.
Through identification based on the first parameter, the code fragment using the first parameter is determined from the code, the description information is run to identify the conditions that are met, and redundant logic switches are deleted. The reflection mechanism and virtual machine traversal annotations are used to accurately identify and clear redundant logic switches in combination with the actual operating conditions.
It achieves the rapid and accurate removal of redundant logic switches in the code, improves code readability, reduces maintenance workload and storage space usage, and avoids interference with search and analysis.
Smart Images

Figure CN120687094A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of software compilation technology, and in particular to a code processing method, apparatus, device, storage medium, and program product. Background Art
[0002] A logical switch in code is an abstract term used in programming to describe a mechanism that controls the flow of code execution through conditional judgments. Simply put, it's like a real-life switch that opens or closes a circuit, determining whether an appliance operates. In programming, when there are multiple logical branches within a code, a logical switch is used to determine whether certain branches are executed. Specifically in programming languages, logical switches are typically implemented using conditional statements (such as if statements and switch statements).
[0003] In some scenarios, such as when a new requirement is successfully launched, the business system runs stably, and the business functions meet expectations, the logical switches used to control the logical branches corresponding to the new and old requirements will not be executed but will still remain in the code, appearing redundant, thereby affecting the readability and comprehension of the code, interfering with code search and analysis, increasing the workload of code maintenance, and occupying storage space.
[0004] Therefore, it is necessary to clean up redundant logic switches in the code to solve the above drawbacks and maintain the health of the code and the sustainable development of the business system. Summary of the Invention
[0005] Embodiments of the present application provide a code processing method, apparatus, device, storage medium, and program product for quickly, accurately, and comprehensively deleting redundant logic switches in code.
[0006] The embodiments of this application adopt the following technical solutions: In a first aspect, an embodiment of the present application provides a code processing method, including: Based on the identifier of the first parameter, determining, from the first code, a first code segment that uses the first parameter, where the first code segment includes description information of the first parameter, where the description information is used to describe values of the first parameter under different conditions; Determine, by running the first code, a first condition satisfied by the first parameter from the description information; Based on a first value of the first parameter under the first condition, the first parameter is deleted from the first code segment to obtain a second code.
[0007] In a second aspect, an embodiment of the present application provides a code processing device, including: a determining module, configured to determine, based on an identifier of a first parameter, from a first code, a first code segment that uses the first parameter, wherein the first code segment includes description information of the first parameter, the description information being used to describe values of the first parameter under different conditions; The determining module is further configured to determine a first condition satisfied by the first parameter from the description information by running the first code; A deletion module is configured to delete the first parameter from the first code segment based on a first value of the first parameter under the first condition, to obtain a second code.
[0008] In a third aspect, an embodiment of the present application provides a computing device, comprising: a memory and a processor, wherein: The memory is used to store computer programs; The processor is coupled to the memory and is configured to execute the computer program stored in the memory to perform the method provided by the first aspect.
[0009] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium storing a computer program, which, when executed by a computer, can implement the method provided in the first aspect.
[0010] In a fifth aspect, an embodiment of the present application provides a computer program product, which stores instructions. When the instructions are executed by a computer, the computer implements the method provided in the first aspect.
[0011] At least one of the above technical solutions provided in the embodiments of the present application can achieve the following beneficial effects: The first parameter is usually used as a logic switch in the code, and the description information of the first parameter is used to describe the value of the first parameter under different conditions. These conditions and values are used to control the logic branch to be executed in the code; and the first parameter has a corresponding identifier. Based on this, based on the identification of the first parameter, the first code fragment using the first parameter can be quickly, accurately and comprehensively determined from the first code. This type of code fragment may contain redundant logical switches. On this basis, by running the first code, the first condition satisfied by the first parameter is determined from the description information. The logical branch corresponding to the first value of the first parameter under the first condition is the logical branch executed during the actual operation of the first code. The other logical branches are redundant logical branches. Therefore, the logical switch represented by the first parameter is no longer useful and is a redundant logical switch. Therefore, this method of identifying and deleting redundant logical switches in combination with the actual operation status of the code can accurately discover logical switches that are hidden deeply, called conditionally or used indirectly, and avoid omissions. Furthermore, by deleting the first parameter from the first code fragment based on the first value, the redundant logical switches in the code can be quickly, accurately and comprehensively removed, thereby improving the readability and comprehensibility of the code, avoiding redundant logical switches from interfering with code search and analysis, reducing the workload of code maintenance and the storage space occupied by the code, etc. BRIEF DESCRIPTION OF THE DRAWINGS
[0012] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings: Figure 1 A flowchart of a specific implementation of a code processing method provided in an embodiment of the present application; Figure 2 A schematic diagram of a specific implementation process of applying the code processing method provided in an embodiment of the present application to an actual scenario; Figure 3 A schematic diagram of the specific structure of a code processing device provided in an embodiment of the present application; Figure 4 A schematic diagram of the specific structure of a computing device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0013] To make the purpose, technical solutions, and advantages of this application more clear, the technical solutions of this application will be clearly and completely described below in conjunction with the specific embodiments of this application and the corresponding drawings. Obviously, the embodiments described are only part of the embodiments of this application, not all of them. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of this application.
[0014] Those skilled in the art will appreciate that, with the development of technology and the emergence of new scenarios, the technical solutions provided in the embodiments of this application are also applicable to similar technical problems.
[0015] The terms "first", "second", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequential order. It should be understood that the terms used in this way can be interchangeable under appropriate circumstances, and this is merely a way of distinguishing the objects of the same attributes when describing them in the embodiments of the present application. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, so that the process, method, system, product or equipment comprising a series of units need not be limited to those units, but may include other units that are not clearly listed or inherent to these processes, methods, products or equipment.
[0016] Some concept descriptions: The Java Virtual Machine (JVM) is a virtual machine specifically designed to execute Java bytecode. The JVM provides a standard runtime environment for Java programs, converting compiled bytecode into machine instructions for execution on a specific hardware platform.
[0017] As mentioned earlier, logical switches in code are often used to describe a mechanism that controls the flow of code execution through conditional judgments. Logical switches can be applied in a wide variety of scenarios. For example, in software upgrades, due to uncertainty about whether the new version of the software will run smoothly, a logical switch is often set within the software code. This switch determines whether to execute the logic branch of the new or old version of the software. For example, two if statements can be set to implement a logical switch, thereby controlling the switching execution of code snippets. For example, if the "if=condition 1" conditional statement holds, the logic branch of the new version of the software is executed; if the "if=condition 2" conditional statement holds, the logic branch of the old version of the software is executed. Using logical switches to control the switching execution of logical switches can avoid directly upgrading a previously stable old version of software to an unstable new version, which can lead to unstable software system operation and difficulty in resolving the instability.
[0018] Theoretically, once the new version of the software runs stably, the logic switches and their associated code that implement the above functions will not be executed and will not be discovered by common code development tools (such as Eclipse and IntelliJ IDEA). As a result, these logic switches and their associated code remain in the code, appearing redundant. Although these redundant logic switches do not directly affect software operation, they have several drawbacks: (1) Reduced readability. Redundant logic switches make the code lengthy and cluttered. New developers or maintainers need to spend more time and energy to distinguish valid code from invalid code when reading the code, which reduces the readability and comprehension of the code.
[0019] (2) Interference with code search and analysis. When using search tools such as grep or the search function of the Integrated Development Environment (IDE), redundant logic switches may appear in the search results, interfering with developers' error location and analysis of related functions.
[0020] (3) Increased workload for code maintenance. Due to the existence of redundant logic switches, developers need to be extra careful when making modifications or adding new features to avoid accidentally touching such logic switches, which undoubtedly increases the complexity and risk of maintenance work.
[0021] (4) Occupied storage space. Although the space occupied by a single redundant logic switch is negligible, a large number of them accumulated may also affect the project file size, especially in large projects.
[0022] Therefore, it is necessary to identify and clean up redundant logic switches in the code to maintain the health of the code and the sustainable development of the project.
[0023] To this end, an embodiment of the present application proposes a code processing method, in which a first parameter is generally used as a logical switch in the code, and the description information of the first parameter is used to describe the value of the first parameter under different conditions. These conditions and values are used to control the logical branches to be executed in the code; and the first parameter has a corresponding identifier. Based on this, based on the identification of the first parameter, the first code fragment using the first parameter can be quickly, accurately and comprehensively determined from the first code. This type of code fragment may contain redundant logical switches. On this basis, by running the first code, the first condition satisfied by the first parameter is determined from the description information. The logical branch corresponding to the first value of the first parameter under the first condition is the logical branch executed during the actual operation of the first code. The other logical branches are redundant logical branches. Therefore, the logical switch represented by the first parameter is no longer useful and is a redundant logical switch. Therefore, this method of identifying and deleting redundant logical switches in combination with the actual operation status of the code can accurately discover logical switches that are hidden deeply, called conditionally or used indirectly, and avoid omissions. Furthermore, by deleting the first parameter from the first code fragment based on the first value, the redundant logical switches in the code can be quickly, accurately and comprehensively removed, thereby improving the readability and comprehensibility of the code, avoiding redundant logical switches from interfering with code search and analysis, reducing the workload of code maintenance and the storage space occupied by the code, etc.
[0024] It should be understood that the code processing method provided in the embodiments of the present application can be executed by a computing device, specifically by a processor of the computing device. The so-called computing device herein may include a terminal, such as but not limited to a smartphone, tablet computer, laptop computer, desktop computer, intelligent voice interaction device, smart home appliance, smart watch, vehicle-mounted terminal, aircraft, etc.; or the computing device may also include a server, such as an independent physical server, a server cluster or distributed system composed of multiple physical servers, or a cloud server providing cloud computing services.
[0025] The different steps of the method may be implemented by the same execution entity or by different execution entities. The embodiments of the present application do not limit the execution entity used to implement the method.
[0026] In addition, the embodiment of the present application does not limit the execution order of different steps. When using the method provided in the embodiment of the present application, the execution order of different steps can be adjusted according to actual needs.
[0027] The following describes in detail the technical solutions provided by various embodiments of the present application in conjunction with the accompanying drawings.
[0028] Please refer to Figure 1 , is a flowchart of a code processing method provided in one embodiment of the present application, the method comprising the following steps: S102: Determine, based on the identifier of the first parameter, from the first code a first code segment using the first parameter.
[0029] The first code can be any code to be processed. The first parameter refers to a parameter used as a logical switch in the code. The first code includes multiple code snippets. The first code snippet includes description information of the first parameter, which is used to describe the values of the first parameter under different conditions. These conditions and values are used to control the logical branches to be executed in the code. In addition, the first code snippet may also include logical branches corresponding to different values of the first parameter.
[0030] As an example, the description information of the first parameter includes the following fields: code: Represents the name of the first parameter. This unique identifier serves as the sole basis for distinguishing different first parameters, essentially acting as an identifier for the first parameter. This field is highly versatile and can be shared not only by multiple code within a single application but also across multiple applications, being shared by different code within those applications. This field is formatted as a single value and cannot be empty.
[0031] expression: This field represents the condition that the first parameter satisfies. The value of this field can be a computable boolean expression, and the operation result of this boolean expression is the value of the first parameter. The value of the first parameter can be limited to true or false, or can also be limited to 1 or 0. For example, the value of expression can be a response judgment condition of a HyperText Transfer Protocol (HTTP) interface, a judgment condition for date comparison, calling a certain method to return a boolean value, etc. In terms of format, the value of this field is a single value and is not empty.
[0032] branch: This field represents the logical branch corresponding to the value of the first parameter under different conditions. In terms of format, the value of this field is a multi-value and is not empty.
[0033] For example, the following shows three types of description information of the first parameter: Description information 1: code = "codeA", expression = "now() <2024060>1", branch = "1" Description information 2: code = "codeA", expression = "Objects.isNull(methodA()) ", branch = "12" Description information 3: code = "codeB", expression = "methodB()", branch = "true" The above description information 1 is the description information of the first parameter named codeA, which means that when the first parameter satisfies the condition that "the current date is earlier than June 1, 2024", execute the logical branch named 1; the above description information 2 is the description information of the first parameter named codeA, which means that when the first parameter satisfies the condition that "the result returned by calling the method methodA() is null", execute the logical branch named 12; the above description information 3 is the description information of the first parameter named codeB, which means that when the first parameter satisfies the condition that "the result returned by calling the method methodB() is true", execute the logical branch named true.
[0034] The first parameter has a corresponding identifier. As an example, the identifier of the first parameter includes a first identifier. The first identifier is used to distinguish the first parameter from other parameters. The first identifier can be set according to actual needs, and the embodiments of this application do not limit this. For example, the first identifier can be LogicSwitch.
[0035] Based on this, the above S102 includes the following steps: searching for a code segment containing a first identifier in the first code as the first code segment using the first parameter.
[0036] As an example, the first code can be loaded into a virtual machine, such as a JVM, and the virtual machine traverses the annotations of the first code through a reflection mechanism to find out whether the first identifier exists in the traversed annotations; if the first identifier exists in the traversed annotations, the code snippet to which the annotation belongs is determined to be the first code snippet using the first parameter.
[0037] For example, the following Table 1 is a code snippet of the first code. Since the code snippet uses the annotation @LogicSwitch containing the first identifier LogicSwitch, the code snippet is determined to be the first code snippet.
[0038] Table 1
[0039] Table 2 below shows another code snippet of the first code. Since this code snippet also uses the annotation @LogicSwitch containing the first identifier LogicSwitch, this code snippet is also determined to be the first code snippet.
[0040] Table 2
[0041] It can be understood that after the first code is loaded into the virtual machine, the information in the first code is stored in the data structure inside the virtual machine, which provides convenience for subsequent operations; reflection is a powerful mechanism provided by the Java language, which allows the code to check and operate the class files, methods, variables, etc. in the first code at runtime. Through the reflection mechanism, the virtual machine can easily traverse the class files, methods, variables and other parts in the first code to find out whether there is an annotation containing the first identifier, so as to accurately identify the code fragment to which such annotation belongs as the first code fragment.
[0042] As another example, the identifier of the first parameter may further include a second identifier and a third identifier, wherein the second identifier is used to identify a class file that uses the first parameter, and the third identifier is used to identify a method that uses the first parameter. For example, the second identifier may be ClassLogicSwitch, and the third identifier may be MethodLogicSwitch.
[0043] In this case, the first code is loaded into a virtual machine for execution; during the execution of the first code, a first type of file containing a first annotation is scanned, and the first annotation contains a second identifier; in the first type of file, a first method containing a second annotation is scanned, and the second annotation contains a third identifier; in the first method, a third annotation containing the first identifier is scanned; and the code snippet to which the third annotation belongs is determined as the first code snippet.
[0044] Specifically, as described above, after the first code is loaded into the virtual machine and executed, the virtual machine can conveniently traverse the class files in the first code to find out whether there is a first annotation containing the second identifier. If the first annotation is found, it is determined that there is a code snippet using the first parameter in the first class file to which the first annotation belongs; further, the virtual machine traverses the methods in the first class file to find out whether there is a second annotation containing the third identifier. If the second annotation is found, it is determined that there is a code snippet using the first parameter in the first method to which the second annotation belongs; further, the virtual machine traverses the first step method to find out whether there is a third annotation containing the first identifier. If the third annotation is found, it is determined that the first parameter is used in the code snippet to which the third annotation belongs, thereby obtaining the first code snippet.
[0045] In the above method, since the virtual machine can use the reflection mechanism to obtain annotations in the code, a three-level positioning strategy is adopted using the first identifier, the second identifier and the third identifier. First, the first annotation containing the second identifier is used to locate the first type of file that uses the first parameter, and then the second annotation containing the third identifier is used to locate the first method that uses the first parameter from the first type of file. Finally, the third annotation containing the first identifier is used to locate the first code fragment that uses the first parameter from the first method. This can ensure the accuracy and comprehensiveness of the positioning of the first code fragment, avoid omissions, and lay the foundation for the subsequent accurate and comprehensive deletion of redundant logical switches and their corresponding code content in the first code.
[0046] The above describes some implementation methods of the above S102. Of course, it should be understood that the above S102 can also be implemented in other ways, and the present embodiment of the application does not limit this.
[0047] S104: Determine a first condition satisfied by the first parameter from the description information of the first parameter by running the first code.
[0048] Specifically, the first code can be loaded into a virtual machine for execution, and the first condition satisfied by the first parameter during the execution of the first code can be monitored. The condition reflects the actual execution result of the first code fragment. That is, if the first parameter cannot satisfy the first condition during the execution of the first code, it indicates that the first condition is redundant.
[0049] For example, taking the description information 1 shown above as an example, if the current time is May 1, 2024, which is earlier than June 1, 2024, that is, during the execution of the first code, the condition described by expression is met, and then the condition is determined as the first condition.
[0050] For another example, taking the description information 2 shown above as an example, if methodA() has been deleted and cannot be called, the condition described by expression is met, and this condition is then determined as the first condition.
[0051] For another example, taking the description information 3 shown above as an example, if the result returned by methodB() after being called is true, the condition described by expression is met, and this condition is then determined as the first condition.
[0052] S106 , based on the first value of the first parameter under the first condition, delete the first parameter from the first code segment to obtain a second code.
[0053] Since the first parameter satisfies the first condition during the execution of the first code, the logic branch corresponding to the first value of the first parameter under the first condition is the logic branch executed during the actual execution of the first code, and the remaining logic branches are redundant logic branches. It can be determined that the logic switch represented by the first parameter is no longer useful and is a redundant logic switch, and the first parameter can be deleted from the first code.
[0054] In one embodiment, description information is used in the first code segment to describe the first parameter. If the first parameter is redundant, the description information of the first parameter is also redundant. Therefore, the description information of the first parameter can be deleted to obtain the second code.
[0055] For example, taking the first code snippet shown in Table 1 above as an example, the description information "code="codeA", expression=true, branch={false}" in the code snippet can be deleted.
[0056] In another embodiment, other logic branches except the first logic branch may be deleted from the first code segment to obtain a second code.
[0057] For example, still taking the first code snippet shown in Table 1 above as an example, the first condition is expression=true, and the value of the first parameter codeA under the first condition corresponds to the logic branch else and the processing operation 2 it contains, and then other logic branches except the logic branch are deleted from the first code snippet.
[0058] In another embodiment, considering that only deleting the description information of the first parameter may cause the first code snippet to fail to run, and the logical branches corresponding to the values under conditions other than the first condition in the first code snippet will not be executed, which is also redundant content and also causes many disadvantages in the code. Therefore, the above S106 includes the following steps: S161: Based on the first logical branch corresponding to the first value, determine a second logical branch from the description information of the first parameter.
[0059] Specifically, the logic branches other than the first logic branch in the description information can be determined as second logic branches. The second logic branches are logic branches that will not be executed during the running of the first code and are redundant logic branches.
[0060] For example, taking the first code snippet shown in Table 1 above as an example, if expression=true holds, the first value is false, and the first logical branch corresponding to this value is as follows: else{ / / Execute processing operation 2 } The second logical branch can then be determined as follows: if(flag){ / / Execute processing operation 1 } For another example, taking the first code snippet shown in Table 2 above as an example, if expression="methodC()" holds, the first value is false, and the first logical branch corresponding to the first value is as follows: case 1; / / Execute processing operation 1' break; default / / Execute processing operation 3' breake; The second logical branch can then be determined as follows: case 2; / / Execute processing operation 2' break; S162: Delete the description information of the first parameter and the second logic branch from the first code segment to obtain a third code.
[0061] As an example, the description information of the first parameter and the first logic branch may be directly deleted from the first code segment.
[0062] As another example, a logic branch includes a first statement describing the value of a first parameter and a second statement describing the processing operation corresponding to the first parameter. In this case, S162 includes the following steps: deleting the description of the first parameter and the second logic branch from the first code segment, and deleting the first statement in the first logic branch, to obtain a third code segment.
[0063] For example, continuing to take the first code snippet shown in Table 1 above as an example, the first code snippet includes two logical branches. The first logical branch is the logical branch when expression=true is true, which includes the first statement "else{}" and the second statement describing processing operation 2; the other logical branch is the logical branch when expression=true is not true, which includes the first statement "if(flag){}" and the second statement describing processing operation 1. Since expression=true is true, the logical branch when expression=true is true is the first logical branch, and the logical branch when expression=true is not true is the second logical branch. Based on this, the description information of the first parameter, the second logical branch, and the first statement in the first logical branch are deleted, and the third code obtained is shown in Table 3 below: Table 3
[0064] For another example, continuing with the first code snippet shown in Table 2 above, the first code snippet includes three logical branches. The first logical branch is a logical branch when expression = "methodC()" is true, and its first statement is "case 1...break" and the second statement describing processing operation 1'; the second logical branch is another logical branch when expression = "methodC()" is true, and its first statement is "default...break" and the second statement describing processing operation 3'; the third logical branch is a logical branch when expression = true is not true, and it includes the first statement "case 2...break" and the second statement describing processing operation 2'. Since expression = "methodC()" is true, the logical branch when expression = "methodC()" is true is the first logical branch, and the remaining logical branches are the second logical branches. Based on this, the description information of the first parameter, the second logical branch, and the first statement in the first logical branch are deleted, and the third code obtained is shown in Table 4 below: Table 4
[0065] S163: If the third code passes the verification, the third code is determined as the second code.
[0066] After obtaining the third code, the third code is verified to ensure that the modification to the first code has not introduced new errors and to ensure that the modified third code operates as expected. If the third code passes verification, the modification to the first code is determined to be successful, and the third code is then determined to be the second code. If the third code fails verification, the first code is modified again and verified again until the modified first code passes verification.
[0067] Through the above implementation, all redundant code contents related to the first parameter in the first code can be deleted quickly, accurately and comprehensively, and the normal operation of the obtained second code can be ensured.
[0068] In the embodiment of the present application, the third code can be verified by various appropriate methods, such as but not limited to: unit testing, integration testing, system testing, regression testing, acceptance testing, performance testing, security testing, static testing, dynamic testing, etc.
[0069] In one embodiment, verifying the third code includes the following steps: Step A1: Generate a test task for testing the third code.
[0070] Add the parameters required for the test to the corresponding task template to obtain the corresponding test task. For example, if the test task is to perform regression testing on the third code, then add the parameters required for the test, such as test environment parameters, to the task template corresponding to the regression test to obtain the test task.
[0071] Step A2: Run the test task to perform regression testing on the third code.
[0072] Regression testing of the third code involves a series of tests to ensure that the third code does not disrupt the original business functions. The purpose is to verify whether the original business functions can operate normally. For example, the business functions that can be implemented by the first code include: product display, adding products to the shopping cart, settlement and payment, etc. After modifying the first code to obtain the third code, such as modifying the first code fragment corresponding to the payment interface using the first parameter or the discount calculation method, regression testing of the third code includes checking whether the third code can display products normally, whether users can add products to the shopping cart, and whether the settlement and payment functions are normal.
[0073] In step A3, if the third code passes the regression test, it is determined that the third code passes the verification.
[0074] By performing regression testing on the third code, it can be ensured that the modified third code does not disrupt the business functions of the cause.
[0075] The above shows some implementation manners of the above S106. Of course, it should be understood that the above S106 can also be implemented in other ways, and the embodiments of the present application do not limit this.
[0076] In the embodiments of the present application, the number of the first code segments can be multiple, that is, the same first parameter is used in different first code segments. At this time, if the logical branches controlled by these first parameters are contradictory to each other, for example, a contradictory situation where the same first parameter can be both deleted and not deleted when used in different positions of the first code, it will affect the consistency of the logical decision-making, thereby affecting the accuracy and unity of the logical processing.
[0077] Therefore, in another embodiment of the present application, before the above S106, the code processing method provided by the embodiments of the present application further includes: comparing the first values corresponding to the first conditions in the description information included in each first code segment. Correspondingly, in the above S106, if the first values corresponding to the first conditions in the description information included in each first code segment are the same, then based on the first values corresponding to the first conditions in the description information included in each code segment, the first parameter is deleted from each first code segment to obtain the second code.
[0078] For example, assume that the first code includes two first code segments, and both of these first code segments use the first parameter codeA. The description information of the first parameter in the first first code segment is as follows: code= "codeA",expression="now()<2024060>1",branch="1" The description information of the first parameter in the second first code segment is as follows: code= "codeA",expression="Objects.isNull(methodA())",branch="12" Assume that the current time is May 1, 2024. Then in the first first code segment, the expression "now()<2024060>1" in the description information of the first parameter codeA holds, and the first value corresponding to the first parameter codeA under this first condition is true.
[0079] In the second first code snippet, expression="Objects.isNull(methodA())" in the description information of the first parameter codeA holds true, and the first value of the first parameter codeA corresponding to the first condition is true.
[0080] Since the first parameter codeA is not contradictory in the two first code fragments, the first parameter codeA can be deleted from the two first code fragments based on the first value true of the first parameter codeA.
[0081] In another embodiment of the present application, after the above S106, the code processing method provided in the embodiment of the present application may further include: deleting the first parameter from a parameter library, wherein the parameter library is used to manage the first parameter used in the first code.
[0082] The parameter library is a unified tool for managing the parameters of all described logical switches. Failure to delete redundant first parameters in the parameter library not only affects the library's readability and understanding, interferes with parameter search and analysis, increases the workload of parameter library maintenance, occupies storage space, but also affects code execution. Therefore, deleting the first parameter in the parameter library effectively addresses these drawbacks, allowing the parameter library to better manage the parameters of described logical switches in the code.
[0083] In the code processing method provided by one or more embodiments of the present application, a first parameter is generally used as a logical switch in the code, and description information of the first parameter is used to describe the value of the first parameter under different conditions. These conditions and values are used to control the logical branches to be executed in the code; and the first parameter has a corresponding identifier. Based on this, based on the identification of the first parameter, the first code fragment using the first parameter can be quickly, accurately and comprehensively determined from the first code. This type of code fragment may contain redundant logical switches. On this basis, by running the first code, the first condition satisfied by the first parameter is determined from the description information. The logical branch corresponding to the first value of the first parameter under the first condition is the logical branch executed during the actual operation of the first code. The other logical branches are redundant logical branches. Therefore, the logical switch represented by the first parameter is no longer useful and is a redundant logical switch. Therefore, this method of identifying and deleting redundant logical switches in combination with the actual operation status of the code can accurately discover logical switches that are hidden deeply, called conditionally or used indirectly, and avoid omissions. Furthermore, by deleting the first parameter from the first code fragment based on the first value, the redundant logical switches in the code can be quickly, accurately and comprehensively removed, thereby improving the readability and comprehensibility of the code, avoiding redundant logical switches from interfering with code search and analysis, reducing the workload of code maintenance and the storage space occupied by the code, etc.
[0084] In order to facilitate the understanding of the above code processing method, the following Figure 2 , a specific implementation process of applying the code processing method provided in an embodiment of the present application to an actual scenario is described.
[0085] like Figure 2 The following is a schematic diagram of the specific implementation process, which includes the following main implementation stages: Phase 1: Application Acquisition In the first stage, the computing device acquires various applications in a company's subsystems - such as but not limited to: middleware systems, payment systems, order systems, and financial systems.
[0086] The subsystem mentioned here may be, for example, a collection of applications other than system software in a computer software system of an enterprise.
[0087] Phase 2: Class file loading and scanning The goal of the second stage is to find the first identifier, the second identifier, and the third identifier of the first parameter from the class file of the application code. These three identifiers are set in the class file by the application developer.
[0088] In the programming world, a class file typically refers to a file containing class definitions. For example, in the Java language, a class file typically has a ".java" extension and contains the definition of one or more classes, each of which defines the properties and methods of an object. After compilation, these class files are converted into bytecode files with a ".class" extension, which can be run on the Java Virtual Machine. Class files do not contain any delimiters, and their individual data items are compactly arranged in a strictly specified order and quantity. The meaning, length, and order of each byte cannot be changed to ensure correct interpretation and execution by the Java Virtual Machine.
[0089] like Figure 2 As shown, in the second stage, the following sub-steps 1 to 4 are mainly performed: Sub-step 1: First, the computing device loads each class file in the acquired application code into the virtual machine; Sub-step 2: The virtual machine then scans each class file separately. Specifically, the virtual machine first scans the file identifiers of the class files to determine whether the first annotation containing the second identifier exists in each class. For class files whose judgment is yes, it can be determined that the first parameter is used in the class file, and then sub-step 3 can be executed. For class files whose judgment is no, the process can be terminated. For example, the computing device may input the use of the second identifier (such as ClassLogicSwitch) into the virtual machine; thus, once the virtual machine scans and finds that the annotation of a class file contains the second identifier, it indicates that the first parameter is used in the class file.
[0090] Sub-step 3: For the class file that uses the first parameter, the virtual machine further scans the methods defined in the class file to determine whether the second annotation containing the third identifier exists in each method; if the method is judged to have the second annotation, it can be determined that the first parameter is used in the method, and then sub-step 4 can be executed; if the method is judged to have the second annotation, the process can be terminated; For example, the computing device may input the use of a third identifier (such as MethodLogicSwitch) into the virtual machine; thus, once the virtual machine scans and finds that the annotation of a method contains the third identifier, it indicates that the first parameter is used in the class.
[0091] It's important to note that class files and methods are two fundamental concepts in object-oriented programming, each with distinct responsibilities. A class file is an abstract data type that defines a set of properties and methods used to describe objects with the same characteristics and behaviors. Methods, on the other hand, are functions defined within a class file that implement specific behaviors. Each method has a name, a parameter list, and a return type (if any). Methods can access member variables and other methods of the class file, thereby implementing complex logic and functionality.
[0092] Sub-step 4: For the method using the first parameter, the virtual machine further scans the code snippets of the method to determine whether the second annotation containing the first identifier exists in each code snippet; if the code snippet is judged to have the second annotation, it can be determined that the first parameter is used in the code snippet, and the code snippet is further determined as the first code snippet; if the code snippet is judged to have the second annotation not included in the second annotation, the process can be terminated; It should be noted that the first, second, and third identifiers mentioned above can be local variables, also known as internal variables. These are variables defined within a method (or function), specifically within a method or compound statement. The lifetime of a local variable is calculated from the moment the method is called until the method returns to the calling point. In other words, a local variable is created when the method is called and destroyed after the method completes execution.
[0093] The third stage: Summarize the description information of all the first parameters in the first code snippet of the subsystem and process it In the third stage, for each application, the computing device mainly performs the following sub-steps I and II: Sub-step I: all first parameters may be classified according to the name (code) of each first parameter; For example, assume that the obtained first parameter and its description information are as follows: LogicSwitch(code= "codeA",expression="now()<2024060>1", branch="1") LogicSwitch(code= "codeA",expression="now()<2024070>1", branch="12") LogicSwitch(code= "codeB",expression="methodA()", branch="true") Then, group by code, that is, divide the description information belonging to the first parameter codeA into one group, and divide the description information belonging to the first parameter codeB into another group.
[0094] Sub-step II: For each group obtained by grouping the description information, obtain the execution result of the expression contained in each description information in each group one by one.
[0095] Among them, the forms of the expressions contained in the description information in the same group may be diverse. For example, as follows: now()<2024060>1: The current time is less than June 1, 2024 Objects.isNull(methodA()): The execution result of method methodA is null methodB(): The return result of method methodB (the return result is of boolean type) The fourth stage: Execute the expression and rewrite the code logic Taking a certain group as an example, in the fourth stage, the following sub-steps are mainly executed: Sub-step a: Execute each expression contained in the description information in this group to obtain the execution result of each expression; if "false" exists in the execution result, it is determined that the logic switch cannot be deleted from the application, and the process ends; if the execution results are all "true", then execute sub-step b; Sub-step b: On the one hand, a snapshot is taken of the class files belonging to the group—that is, a backup of the class files at the current time—in order to add the snapshot to the operation log for the class files, thereby facilitating subsequent review and code optimization of the class files before code rewriting based on the snapshot in the operation log. On the other hand, a code logic rewrite operation is performed—that is, the code of the application to which the class files belong is rewritten. Rewriting the application code means deleting the logic branches where the execution results of the expressions contained in the description information of the group are "false".
[0096] Sub-step c: Submit a merge request to the code management system GIT; This request is used to request GIT to incorporate the rewritten application into the subsystem as part of the subsystem.
[0097] Among them, GIT is an open source distributed version control system that can effectively and quickly handle version management of projects from very small to very large.
[0098] Sub-step d: The code management system GIT reviews the merge request; if approved, proceed to sub-step e; if not, roll back the rewritten application to the pre-modified state; further, record information about the rollback operation in the operation log for this type of file, and the process ends. Sub-step e: Create tester regression tasks; Since the code has been changed, in order to ensure that the original function is normal, it is necessary to revert to the original function. Therefore, GIT can connect to a third-party system and trigger the third-party system to create a tester regression task to remind the tester to revert the function in time.
[0099] Sub-step f: The tester verifies whether the application functions properly after the code rewrite. If so, the tester deletes the code in the group (e.g., code A) from the switch library of the application's class file. The tester then records the deletion information in the operation log for the class file, and the process ends. If not, the tester proceeds to sub-step g. Sub-step g: The developer modifies the code of the rewritten application and then re-executes sub-step f.
[0100] Throughout the software development lifecycle, from initial code adjustments, code reviews, and merging into the main branch, to comprehensive functional testing and verification, and finally to the identification and removal of no longer needed logic switches and redundant code, this consistent process builds a closed-loop quality control system.
[0101] Experiments have shown that the solution provided by the embodiment of this application can significantly improve the clarity and maintainability of the target code after being applied to the quality control system. More specific effects include: 1. Enhance code readability: By removing outdated logic branches, the code becomes more concise and clear, making it easier for new and experienced developers to quickly understand business logic and accelerate team collaboration efficiency.
[0102] 2. Reduce maintenance costs: Reducing the amount of unnecessary code means that in future iterations and bug fixes, developers can focus more on core logic, avoid misunderstandings or errors caused by zombie code, and effectively control maintenance workload and complexity.
[0103] 3. Optimize Storage and Performance: This solution can be applied to the source code in a code repository (i.e., the source code collection of an application, software component, or software system) to streamline the code repository. This streamlined code repository not only saves storage space but also helps improve compilation speed and runtime efficiency, which is particularly important for large projects.
[0104] 4. Improve development efficiency: By reducing the time wasted on useless code, developers can devote more energy to developing new features and optimizing existing features, thereby improving the overall efficiency of the team's human resource utilization and reducing unnecessary development costs.
[0105] Based on the same inventive concept, the present application embodiment also provides a code processing device. Figure 3 , is a structural diagram of a code processing device 300 provided in an embodiment of the present application, wherein the device 300 includes: a determination module 310 and a deletion module 320.
[0106] The determination module 310 is used to determine a first code fragment using the first parameter from the first code based on the identifier of the first parameter, where the first code fragment includes description information of the first parameter, and the description information is used to describe the value of the first parameter under different conditions.
[0107] The determining module 310 is further configured to determine a first condition satisfied by the first parameter from the description information by running the first code.
[0108] The deleting module 320 is configured to delete the first parameter from the first code segment based on the first value of the first parameter under the first condition to obtain a second code.
[0109] In another embodiment, the determining module is configured to: Based on the first logical branch corresponding to the first value, determining the second logical branch from the description information of the first parameter; Deleting the description information of the first parameter and the second logic branch from the first code fragment to obtain a third code; If the third code passes the verification, the third code is determined as the second code.
[0110] In another embodiment, a logic branch includes a first statement describing a value of the first parameter and a second statement describing a processing operation corresponding to the first parameter; When the determination module deletes the description information of the first parameter and the second logical branch from the first code fragment to obtain the third code, the determination module performs the following steps: The description information of the first parameter and the second logic branch are deleted from the first code fragment, and the first statement in the first logic branch is deleted to obtain a third code.
[0111] In another embodiment, the code processing device further includes: A generating module, configured to generate a test task for testing the third code; A testing module, configured to run the test task and perform regression testing on the third code; The determining module is further configured to determine whether the third code passes the verification if the third code passes the regression test.
[0112] In another embodiment, the identifier includes a first identifier; The determining module is used for: A code segment containing the first identifier in the first code is searched as a first code segment using the first parameter.
[0113] In another embodiment, the identifier further includes a second identifier and a third identifier; The determining module performs the following steps when searching for a code segment containing the first identifier in the first code as the first code segment using the first parameter: Loading the first code into a virtual machine for execution; During the execution of the first code, scanning a first type of file containing a first annotation, wherein the first annotation contains the second identifier; In the first type of file, scanning a first method containing a second annotation, wherein the second annotation contains the third identifier; In the first method, scanning a third annotation including the first identifier; The code snippet to which the third annotation belongs is determined as the first code snippet.
[0114] In another embodiment, the number of the first code snippets is multiple; The code processing device further includes: a comparison module, configured to compare a first value corresponding to a first condition in the description information included in each first code snippet; The deletion module is used to delete the first parameter from each first code fragment based on the first value corresponding to the first condition in the description information contained in each first code fragment to obtain the second code if the first value corresponding to the first condition in the description information contained in each first code fragment is the same.
[0115] In another embodiment, the deletion module is further configured to: The first parameter is deleted from a parameter library, where the parameter library is used to manage the first parameters used in the first code.
[0116] Obviously, the code processing device provided in the embodiment of the present application can be used as Figure 1 The code shown handles the execution of the method, for example Figure 1 In the code processing method shown, steps S102 and S104 can be performed by Figure 3 The determination module 310 in the code processing device shown in FIG. 1 is executed, and step S106 can be performed by Figure 3 The deletion module 320 in the code processing device shown is executed.
[0117] According to another embodiment of the present application, Figure 3 The various modules in the code processing device shown can be individually or all combined into one or several other modules to form a whole, or one (or more) of the modules can be further divided into multiple smaller modules to form a whole, which can achieve the same operation without affecting the realization of the technical effects of the embodiments of the present application. The above-mentioned units are divided based on logical functions. In actual applications, the functions of one module can also be implemented by multiple modules, or the functions of multiple modules can be implemented by one module. In an embodiment of the present application, the code processing device may also include other modules. In actual applications, these modules can also be implemented with the assistance of other modules, and can be implemented by the collaboration of multiple modules.
[0118] According to another embodiment of the present application, a general computing device such as a computer including a central processing unit (CPU), a random access memory (RAM), a read-only memory (ROM) and other processing elements and storage elements can be run to execute the following operations: Figure 1 A computer program (including program code) for each step involved in the corresponding method shown in FIG. Figure 3The computer program can be recorded on a computer-readable storage medium, for example, and transferred to an electronic device through the computer-readable storage medium and run therein.
[0119] Figure 4 This is a schematic diagram of a computing device provided by an embodiment of the present application. Figure 4 At the hardware level, the computing device includes a processor and, optionally, an internal bus, a network interface, and memory. The memory may include internal memory, such as high-speed random-access memory (RAM), or non-volatile memory, such as at least one disk drive. Of course, the computing device may also include other hardware required for the business.
[0120] The processor, network interface, and memory can be interconnected via an internal bus, which can be an ISA (Industry Standard Architecture) bus, a PCI (Peripheral Component Interconnect) bus, or an EISA (Extended Industry Standard Architecture) bus. The bus can be divided into an address bus, a data bus, a control bus, and the like. For ease of representation, Figure 4 Only one bidirectional arrow is used in the diagram, but this does not mean that there is only one bus or one type of bus.
[0121] The memory is used to store programs. Specifically, the program may include program code, which includes computer operating instructions. The memory may include internal memory and non-volatile memory, and provides instructions and data to the processor.
[0122] The processor reads the corresponding computer program from the non-volatile memory into the internal memory and then runs it, forming a code processing device at the logical level. The processor executes the program stored in the memory and is specifically used to perform the following operations: Based on the identifier of the first parameter, determining, from the first code, a first code segment that uses the first parameter, where the first code segment includes description information of the first parameter, where the description information is used to describe values of the first parameter under different conditions; Determine, by running the first code, a first condition satisfied by the first parameter from the description information; Based on a first value of the first parameter under the first condition, the first parameter is deleted from the first code segment to obtain a second code.
[0123] The above application Figure 1 The methods performed by the code processing device disclosed in the illustrated embodiments can be applied to or implemented by a processor. The processor may be an integrated circuit chip with signal processing capabilities. During implementation, each step of the above method can be performed by hardware integrated logic circuits within the processor or by software instructions. The above processor can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components. The methods, steps, and logic block diagrams disclosed in the embodiments of this application can be implemented or executed. The general-purpose processor can be a microprocessor or any conventional processor. The steps of the methods disclosed in the embodiments of this application can be directly implemented and executed by a hardware decoding processor, or by a combination of hardware and software modules within the decoding processor. The software module can be located in a storage medium well-known in the art, such as random access memory, flash memory, read-only memory, programmable read-only memory, electrically erasable programmable memory, registers, etc. The storage medium is located in the memory, and the processor reads the information in the memory and, in conjunction with its hardware, completes the steps of the above method.
[0124] The computing device may also execute Figure 1 Method, and implement the code processing device in Figure 1 The functions of the illustrated embodiment will not be described in detail in the embodiments of the present application.
[0125] Of course, in addition to software implementation, the computing device of this application does not exclude other implementation methods, such as logic devices or a combination of software and hardware, etc. That is to say, the execution subject of the following processing flow is not limited to each logic unit, but can also be hardware or logic devices.
[0126] The present application also provides a computer-readable storage medium that stores one or more programs, wherein the one or more programs include instructions that, when executed by a computing device including multiple application programs, enable the computing device to execute Figure 1 The method of the embodiment shown is specifically used to perform the following operations: Based on the identifier of the first parameter, determining, from the first code, a first code segment that uses the first parameter, where the first code segment includes description information of the first parameter, where the description information is used to describe values of the first parameter under different conditions; Determine, by running the first code, a first condition satisfied by the first parameter from the description information; Based on a first value of the first parameter under the first condition, the first parameter is deleted from the first code segment to obtain a second code.
[0127] An embodiment of the present application further provides a computer program product, which stores instructions. When the instructions are executed by a computer, the computer implements the code processing method provided by the embodiment of the present application.
[0128] In short, the above description is only a preferred embodiment of the present application and is not intended to limit the scope of protection of the present application. Any modifications, equivalent replacements, improvements, etc. made within the spirit and principles of the present application shall be included in the scope of protection of the present application.
[0129] The systems, devices, modules, or units described in the above embodiments may be implemented by computer chips or entities, or by products having certain functions. A typical implementation device is a computer. Specifically, the computer may be, for example, a personal computer, a laptop computer, a cellular phone, a camera phone, a smartphone, a personal digital assistant, a media player, a navigation device, an email device, a game console, a tablet computer, a wearable device, or a combination of any of these devices.
[0130] Computer-readable media includes permanent and non-permanent, removable and non-removable media that can be implemented by any method or technology to store information. The information can be computer-readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disc read-only memory (CD-ROM), digital versatile disc (DVD) or other optical storage, magnetic cassettes, magnetic disk storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer-readable media does not include transitory computer-readable media (transitory media), such as modulated data signals and carrier waves.
[0131] It should also be noted that the terms "comprises," "includes," or any other variations thereof are intended to encompass non-exclusive inclusion, such that a process, method, commodity, or apparatus that includes a series of elements includes not only those elements but also other elements not explicitly listed, or includes elements inherent to such process, method, commodity, or apparatus. In the absence of further limitations, an element defined by the phrase "comprises a ..." does not exclude the presence of other identical elements in the process, method, commodity, or apparatus that includes the element.
[0132] The various embodiments in this specification are described in a progressive manner. Similar parts between the various embodiments can be referred to in conjunction with each other. Each embodiment focuses on the differences between the other embodiments. In particular, the system embodiments are generally similar to the method embodiments, so the description is relatively simple. For relevant parts, refer to the description of the method embodiments.
Claims
1. A code processing method, characterized in that: include: Based on the identifier of the first parameter, determining, from the first code, a first code segment that uses the first parameter, where the first code segment includes description information of the first parameter, where the description information is used to describe values of the first parameter under different conditions; Determine, by running the first code, a first condition satisfied by the first parameter from the description information; Based on a first value of the first parameter under the first condition, the first parameter is deleted from the first code segment to obtain a second code.
2. The method according to claim 1, characterized in that The first code segment further includes logic branches corresponding to different values of the first parameter; and based on the first value of the first parameter under the first condition, deleting the first parameter from the first code segment to obtain the second code includes: Based on the first logical branch corresponding to the first value, determining a second logical branch from the description information of the first parameter; Deleting the description information of the first parameter and the second logic branch from the first code fragment to obtain a third code; If the third code passes the verification, the third code is determined as the second code.
3. The method according to claim 2, characterized in that A logic branch includes a first statement describing a value of the first parameter and a second statement describing a processing operation corresponding to the first parameter; The step of deleting the description information of the first parameter and the second logic branch from the first code fragment to obtain a third code includes: The description information of the first parameter and the second logic branch are deleted from the first code fragment, and the first statement in the first logic branch is deleted to obtain a third code.
4. The method according to claim 2, characterized in that The method further comprises: generating a test task for testing the third code; Running the test task to perform regression testing on the third code; If the third code passes the regression test, it is determined that the third code passes the verification.
5. The method according to claim 1, wherein The identification includes a first identification; The step of determining, based on the identification of the first parameter, a first code segment using the first parameter from the first code includes: A code segment containing the first identifier in the first code is searched as a first code segment using the first parameter.
6. The method according to claim 5, characterized in that The identification further includes a second identification and a third identification; The step of searching for a code segment containing the first identifier in the first code as the first code segment using the first parameter includes: Loading the first code into a virtual machine for execution; During the execution of the first code, scanning a first type of file containing a first annotation, wherein the first annotation contains the second identifier; In the first type of file, scanning a first method containing a second annotation, wherein the second annotation contains the third identifier; In the first method, scanning a third annotation including the first identifier; The code snippet to which the third annotation belongs is determined as the first code snippet.
7. The method according to claim 1, characterized in that There are multiple first code snippets; Before deleting the first parameter from the first code segment based on the first value of the first parameter under the first condition to obtain the second code, the method further includes: Comparing the first value corresponding to the first condition in the description information included in each first code snippet; The step of deleting the first parameter from the first code segment based on the first value of the first parameter under the first condition to obtain the second code includes: If the first value corresponding to the first condition in the description information contained in each first code fragment is the same, then based on the first value corresponding to the first condition in the description information contained in each code fragment, the first parameter is deleted from each first code fragment to obtain the second code.
8. The method according to claim 1, characterized in that After deleting the first parameter from the first code segment based on the first value of the first parameter under the first condition to obtain the second code, the method further includes: The first parameter is deleted from a parameter library, where the parameter library is used to manage the first parameters used in the first code.
9. A code processing device, characterized in that: include: a determining module, configured to determine, based on an identifier of a first parameter, from a first code, a first code segment that uses the first parameter, wherein the first code segment includes description information of the first parameter, the description information being used to describe values of the first parameter under different conditions; The determining module is further configured to determine a first condition satisfied by the first parameter from the description information by running the first code; A deletion module is configured to delete the first parameter from the first code segment based on a first value of the first parameter under the first condition, to obtain a second code.
10. A computing device, characterized in that include: Memory and processor, wherein The memory is used to store computer programs; The processor is coupled to the memory and is configured to execute the computer program stored in the memory to perform the method according to any one of claims 1 to 8.
11. A computer-readable storage medium storing a computer program, wherein the computer program, when executed by a computer, can implement the method according to any one of claims 1 to 8.
12. A computer program product, characterized in that The computer program product stores instructions, and when the instructions are executed by a computer, the computer is caused to implement the method according to any one of claims 1 to 8.