Detailed Description
In order to make the objects, technical solutions and advantages of the present application more apparent, the technical solutions of the present application will be clearly and completely described below with reference to specific embodiments of the present application and corresponding drawings. It will be apparent that the described embodiments are only some, but not all, embodiments of the application. All other embodiments, which can be made by those skilled in the art based on the embodiments of the application without making any inventive effort, are intended to be within the scope of the application.
As one of ordinary skill in the art can know, with the development of technology and the appearance of new scenes, the technical scheme provided by the embodiment of the application is also applicable to similar technical problems.
The terms first, second and the like in the description and in the claims and in the above-described figures, are used for distinguishing between similar elements and not necessarily for describing a particular sequential or chronological order. It is to be understood that the terms so used are interchangeable under appropriate circumstances and are merely illustrative of the manner in which embodiments of the application have been described in connection with the description of the objects having the same attributes. Furthermore, the terms "comprises," "comprising," and "having," and any variations thereof, are intended to cover a non-exclusive inclusion, such that a process, method, system, article, or apparatus that comprises a list of elements is not necessarily limited to those elements, but may include other elements not expressly listed or inherent to such process, method, article, or apparatus.
Partial conceptual description:
Java virtual machine (Java Virtual Machine, JVM) is a virtual machine specifically designed to execute Java bytecodes. The JVM provides a standard running environment for Java programs, and is responsible for converting compiled bytecodes into machine instructions on a specific hardware platform and executing them.
As previously mentioned, logic switches in code are often used to describe one mechanism for controlling the flow of code execution through conditional decisions. The logic switch can be applied to a wide variety of scenarios. Taking a software modification and upgrade scenario as an example, since it is uncertain whether the code of the new version software can run smoothly, a logic switch is usually set in the code of the software, and whether to execute a logic branch of the new version software or a logic branch of the old version software is determined by the logic switch. For example, two if sentences may be set to implement a logic switch, so as to control the execution of the switch of the code segment, for example, when the condition sentence "if=conditional 1" is satisfied, the logic branch of the new version software is executed, and when the condition sentence "if=conditional 2" is satisfied, the logic branch of the old version software is executed. The logic switch is utilized to control the switching execution of the logic switch, so that the problem that the software system is unstable in operation and unstable state is difficult to eliminate due to the fact that the original old version software which is stably operated is directly changed into the unstable new version software can be avoided.
In theory, after the code running of the new version software is stable, the logic switch and the related code content thereof which achieve the function are not executed and cannot be found by common code development tools (such as Eclipse, intelliJIDEA), so that the logic switch and the related code content thereof still remain in the code to be redundant. Although such redundant logic switches do not directly affect the operation of the software, there are a number of drawbacks:
(1) Reducing readability. Redundant logic switches can make the code tedious and messy, requiring more time and effort by newly added developers or maintainers to distinguish between valid and invalid codes when reading the code, which reduces the readability and understanding of the code.
(2) Interference code search and analysis. When using search tools such as grep or integrated development environment (INTEGRATED DEVELOPMENT ENVIRONMENT, IDE) search functions, redundant logic switches may appear in the search results, interfering with the developer's incorrect positioning and analysis of the relevant functions.
(3) The workload of maintaining the code is increased. Due to the existence of redundant logic switches, the developer needs additional care in making modifications or adding new functionality to avoid false touches to such logic switches, which undoubtedly increases the complexity and risk of maintenance work.
(4) Occupying storage space. While a single redundant logic switch occupies little space, a large accumulation can also have an impact on project file size, especially in large projects.
Thus, there is a need to identify and clear redundant logic switches in code to maintain the health of the code and the sustainable development of the project.
Therefore, the embodiment of the application provides a code processing method, wherein a first parameter is generally used as a logic switch in a code, the description information of the first parameter is used for describing the value of the first parameter under different conditions, the conditions and the values are used for controlling logic branches to be executed in the code, and the first parameter is provided with corresponding identifiers. Based on the identification of the first parameter, the first code segment using the first parameter can be quickly, accurately and comprehensively determined, wherein the code segment possibly comprises redundant logic switches, on the basis, the first condition met by the first parameter is determined from the description information by operating the first code, the logic branch corresponding to the first value of the first parameter under the first condition is the logic branch executed in the actual operation process of the first code, other logic branches are the redundant logic branches, and further the logic switches expressed by the first parameter are not useful any more and belong to the redundant logic switches, so that the mode of identifying and deleting the redundant logic switches by combining the actual operation condition of the code can accurately find the logic switches which are deeper to be hidden and are conditionally called or indirectly used, and the like, omission is avoided, and further, the first parameter is deleted from the first code segment based on the first value, so that the quick, accurate and comprehensive elimination of the redundant logic switches in the code is realized, the readability and the understanding of the code are improved, the redundancy code is prevented, the occupied space is reduced, the work space is saved, and the like.
It should be understood that the code processing method provided by the embodiment of the present application may be executed by a computing device, and in particular, may be executed by a processor of the computing device. The computing device may include a terminal, including for example, but not limited to, a smart phone, a tablet computer, a notebook computer, a desktop computer, a smart voice interaction device, a smart home appliance, a smart watch, a vehicle-mounted terminal, an aircraft, etc., or the computing device may further include a server, such as a stand-alone physical server, a server cluster or a distributed system formed by a plurality of physical servers, and a cloud server for providing cloud computing services.
Different steps of the method may be implemented by the same execution body or different execution bodies, and the embodiment of the application does not limit what execution body is adopted to implement the method.
In addition, the execution sequence of the different steps is not limited in the embodiment of the application. When the method provided by the embodiment of the application is used, the execution sequence of different steps can be adjusted according to actual requirements.
The following describes in detail the technical solutions provided by the embodiments of the present application with reference to the accompanying drawings.
Referring to fig. 1, a flowchart of a code processing method according to an embodiment of the application is shown, and the method includes the following steps:
S102, determining a first code segment using the first parameter from the first code based on the identification of the first parameter.
The first code may be any code to be processed. The first parameter refers to a parameter used as a logic switch in the code. The first code includes a plurality of code segments. The first code segment includes description information of the first parameter, the description information is used for describing the value of the first parameter under different conditions, and the conditions and the values are used for controlling logic branches to be executed in the code. In addition, the first code segment may further include logic branches corresponding to different values of the first parameter.
As an example, the description information of the first parameter includes the following fields:
The code, which represents the name of the first parameter, can be used as a unique mark of the first parameter, is the unique basis for distinguishing different first parameters, and essentially serves as the identity of the first parameter. The field has high universality, can be shared and called by a plurality of codes in a single application, can span a plurality of applications and is shared by different codes of the applications. In format, the field is single valued and not null.
The expression field indicates the condition that the first parameter satisfies. The value of the field may be a computable boolean expression, and the operation result of the boolean expression is the value of the first parameter. The value of the first parameter may be defined as true or false, or may be defined as 1 or 0. For example, the value of the expression may be a response determination condition of a hypertext transfer protocol (HyperText Transfer Protocol, HTTP) interface, a determination condition of date comparison, a method called to return a boolean value, or the like. In format, the field is single valued and not null.
And branch, wherein the field represents a logic branch corresponding to the value of the first parameter under different conditions. In format, the value of this field is multi-valued and not null.
For example, three types of description information of the first parameter are shown as follows:
Description information 1:code= "codeA", expression= "now () <20240601", branch= "1"
Description information 2:code= "codeA", expression= "objects. Isnull (methodA ()", branch= "12";
Description information 3:code= "codeB", expression= "methodB ()", branch= "true"
The description information 1 is description information of a first parameter named codeA, which indicates that a logical branch named 1 is executed when the first parameter satisfies a condition that the current date is earlier than 2024, month 6, and day 1, the description information 2 is description information of a first parameter named codeA, which indicates that a logical branch named 12 is executed when the first parameter satisfies a condition that a result returned by a call method da () method is null, and the description information 3 is description information of a first parameter named codeB, which indicates that a logical branch named true is executed when the first parameter satisfies a condition that a result returned by a call methodB () method is true.
The first parameter has a corresponding identification. As one example, the identification of the first parameter includes a first identification. The first identifier is used to distinguish the first parameter from other parameters. The first identifier may be set according to actual needs, which is not limited in the embodiment of the present application. For example, the first identification may be LogicSwitch.
Based on this, the step S102 includes the step of searching for a code segment of the first code that includes the first identifier as a first code segment using the first parameter.
As one example, a first code may be loaded into a virtual machine, such as a JVM, and the virtual machine traverses annotations of the first code via a reflection mechanism to find out whether a first identifier exists in the traversed annotations, and if the first identifier exists in the traversed annotations, the code segment to which the annotation belongs is determined to be the first code segment using the first parameter.
For example, table 1 below is one code segment of the first code. Since the annotation @ LogicSwitch containing the first identifier LogicSwitch is used in the code fragment, the code fragment is determined to be the first code fragment.
TABLE 1
Table 2 below is another code segment of the first code. Since the annotation @ LogicSwitch containing the first identifier LogicSwitch is also used in the code fragment, the code fragment is also determined to be the first code fragment.
TABLE 2
It can be understood that when the first code is loaded into the virtual machine, the information in the first code is stored in a data structure inside the virtual machine, thereby providing convenience for subsequent operation, reflection is a powerful mechanism provided by java language, and allows the code to check and operate class files, methods, variables and the like in the first code through the reflection mechanism when running, the virtual machine can conveniently traverse all parts of the class files, methods, variables and the like in the first code, and find whether annotations containing the first identifier exist or not, so that code fragments to which the annotations belong are accurately identified as the first code fragments.
As another example, the identification of the first parameter may further include a second identification for identifying the class file using the first parameter and a third identification for identifying the method using the first parameter. For example, the second identifier may be ClassLogicSwitch and the third identifier may be MethodLogicSwitch.
In this case, the first code is loaded into the virtual machine for operation, a first type of file containing a first annotation is scanned during the operation of the first code, the first annotation contains a second identifier, a first method containing the second annotation is scanned in the first type of file, the second annotation contains a third identifier, a third annotation containing the first identifier is scanned in the first method, and a code segment to which the third annotation belongs is determined to be the first code segment.
Specifically, after the first code is loaded into the virtual machine to run, the virtual machine can conveniently traverse the class file in the first code to find whether the first annotation containing the second identifier exists, if the first annotation is found, the code fragment using the first parameter is determined to exist in the first class file to which the first annotation belongs, further, the virtual machine traverses the method in the first class file to find whether the second annotation containing the third identifier exists, if the second annotation is found, the code fragment using the first parameter is determined to exist in the first method to which the second annotation belongs, further, the virtual machine traverses the first step method to find whether the third annotation containing the first identifier exists, and if the third annotation is found, the first parameter is determined to be used in the code fragment to which the third annotation belongs, and further, the first code fragment is obtained.
In the above manner, the virtual machine can acquire the annotation in the code by using the reflection mechanism, and uses the first identifier, the second identifier and the third identifier to position the first type file using the first parameter by using the first annotation containing the second identifier, then uses the second annotation containing the third identifier to position the first method using the first parameter from the first type file, and finally uses the third annotation containing the first identifier to position the first code fragment using the first parameter from the first method, thereby ensuring the positioning accuracy and comprehensiveness of the first code fragment, avoiding omission, and laying a foundation for the subsequent accurate and comprehensive deletion of the redundant logic switch and the corresponding code content in the first code.
The above shows some embodiments of S102 described above. Of course, it should be understood that S102 may be implemented in other manners, which are not limited by the embodiment of the present application.
S104, determining a first condition met by the first parameter from the description information of the first parameter by running the first code.
Specifically, the first code may be loaded into the virtual machine to run, and a first condition that is satisfied by the first parameter during the running of the first code may be monitored, where the first condition reflects an actual running result of the first code segment, that is, if the first parameter fails to satisfy the first condition during the running of the first code, the first condition is indicated as an unnecessary condition.
For example, taking description information 1 shown above as an example, if the current time is 2024, 5, 1, and is 2024, 6, 1, that is, the condition described by expression is satisfied during the running of the first code, this condition is determined as the first condition.
As 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 satisfied, and this condition is further determined as the first condition.
For another example, taking the description information 3 shown above as an example, if the result returned after methodB () is called is true, the condition described by expression is satisfied, and then the condition is determined as the first condition.
And S106, deleting 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.
Because the first parameter meets the first condition in the running process 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 in the actual running process of the first code, and the rest logic branches are redundant logic branches, so that the logic switch represented by the first parameter can be determined to be not useful any more, belongs to the redundant logic switch, and can be deleted from the first code.
In an embodiment, the first parameter is described in the first code segment using the description information, and in case of redundancy of the first parameter, the description information of the first parameter is also redundant, and for this purpose, the description information of the first parameter may be deleted to obtain the second code.
For example, taking the first code segment shown in table 1 as an example, the description information "code=" codeA ", expression=true, and branch= { false }" in the code segment may be deleted.
In another embodiment, logical branches other than the first logical branch may be deleted from the first code segment to obtain the second code.
For example, taking the first code segment shown in table 1 as an example, the first condition is expression=true, the logical branch corresponding to the value of the first parameter codeA under the first condition is else and the processing operation 2 included therein, and then other logical branches except the logical branch are deleted from the first code segment.
In another embodiment, considering that only the description information of the first parameter is deleted, the first code segment may not be operated, and the logical branch corresponding to the value under the condition other than the first condition in the first code segment is not executed, which also belongs to redundant content, and also causes a plurality of defects in the code. For this purpose, the step S106 includes the following steps:
s161, determining a second logic branch from the description information of the first parameter based on the first logic branch corresponding to the first value.
In particular, logical branches other than the first logical branch in the description information may be determined as the second logical branch. The second logical branch is a logical branch which can not be executed in the running process of the first code, and belongs to redundant logical branches.
For example, taking the first code segment shown in table 1 as an example, if expression=true, the first value is false, and the first logical branch corresponding to the first value is as follows:
else{
execution processing operation 2
}
The second logical branch may then be determined as follows:
if(flag){
Execution processing operation 1
}
For another example, taking the first code segment shown in table 2 as an example, if expression= "methodC ()" is satisfied, the first value is false, and the first logical branch corresponding to the first value is as follows:
case 1;
Execution of processing operation 1'
break;
default
Execution of processing operation 3'
breake;
The second logical branch may then be determined as follows:
case 2;
execution of processing operation 2'
break;
And S162, deleting the description information of the first parameter and the second logic branch from the first code segment to obtain a third code.
As an example, the description information of the first parameter and the first logical branch may be deleted directly from the first code segment.
As another example, one logical branch includes a first statement describing a value of a first parameter and a second statement describing a processing operation to which the first parameter corresponds. In this case, the step S162 includes the steps of deleting the description information of the first parameter and the second logical branch from the first code segment, and deleting the first statement in the first logical branch to obtain the third code.
For example, continuing with the example of the first code segment shown in table 1 above, the first code segment includes two logical branches, the first logical branch being a logical branch when expression=true, which includes a first statement "else { }" and a second statement describing processing operation 2, and the other logical branch being a logical branch when expression=true, which includes a first statement "if (flag) { }" and a second statement describing processing operation 1. Since expression=true holds, the logical branch when expression=true holds is the first logical branch, and the logical branch when expression=true does not hold 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 resulting third code is shown in table 3 below:
TABLE 3 Table 3
As another example, continuing with the first code segment shown in table 2, the first code segment includes three logical branches, where the first logical branch is one logical branch when expression= "methodC ()" is established, the first sentence "case 1..break" and the second sentence describing the processing operation 1', the second logical branch is another logical branch when expression= "methodC ()" is established, the first sentence "default..break" and the second sentence describing the processing operation 3', and the third logical branch is a logical branch when expression=true is not established, and includes the first sentence "case 2..break" and the second sentence describing the processing operation 2 '. Since expression= "methodC ()" holds, the logical branch at the time of the logical branch when expression= "methodC ()" holds 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 resulting third code is shown in table 4 below:
TABLE 4 Table 4
And S163, if the third code passes the verification, determining the third code as the second code.
After the third code is obtained, by verifying the third code, it can be ensured that no new errors are introduced to the modification of the first code, while ensuring that the modified third code operates as intended. And if the third code does not pass the verification, the first code is revised and the revised first code is verified until the revised first code passes the verification.
By the implementation mode, all redundant code contents related to the first parameter in the first code can be deleted rapidly, accurately and comprehensively, and normal operation of the obtained second code is ensured.
In embodiments of the present application, the third code may be verified in a variety of suitable ways, including, but not limited to, unit testing, integration testing, system testing, regression testing, acceptance testing, performance testing, security testing, static testing, dynamic testing, and the like.
In one embodiment, verifying the third code includes the steps of:
And step A1, generating a test task for testing the third code.
And adding parameters required by the test into the corresponding task templates to obtain corresponding test tasks. For example, if the test task is to perform regression test on the third code, parameters required by the test, such as test environment parameters, are added to a task template corresponding to the regression test, so as to obtain the test task.
And step A2, running a test task, and carrying out regression test on the third code.
Regression testing of the third code includes a series of tests performed to ensure that the third code does not destroy the original business functions in order to verify that the original business functions are functioning properly. For example, the business functions that the first code may implement include merchandise display, adding merchandise to a shopping cart, settling payment, and the like. After the first code is modified to obtain a third code, for example, a payment interface using the first parameter or a first code segment corresponding to the preferential calculation mode is modified, the regression test is performed on the third code, which includes checking whether the commodity can be normally displayed through the third code, whether the user can add the commodity to the shopping cart, whether the settlement payment function is normal, and the like.
And step A3, if the third code passes the regression test, determining that the third code passes the verification.
By performing regression testing on the third code, it can be ensured that the modified third code does not damage the business function of the cause.
The above shows some embodiments of S106 described above. Of course, it should be understood that S106 may be implemented in other manners, which are not limited by the embodiment of the present application.
In the embodiment of the present application, the number of the first code segments may be plural, that is, the same first parameter is used in different first code segments. At this time, if the logic branches controlled by the first parameters contradict each other, for example, if the same first parameter is used at different positions of the first code, the contradiction that the first parameter can be deleted or not deleted is generated, the consistency of the logic decision will be affected, so that the accuracy and the uniformity of the logic processing are affected.
Therefore, in another embodiment of the present application, before the step S106, the code processing method provided in the embodiment of the present application further includes comparing the first value corresponding to the first condition in the description information included in each first code segment. Correspondingly, in S106, if the first values corresponding to the first conditions in the description information included in each first code segment are the same, the first parameter is deleted from each first code segment based on the first values corresponding to the first conditions in the description information included in each code segment, so as to obtain the second code.
For example, assume that the first code includes two first code segments, and the first code segments each use a first parameter codeA, where the description information of the first parameter in the first code segment is as follows:
code= "codeA",expression="now()<20240601",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"
Assuming that the current time is 2024, 5, 1, then in the first code segment, expression= "now () <20240601" is satisfied in the description information of the first parameter codeA, and the first value of the first parameter codeA corresponding to the first condition is true.
In the second first code segment, expression= "objects.isnull (methodA ())", in the description information of the first parameter codeA, the first value of the first parameter codeA corresponding to the first condition is true.
Since the first parameter codeA is not inconsistent with the two first code segments, the first parameter codeA may be deleted from the two first code segments based on the first value true of the first parameter codeA.
In another embodiment of the present application, after the step S106, the code processing method provided in the embodiment of the present application may further include deleting the first parameter from the parameter library. The parameter library is used for managing first parameters used in the first code.
The parameter library is used as a unified tool for managing all parameters describing the logic switch, and if the redundant first parameters are not deleted, the readability and understanding of the parameter library, the searching and analysis of interference parameters, the workload of maintaining the parameter library and the occupied storage space are affected, and the code operation is also affected. For this reason, these drawbacks can be effectively solved by deleting the first parameter in the parameter library, so that the parameter describing the logic switch in the code can be better managed by using the parameter library.
One or more embodiments of the present application provide a code processing method, in which a first parameter is generally used as a logic switch in a code, and the values of the first parameter under different conditions are described using description information of the first parameter, where the conditions and the values are used to control a logic branch to be executed in the code, and the first parameter has a corresponding identifier. Based on the identification of the first parameter, the first code segment using the first parameter can be quickly, accurately and comprehensively determined, wherein the code segment possibly comprises redundant logic switches, on the basis, the first condition met by the first parameter is determined from the description information by operating the first code, the logic branch corresponding to the first value of the first parameter under the first condition is the logic branch executed in the actual operation process of the first code, other logic branches are the redundant logic branches, and further the logic switches expressed by the first parameter are not useful any more and belong to the redundant logic switches, so that the mode of identifying and deleting the redundant logic switches by combining the actual operation condition of the code can accurately find the logic switches which are deeper to be hidden and are conditionally called or indirectly used, and the like, omission is avoided, and further, the first parameter is deleted from the first code segment based on the first value, so that the quick, accurate and comprehensive elimination of the redundant logic switches in the code is realized, the readability and the understanding of the code are improved, the redundancy code is prevented, the occupied space is reduced, the work space is saved, and the like.
In order to facilitate understanding of the above code processing method, a specific implementation procedure of the code processing method provided by the embodiment of the present application applied to an actual scenario is described below with reference to fig. 2.
As shown in fig. 2, a schematic diagram of the implementation process includes the following main implementation stages:
First stage, application acquisition stage
In a first stage, a computing device obtains various applications in a subsystem of an enterprise, such as, but not limited to, middleware systems, payment systems, order systems, financial systems, and the like.
The subsystem may be, for example, a set of applications other than system software in a computer software system of an enterprise.
Second stage, class file loading and scanning stage
The second stage aims at finding the first, second and third identifications of the first parameter from the class file of the code of the application. These three identifications are set in the class file by the developer of the application.
In the programming arts, class files generally refer to files that contain class definitions. For example, in the Java language, a class file typically extends under ". Java" and contains definitions of one or more classes, each defining the properties and methods of an object. After compilation, these class files are converted into bytecode files, with extensions ". Class", which can run on a Java virtual machine. The class file does not contain any delimiters, its individual data items are arranged compactly, and the order and number are well defined. The meaning, length, and sequencing of each byte representation are not allowed to change to ensure proper interpretation and execution of the Java virtual machine.
As shown in fig. 2, in the second stage, the following sub-steps 1 to 4 are mainly performed:
Firstly, the computing equipment loads each class file in the acquired codes of the application into a virtual machine;
step 2, the virtual machine scans each class file, namely, the file identification of the class file is scanned firstly to judge whether a first annotation containing a second identification exists in each class, the class file judged to be yes can be determined to use a first parameter in the class file, and then the sub-step 3 can be executed;
For example, the computing device may enter a second identification (e.g., classLogicSwitch) into the virtual machine, thereby indicating that the first parameter was used in a class file once the virtual machine scanned that the second identification was included in the annotation for that class file.
A sub-step 3 of further scanning the method defined in the class file by the virtual machine for judging whether the second annotation containing the third mark exists in each method or not for the class file using the first parameter, determining that the first parameter is used in the method for judging yes, and executing the sub-step 4 for judging no;
For example, the computing device may enter a third identification (e.g., methodLogicSwitch) into the virtual machine, thereby indicating that the first parameter was used in the class once the virtual machine scanned that the third identification was included in the annotation for a method.
It should be noted that class files and methods are two basic concepts in object-oriented programming, each of which assumes different responsibilities. A class file is an abstract data type that defines a set of properties and methods for describing objects with identical characteristics and behaviors. And the method is a function defined in the class file for realizing the specific behavior of the class file. Each method has a name, parameter list and return type (if any) and the method can access the member variables of the class file and other methods to implement complex logic and functions.
Sub-step 4, for the method using the first parameter, the virtual machine further scans the code segments of the method to determine whether a second annotation containing the first identifier exists in each code segment; for the code segment judged to be yes, the first parameter is used in the code segment, and then the code segment is determined to be the first code segment;
The first identifier, the second identifier, and the third identifier may be local variables, also referred to as internal variables, and are variables defined inside a method (function), and are variables defined inside a method or inside a compound sentence. The lifetime of the local variable is the end of the time from the time when the method is called to the time when the method returns to the use, that is to say, the local variable is created when the method is called, and destroyed after the execution of the method is completed.
A third stage of summarizing the description information of all the first parameters in the first code segment of subsystem and making processing
In the third phase, for each application, the computing device mainly performs the following sub-step I and sub-step II:
a substep I of classifying all the first parameters 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()<20240601",branch="1")
LogicSwitch(code= "codeA",expression="now()<20240701",branch="12")
LogicSwitch(code= "codeB",expression="methodA()",branch="true")
Then, grouping is performed by code, i.e., the description information belonging to the first parameter codeA is divided into one subgroup, and the description information belonging to the first parameter codeB is divided into another subgroup.
And II, for each subgroup obtained by grouping the descriptive information, acquiring the execution result of the expression contained in the descriptive information in each subgroup one by one.
The expression included in each description information in the same group may be various in form, for example, as follows:
non () <20240601 current time less than 20240601
Objects.isNull (methodA ()): execution of method methodA results in null
MethodB (): the return result of method methodB (the return result is of the boolean type)
Fourth stage of executing expression and rewriting code logic
Taking a certain said subgroup as an example, in the fourth phase the following sub-steps are mainly performed:
A substep a, executing each expression contained in each description information in the group so as to obtain an execution result of each expression, if the execution result contains "false", judging that the logic switch can not be deleted from the application so as to end the flow, and if the execution results are "true", executing the substep b;
On one hand, the snapshot is carried out on the class file to which the group belongs, namely, the backup of the class file at the current time point is created, so that the snapshot is added into an operation log of the class file, the examination and the code program optimization are carried out on the class file before the code is rewritten on the basis of the snapshot in the operation log, and on the other hand, the operation of rewriting the code logic is carried out, namely, the code of the application to which the class file belongs is rewritten;
the rewriting of the code of the application means deleting the logical branch of which the execution result of each expression included in each piece of description information in the group is "false".
A substep c of submitting a merge request to the code management system GIT;
the request is for requesting the GIT to incorporate the code-rewritten application into the subsystem as part of the subsystem.
The GIT is an open-source distributed version control system, and can effectively and rapidly process project version management from small to very large.
The sub-step d, the code management system GIT examines and approves the merging request, if the examination and approval is passed, the sub-step e is executed, if the examination and approval is not passed, the application after the code is rewritten is rolled back, thereby the application before the code is modified is rolled back;
Step e, creating a regression task of the tester;
Because the codes are changed, in order to ensure that the original function is normal, the original function must be returned, so the GIT can be connected with a third party system, and the third party system is triggered to create a test person regression task to remind the test person of the timely regression function.
Step f, testing personnel verify whether the function of the application after the code rewriting is normal or not, if so, deleting the codes (such as deleting codeA) in the group from a switch library of the class file of the application, then recording the related information of the deleting operation into an operation log of the class file, ending the flow, and if not, executing the step g;
And g, modifying the code of the application after the code is rewritten by a developer, and then re-executing the substep f.
In the life cycle of software development, the continuous flow builds a closed-loop quality control system from preliminary code adjustment, code review and merging to a main branch to comprehensive functional test verification until the logic switch and redundant code which are not needed any more are finally identified and cleaned.
Experiments prove that the scheme provided by the embodiment of the application can obviously improve the definition and maintainability of the target code after being applied to the quality control system. More specific effects include:
1. and the readability of the codes is enhanced, namely, the codes become more concise and clear by removing the outdated logic branches, so that new and old developers can quickly understand business logic, and the team cooperation efficiency is accelerated.
2. The maintenance cost is reduced, the unnecessary code quantity is reduced, so that in the future iteration and bug repair process, developers can concentrate on core logic more, misunderstanding or errors caused by zombie codes are avoided, and the maintenance workload and complexity are controlled effectively.
3. Optimizing storage and performance by executing the scheme on source code in a code library (i.e., source code set of an application program, software components or software system) can achieve the effect of simplifying the code library. The simplified code library saves storage space, is beneficial to improving compiling speed and running efficiency, and is particularly important for large projects.
4. The development efficiency is improved, the time wasted on useless codes is reduced, and developers can put more effort on new function development and optimizing the existing functions, so that the human resource utilization efficiency of a team is improved as a whole, and the unnecessary development cost is reduced.
Based on the same inventive concept, the embodiment of the application also provides a code processing device. Referring to fig. 3, a schematic structure of a code processing apparatus 300 according to an embodiment of the present application is shown, where the apparatus 300 includes a determining module 310 and a deleting module 320.
A determining module 310, configured to determine, based on the identification of the first parameter, a first code segment using the first parameter from a first code, where the first code segment includes description information of the first parameter, where the description information is used to describe a value of the first parameter under different conditions.
The determining module 310 is further configured to determine, from the description information, a first condition that the first parameter satisfies by executing the first code.
And a deleting module 320, configured to delete the first parameter from the first code segment based on a first value of the first parameter under the first condition, so as to obtain a second code.
In another embodiment, the determining module is configured to:
Determining the second logic branch from the description information of the first parameter based on the first logic branch corresponding to the first value;
deleting the description information of the first parameter and the second logic branch from the first code segment to obtain a third code;
if the third code is authenticated, the third code is determined to be a second code.
In another embodiment, one logical 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 determining module performs the following steps when deleting the description information of the first parameter and the second logical branch from the first code segment to obtain a third code:
Deleting the description information 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.
In another embodiment, the code processing apparatus further includes:
the generation module is used for generating a test task for testing the third code;
The test module is used for running the test task and carrying out regression test on the third code;
the determining module is further configured to determine that the third code passes the verification if the third code passes a regression test.
In another embodiment, the identification comprises a first identification;
The determining module is used for:
And searching a code segment containing the first identifier in the first code to be used as a first code segment using the first parameter.
In another embodiment, the identifiers further comprise a second identifier and a third identifier;
the determining module performs the following steps when searching the 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 running;
Scanning a first type file containing a first annotation during the running process of the first code, wherein the first annotation contains the second identifier;
Scanning a first method comprising a second annotation in the first type of file, the second annotation comprising the third identification;
in the first method, scanning a third annotation containing the first identity;
and determining the code segment to which the third annotation belongs as the first code segment.
In another embodiment, the number of the first code segments is a plurality;
the code processing apparatus further includes:
The comparison module is used for comparing the first value corresponding to the first condition in the description information contained in each first code fragment;
And the deleting module is used for deleting the first parameter from each first code segment based on the first value corresponding to the first condition in the description information contained in each code segment if the first value corresponding to the first condition in the description information contained in each first code segment is the same, so as to obtain a second code.
In another embodiment, the deletion module is further configured to:
Deleting the first parameter from a parameter library, wherein the parameter library is used for managing the first parameter used in the first code.
It is obvious that the code processing apparatus provided in the embodiment of the present application can be used as an execution subject of the code processing method shown in fig. 1, for example, in the code processing method shown in fig. 1, steps S102 and S104 may be executed by the determination module 310 in the code processing apparatus shown in fig. 3, and step S106 may be executed by the deletion module 320 in the code processing apparatus shown in fig. 3.
According to another embodiment of the present application, each module in the code processing apparatus shown in fig. 3 may be separately or completely combined into one or several other modules to form a structure, or some (some) of the modules may be further split into a plurality of modules with smaller functions to form a structure, which may achieve the same operation without affecting the implementation of the technical effects of the embodiments of the present application. The above units are divided based on logic functions, and in practical application, the functions of one module may be implemented by a plurality of modules, or the functions of a plurality of modules may be implemented by one module. In the embodiment of the application, the code processing apparatus may also include other modules, and in practical application, these modules may also be implemented with assistance of other modules, and may be implemented by cooperation of multiple modules.
According to another embodiment of the present application, a code processing apparatus as shown in fig. 3 may be constructed by running a computer program (including program code) capable of executing the steps involved in the respective methods as shown in fig. 1 on a general-purpose computing device such as a computer including a processing element such as a central processing unit (Central Processing Unit, CPU), a random access storage medium (Random Access Memory, RAM), a Read-Only Memory (ROM), and a storage element, and implementing the code processing method of the embodiment of the present application. The computer program may be recorded on, for example, a computer readable storage medium, transferred to, and run in, an electronic device via the computer readable storage medium.
FIG. 4 is a schematic diagram of a computing device according to one embodiment of the application. Referring to fig. 4, at the hardware level, the computing device includes a processor, and optionally an internal bus, a network interface, and a memory. The Memory may include a Memory, such as a Random-Access Memory (RAM), and may further include a non-volatile Memory (non-volatile Memory), such as at least 1 disk Memory. Of course, the computing device may also include hardware required for other services.
The processor, network interface, and memory may be interconnected by an internal bus, which may be an ISA (Industry Standard Architecture ) bus, a PCI (PERIPHERAL COMPONENT INTERCONNECT, peripheral component interconnect standard) bus, or EISA (Extended Industry Standard Architecture ) bus, among others. The buses may be classified as address buses, data buses, control buses, etc. For ease of illustration, only one bi-directional arrow is shown in FIG. 4, but not only one bus or type of bus.
And the memory is used for storing programs. In particular, the program may include program code including computer-operating instructions. The memory may include memory and non-volatile storage and provide instructions and data to the processor.
The processor reads the corresponding computer program from the nonvolatile memory into the memory and then runs, and the code processing device is formed on the logic level. The processor is used for executing the programs stored in the memory and is specifically used for executing the following operations:
Determining a first code segment using a first parameter from a first code based on the identification of the first parameter, wherein the first code segment comprises description information of the first parameter, and the description information is used for describing the value of the first parameter under different conditions;
Determining a first condition satisfied by the first parameter from the description information by running the first code;
And deleting 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.
The method performed by the code processing apparatus disclosed in the embodiment of fig. 1 of the present application may be applied to a processor or implemented by a processor. The processor may be an integrated circuit chip having signal processing capabilities. In implementation, the steps of the above method may be performed by integrated logic circuits of hardware in a processor or by instructions in the form of software. The Processor may be a general-purpose Processor including a central processing unit (Central Processing Unit, CPU), a network Processor (Network Processor, NP), etc., or may be a digital signal Processor (DIGITAL SIGNAL Processor, DSP), application SPECIFIC INTEGRATED Circuit (ASIC), field-Programmable gate array (Field-Programmable GATE ARRAY, FPGA) or other Programmable logic device, discrete gate or transistor logic device, discrete hardware components. The disclosed methods, steps, and logic blocks in the embodiments of the present application may be implemented or performed. A general purpose processor may be a microprocessor or the processor may be any conventional processor or the like. The steps of the method disclosed in connection with the embodiments of the present application may be embodied directly in the execution of a hardware decoding processor, or in the execution of a combination of hardware and software modules in a decoding processor. The software modules may be located in a random access memory, flash memory, read only memory, programmable read only memory, or electrically erasable programmable memory, registers, etc. as well known in the art. The storage medium is located in a memory, and the processor reads the information in the memory and, in combination with its hardware, performs the steps of the above method.
The computing device may also execute the method of fig. 1 and implement the functions of the code processing apparatus in the embodiment shown in fig. 1, which is not described herein.
Of course, other implementations, such as logic devices or combinations of hardware and software, are not excluded from the computing device of the present application, that is, the execution subject of the following processing flows is not limited to the respective logic units, but may be hardware or logic devices.
Embodiments of the present application also provide a computer-readable storage medium storing one or more programs, the one or more programs comprising instructions, which when executed by a computing device comprising a plurality of application programs, enable the computing device to perform the method of the embodiment of fig. 1, and in particular to perform the operations of:
Determining a first code segment using a first parameter from a first code based on the identification of the first parameter, wherein the first code segment comprises description information of the first parameter, and the description information is used for describing the value of the first parameter under different conditions;
Determining a first condition satisfied by the first parameter from the description information by running the first code;
And deleting 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.
Embodiments of the present application also provide a computer program product storing instructions that, when executed by a computer, cause the computer to implement the code processing method provided by the embodiments of the present application.
In summary, the foregoing description is only of the preferred embodiments of the present application, and is not intended to limit the scope of the present application. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application should be included in the protection scope of the present application.
The system, apparatus, module or unit set forth in the above embodiments may be implemented in particular by a computer chip or entity, or by a product having a certain function. One typical implementation is a computer. In particular, the computer may be, for example, a personal computer, a laptop computer, a cellular telephone, a camera phone, a smart phone, 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.
Computer readable media, including both non-transitory and non-transitory, removable and non-removable media, may implement information storage by any method or technology. The information may be computer readable instructions, data structures, modules of a program, or other data. Examples of storage media for a computer 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 Discs (DVD) or other optical storage, magnetic cassettes, magnetic tape disk storage or other magnetic storage devices, or any other non-transmission medium, which can be used to store information that can be accessed by a computing device. Computer-readable media, as defined herein, does not include transitory computer-readable media (transmission media), such as modulated data signals and carrier waves.
It should also be noted that the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising one does not exclude the presence of other like elements in a process, method, article, or apparatus that comprises an element.
In this specification, each embodiment is described in a progressive manner, and identical and similar parts of each embodiment are all referred to each other, and each embodiment mainly describes differences from other embodiments. In particular, for system embodiments, since they are substantially similar to method embodiments, the description is relatively simple, as relevant to see a section of the description of method embodiments.