A method for reducing code coupling based on abstract policy
By identifying logical statements and logical judgment code blocks, and combining business data sources and data volume to evaluate code coupling, and then abstracting the data, the problem of increased code complexity in existing technologies is solved, achieving efficient decoupling and simplified maintenance of the code.
Patent Information
- Application Number
- CN202311484808.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-11-09
- Publication Date
- 2025-12-19
- Estimated Expiration
- 2043-11-09
AI Technical Summary
Existing technologies fail to comprehensively consider the differences in code volume and coupling when reducing code coupling, leading to increased code complexity and maintenance difficulty.
By identifying logical statements and logical judgment code blocks, and combining the referenced business data sources and data volume, an initial coupling assessment of the code is performed. Logical judgment code blocks are then abstracted to generate a parent class interface, thereby achieving decoupling.
It improves the accuracy and efficiency of code coupling assessment, reduces code complexity, and simplifies the maintenance process.
Smart Images

Figure CN117573086B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The application belongs to the technical field of data processing, and particularly relates to a method for reducing code coupling based on an abstract strategy. BACKGROUND
[0002] With the rapid development of enterprise business, there will be more and more core business resource processing scenarios in the software service system of the enterprise. If if... else... is used to control the use of these algorithms, the code will become lengthy and complex, and will be strongly associated with other business codes, which will seriously affect the maintenance and expansion in the later stage, and also make the system more complex and difficult to maintain.
[0003] In order to reduce the technical problem of large coupling of software system caused by the large use of if-else and other logical statements, in the invention patent CN202111468034.X "Method and device for solving if-else coupling and high redundancy in code", the execution strategy corresponding to the identifier is determined in the configuration file according to the identifier, and the instance object corresponding to the execution strategy in the Spring container is called according to the determined execution strategy, so as to realize the reusability of the program and reduce the technical problem of high coupling of the program caused by the excessive number of logical statements. However, at the same time, the following technical problems exist:
[0004] The existing technical solution ignores the differentiated coupling processing result according to the coupling detection result of the code. Specifically, the use of logical statements is reduced, which increases the amount of code and the number of abstract execution modules that need to be maintained and tested. Therefore, if the above factors are not considered in the differentiated coupling processing mode, the complexity of the program may be increased.
[0005] In the existing technical solution, the coupling of the processing module of the logical statement is ignored during the coupling detection of the code. Specifically, the number of business data and the number of business data sources referenced by different logical statements exist to some extent. At the same time, the number of logical statements used by the use module of different logical statements also exists to some extent. These will cause the coupling of the use module of different logical statements to exist to some extent. Therefore, if the above factors are not considered, the coupling detection of the code cannot be accurately realized.
[0006] In view of the above technical problems, the application provides a method for reducing code coupling based on an abstract strategy. SUMMARY
[0007] To achieve the purpose of the application, the application adopts the following technical solutions:
[0008] According to one aspect of the present application, a method for reducing code coupling based on an abstraction strategy is provided.
[0009] A method for reducing code coupling based on an abstraction strategy, specifically comprising:
[0010] S1 adopts a preset keyword to determine the logical statements of the to-be-processed code and the logical judgment code blocks composed of different logical statements based on the matching results of the to-be-processed code, and determines the initial coupling evaluation result of the to-be-processed code based on the logical statements of the to-be-processed code and the logical judgment code blocks, and when the initial coupling evaluation result meets the requirements, proceeds to the next step;
[0011] S2 determines the reference business data source and the reference business data of the logical judgment code blocks based on the identification results of the keywords of the logical judgment code blocks of the to-be-processed code, and determines the coupling evaluation result of the logical judgment code blocks in combination with the usage data of the logical statements of the logical judgment code blocks;
[0012] S3 obtains the coupling evaluation results of different logical judgment code blocks of the to-be-processed code, and determines the coupling evaluation result of the to-be-processed code in combination with the initial coupling evaluation result of the to-be-processed code;
[0013] S4 determines the evaluation result of the to-be-processed code according to the coupling evaluation result of the to-be-processed code and the code amount, and when the evaluation result does not meet the requirements, performs abstraction processing on the reference business data of the logical judgment code blocks to obtain a parent class interface, and performs decoupling processing on the logical judgment code blocks of the to-be-processed code according to the parent class interface.
[0014] The present application has the following advantages:
[0015] 1. By determining the initial coupling evaluation result of the to-be-processed code based on the logical statements and the logical judgment code blocks of the to-be-processed code, the code coupling of the to-be-processed code is evaluated from the number and type of the logical statements and the number of the logical judgment code blocks, the to-be-processed code with high code coupling is preliminarily screened, and the processing efficiency of the to-be-processed code with high coupling is improved.
[0016] 2. By determining the coupling evaluation result of the logical judgment code blocks in combination with the reference business data source, the reference business data, and the usage data of the logical statements, not only the differences in coupling caused by the differences in the reference business data source and the reference business data of different logical judgment code blocks are considered, but also the differences in coupling caused by the differences in the usage data of different logical judgment code blocks are considered, thereby ensuring the accuracy of the coupling evaluation result of the logical judgment code blocks.
[0017] 3. By determining the evaluation result of the code to be processed based on the coupling evaluation result and the amount of code, the evaluation result is determined from the perspectives of code complexity and coupling, which also lays the foundation for further differentiated decoupling processing of the code to be processed.
[0018] A further technical solution is that the preset keywords include if-else, if, switch-case, for, and while.
[0019] A further technical solution is that the method for determining the evaluation result of the code to be processed is as follows:
[0020] When the coupling evaluation result of the code to be processed is greater than the preset code coupling degree:
[0021] The evaluation result of the code to be processed is determined to be unsatisfactory, and the evaluation result of the code to be processed is determined by the coupling evaluation result of the code to be processed.
[0022] When the coupling evaluation result of the code to be processed is not greater than the preset code coupling degree:
[0023] Obtain the code size of the code to be processed, and determine whether the code size of the code to be processed meets the requirements based on the code size of the code to be processed and the preset maximum code limit:
[0024] The evaluation result of the code to be processed is determined to be unsatisfactory, and the evaluation result of the code to be processed is determined by the amount of code in the code to be processed;
[0025] When the code size of the code to be processed does not meet the requirement based on the code size of the code to be processed and the preset maximum code limit:
[0026] The correction amount for the evaluation result is determined by the ratio of the amount of code to be processed to the preset maximum limit of code, and the evaluation result of the code to be processed is determined based on the correction amount for the evaluation result and the coupling evaluation result of the code to be processed.
[0027] A further technical solution involves decoupling the logical judgment code block of the code to be processed based on the parent class interface, specifically including:
[0028] Abstract the business data referenced in each logical judgment code block to obtain the parent class interface, and set a separate execution algorithm program for each parent class interface to obtain the processing data of the parent class interface;
[0029] According to the logic judgment code block business processing requirement reference the parent class interface processing data and generate the execution program for the business processing requirement.
[0030] In a second aspect, the embodiments of the present application provide a computer readable storage medium, which stores a computer program, and when the computer program is executed in a computer, the computer program causes the computer to execute the method for reducing code coupling based on abstract strategy.
[0031] Other features and advantages will be set forth in the descriptions that follow, and in part will be apparent from the description, or can be learned by practice of the application. The purposes and other advantages of the application will be realized and attained by the structure particularly pointed out in the written description and claims.
[0032] In order to make the above objectives, features and advantages of the present application more apparent, the following will specifically describe preferred embodiments, and combine with the accompanying drawings, and make detailed description as follows. BRIEF DESCRIPTION OF DRAWINGS
[0033] The above and other features and advantages of the present application will become more apparent by describing in detail exemplary embodiments thereof with reference to the attached drawings:
[0034] Figure 1 is a flowchart of a method for reducing code coupling based on abstract strategy;
[0035] Figure 2 is a flowchart of a method for determining the initial coupling assessment result of the code to be processed;
[0036] Figure 3 is a flowchart of a method for determining the coupling assessment result of the code block;
[0037] Figure 4 is a flowchart of a method for determining the uniformity of the data source of the reference business data of the code module according to the logical judgment of the data source of the reference business data of the code block;
[0038] Figure 5 is a flowchart of a method for determining the coupling assessment result of the code to be processed;
[0039] Figure 6 is an example diagram of coupling identification;
[0040] Figure 7 is an example diagram of code decoupling processing;
[0041] Figure 8 is a framework diagram of a computer readable storage medium. DETAILED DESCRIPTION
[0042] In order for those skilled in the art to better understand the technical solutions in the specification, the technical solutions in the specification will be clearly and completely described below in combination with the drawings in the specification. Obviously, the described embodiments are only a part of the embodiments of the specification, not all the embodiments. Based on the embodiments of the specification, all other embodiments obtained by those skilled in the art without creative labor should belong to the protection scope of the specification.
[0043] The applicant finds that in the prior art, due to the large use of if-else and other logical judgment statements, the coupling of the code is increased, and at the same time, great difficulty is brought to the maintenance and modification of the code. Therefore, in the decoupling processing of the code, only the coupling of the code is considered in the prior art, but the increase of the code amount caused by the decoupling processing is ignored, which also increases the difficulty of the maintenance of the code. Therefore, how to determine the differentiated decoupling measures by comprehensively considering the code amount and the coupling becomes a technical problem to be solved urgently.
[0044] To solve the above technical problems, the applicant mainly adopts the following technical solutions:
[0045] Firstly, the initial coupling evaluation result of the code is determined by the logical statements in the code and the number of logical judgment code blocks composed of different logical statements. Specifically, the initial coupling evaluation result can be determined by the number of logical statements in the code and the number of logical judgment code blocks composed of different logical statements in the preset number interval. When the initial coupling evaluation result of the code is large, the decoupling processing is directly performed. If the requirement is met, the next step is entered.
[0046] The coupling evaluation result of the different logical judgment code blocks is determined according to the reference business data source, the reference business data and the use number of the logical statements of the different logical judgment code blocks. In one possible embodiment, the coupling evaluation result under the reference business data source, the reference business data and the use number of the logical statements can be respectively determined by the proportion of the reference business data source, the reference business data and the use number of the logical statements to the set number. Then, the coupling evaluation result of the logical judgment code block is determined according to the weights of the three.
[0047] After the coupling evaluation result of the logical judgment code block is obtained, the coupling evaluation result of the code is determined according to the coupling evaluation result of the different logical judgment code blocks of the code and the initial coupling evaluation result of the code. Specifically, the coupling evaluation result of the code can be determined by the maximum value of the coupling evaluation result of the different logical judgment code blocks and the average value of the initial coupling evaluation result of the code.
[0048] The determination of the evaluation result of the code is performed by the coupling evaluation result of the code and the code amount, specifically, the determination of the evaluation result is performed by the coupling evaluation result of the code, the weight of the ratio of the code amount to the preset code amount, and when it is determined that the decoupling processing is needed according to the evaluation result, the reference business data of the logical judgment code block is abstracted to obtain a parent class interface, and the logical judgment code block of the to-be-processed code is decoupled according to the parent class interface.
[0049] The following will be described in detail from the method embodiment and the medium embodiment.
[0050] To solve the above problems, according to one aspect of the present application, as shown in the accompanying drawings, a method for reducing code coupling based on an abstract strategy is provided, which specifically comprises: Figure 1
[0051] S1 determines the logical statement of the to-be-processed code and the logical judgment code block composed of different logical statements by the matching result of the preset keyword and the to-be-processed code, determines the initial coupling evaluation result of the to-be-processed code based on the logical statement of the to-be-processed code and the logical judgment code block, and when the initial coupling evaluation result meets the requirement, proceeds to the next step.
[0052] It should be noted that the preset keyword includes if-else, if, switch-case, for, and while.
[0053] In one possible embodiment, as shown in the accompanying drawings, the method for determining the initial coupling evaluation result of the to-be-processed code in the above step S1 is: Figure 2
[0054] S11 determines the number of the logical statement of the to-be-processed code by the logical statement of the to-be-processed code, and determines whether the number of the logical statement of the to-be-processed code meets the requirement, if yes, proceeds to the next step, if not, proceeds to step S13.
[0055] S12 determines the number of the logical judgment code block of the to-be-processed code based on the logical judgment code block of the to-be-processed code, and determines whether the number of the logical judgment code block of the to-be-processed code meets the requirement, if yes, determines the initial coupling evaluation result of the to-be-processed code by the number of the logical judgment code block of the to-be-processed code, if not, proceeds to the next step.
[0056] S13 determines the code amount of the logical judgment code block of the to-be-processed code according to the logical judgment of the to-be-processed code, and determines the initial coupling evaluation result of the to-be-processed code in combination with the number of the logical judgment code blocks and the number of the logical statements.
[0057] In one possible embodiment, the basic coupling evaluation result is obtained by the code amount of the logical judgment code block of the to-be-processed code, the code coupling evaluation result of the to-be-processed code in the corresponding data interval is obtained according to the number interval in which the number of the logical judgment code blocks and the number of the logical statements are located, and finally the initial coupling evaluation result of the to-be-processed code is determined according to the number and of the basic coupling evaluation result and the code coupling evaluation result.
[0058] It should be further explained that the determination of whether the number of the logical judgment code blocks of the to-be-processed code meets the requirement in the step S12 specifically includes:
[0059] The code block number limit value of the to-be-processed code is determined by the code amount of the to-be-processed code, and whether the number of the logical judgment code blocks of the to-be-processed code meets the requirement is determined based on the code block number limit value of the to-be-processed code and the number of the logical judgment code blocks.
[0060] It can be understood that when the initial coupling evaluation result of the to-be-processed code does not meet the requirement, the decoupling processing is directly performed on the logical judgment code block of the to-be-processed code.
[0061] In the embodiment, the initial coupling evaluation result of the to-be-processed code is determined based on the logical statements and the logical judgment code blocks of the to-be-processed code, the code coupling of the to-be-processed code is evaluated from the number and type of the logical statements and the number of the logical judgment code blocks, the preliminary screening of the to-be-processed code with high code coupling is implemented, and the processing efficiency of the to-be-processed code with high coupling is improved.
[0062] S2 determines the reference business data source and the reference business data of the logical judgment code block based on the recognition result of the keyword of the logical judgment code block of the to-be-processed code, and determines the coupling evaluation result of the logical judgment code block in combination with the use data of the logical statement of the logical judgment code block;
[0063] In one possible embodiment, as shown in Figure 3 The method for determining the coupling evaluation result of the logical judgment code block in the step S2 is:
[0064] S21 determines the type of the logical statement of the logical judgment code block and the number of different types of logical statements based on the use data of the logical statement of the logical judgment code block, and determines the coupling evaluation coefficient of the logical judgment code block in combination with the code amount of the logical judgment code block;
[0065] S22 determines the number of the reference business data source of the logical judgment code block through the reference business data source of the logical judgment code block of the to-be-processed code, and determines whether the coupling evaluation coefficient of the logical judgment code block is accurate according to the number of the reference business data source, if yes, proceeds to the next step, if not, proceeds to step S24;
[0066] S23 determines the number of the reference business data of the logical judgment code block by using the reference business data of the logical judgment code block of the to-be-processed code, and determines whether the coupling evaluation coefficient of the logical judgment code block is accurate according to the number of the reference business data, if yes, determines the coupling evaluation result of the to-be-processed code through the coupling evaluation coefficient of the logical judgment code block, if not, proceeds to the next step;
[0067] S24 determines the data source uniformity of the reference business data of the logical judgment code module according to the data source of the reference business data of the logical judgment code block, and determines the data coupling coefficient of the logical judgment code block in combination with the number of the reference business data of the logical judgment code block and the number of the reference business data source, and determines the coupling evaluation result of the logical judgment code block through the data coupling coefficient and the coupling evaluation coefficient of the logical judgment code block.
[0068] In one possible embodiment, as shown in Figure 4 The determination of the data source uniformity of the reference business data of the logical judgment code module according to the data source of the reference business data of the logical judgment code block in step S24 specifically includes:
[0069] S241 determines the number of the reference business data of different reference business data sources according to the data source of the reference business data of the logical judgment code block, and determines the reference number of the reference business data of the reference business data source by using the number of the reference business data of different reference business data sources;
[0070] S242 determines the uniformity of different reference business data sources through the deviation amount of the number and the reference number of the reference business data of different reference business data sources, and determines the data source uniformity of the reference business data of the logical judgment code module based on the number of the uniformity of different reference business data sources.
[0071] In another possible implementation, the method for determining the coupling evaluation result of the logic judgment code block in step S2 is:
[0072] determining the uniformity of the data sources of the reference business data of the logic judgment code module according to the data sources of the reference business data of the logic judgment code block, and determining the data coupling coefficient of the logic judgment code block in combination with the number of the reference business data and the number of the reference business data sources;
[0073] when the number of the logic statements of the logic judgment code block is less than the preset number of logic statements:
[0074] determining the coupling evaluation result of the to-be-processed code by the data coupling coefficient of the logic judgment code block;
[0075] when the number of the logic statements of the logic judgment code block is not less than the preset number of logic statements:
[0076] determining the type of the logic statements and the number of the logic statements of different types of the logic judgment code block based on the usage data of the logic statements of the logic judgment code block, and determining the coupling evaluation coefficient of the logic judgment code block in combination with the code amount of the logic judgment code block;
[0077] determining the coupling evaluation result of the logic judgment code block by the data coupling coefficient and the coupling evaluation coefficient of the logic judgment code block.
[0078] In this embodiment, the coupling evaluation result of the logic judgment code block is determined by combining the reference business data sources, the reference business data and the usage data of the logic statements, which not only considers the difference in coupling of different logic judgment code blocks due to the difference in reference business data sources and reference business data, but also considers the difference in coupling of different logic judgment code blocks due to the difference in usage data of the logic statements, thereby ensuring the accuracy of the coupling evaluation result of the logic judgment code block.
[0079] S3 obtains the coupling evaluation results of different logic judgment code blocks of the to-be-processed code, and determines the coupling evaluation result of the to-be-processed code in combination with the initial coupling evaluation result of the to-be-processed code;
[0080] In one possible implementation, the method for determining the coupling evaluation result of the to-be-processed code in step S3 is:
[0081] determining the complex coupling code blocks in the to-be-processed code according to the coupling evaluation results of the different logical judgment code blocks in the to-be-processed code;
[0082] when the complex coupling code blocks exist in the to-be-processed code:
[0083] determining the coupling compensation amount of the complex coupling code blocks in the to-be-processed code according to the proportion of the complex coupling code blocks in the to-be-processed code, the proportion of the code amount of the complex coupling code blocks in the to-be-processed code, and the proportion of the number of the logical statements of the complex coupling code blocks in the to-be-processed code;
[0084] when the coupling compensation amount of the complex coupling code blocks in the to-be-processed code and the coupling evaluation results of the complex coupling code blocks both meet the requirements:
[0085] determining the coupling evaluation results of the to-be-processed code according to the coupling compensation amount of the complex coupling code blocks in the to-be-processed code and the initial coupling evaluation results of the to-be-processed code;
[0086] when the coupling compensation amount of the complex coupling code blocks in the to-be-processed code and the coupling evaluation results of the complex coupling code blocks do not meet the requirements:
[0087] determining the weights of the different logical judgment code blocks according to the coupling evaluation results of the logical judgment code blocks in the to-be-processed code, and determining the coupling evaluation results of the to-be-processed code according to the sum of the weights of the coupling judgment code blocks in the to-be-processed code, the coupling compensation amount of the complex coupling code blocks in the to-be-processed code, and the initial coupling evaluation results of the to-be-processed code;
[0088] when the complex coupling code blocks do not exist in the to-be-processed code:
[0089] determining the coupling evaluation results of the to-be-processed code according to the initial coupling evaluation results of the to-be-processed code.
[0090] It can be understood that the preset coupling degree in the above steps is determined according to the number of the reference business data sources of the to-be-processed code, wherein the more the number of the reference business data sources of the to-be-processed code, the smaller the preset coupling degree.
[0091] In another possible embodiment, as shown in Figure 5 the method for determining the coupling evaluation results of the to-be-processed code in the step S3 is:
[0092] S31 obtains the number of coupling evaluation results of the logical judgment code blocks in the to-be-processed code and the maximum value of the coupling evaluation results, and determines whether the number of coupling evaluation results of the logical judgment code blocks in the to-be-processed code and the maximum value of the coupling evaluation results both meet the requirements, if yes, proceeds to the next step, if no, proceeds to step S35;
[0093] S32 determines the complex coupling code blocks in the to-be-processed code through the coupling evaluation results of different logical judgment code blocks in the to-be-processed code, and determines whether the number of the complex coupling code blocks in the to-be-processed code meets the requirements, if yes, proceeds to the next step, if no, proceeds to step S35;
[0094] S33 determines the coupling compensation amount of the complex coupling code blocks in the to-be-processed code through the proportion of the number of the complex coupling code blocks in the to-be-processed code to the number of the logical judgment code blocks in the to-be-processed code, the proportion of the code amount of the complex coupling code blocks in the to-be-processed code to the to-be-processed code, and the proportion of the number of the logical statements of the complex coupling code blocks in the to-be-processed code to the to-be-processed code;
[0095] S34 determines whether the coupling compensation amount of the complex coupling code blocks in the to-be-processed code meets the requirements, if yes, determines the coupling evaluation result of the to-be-processed code through the coupling compensation amount of the complex coupling code blocks in the to-be-processed code and the initial coupling evaluation result of the to-be-processed code, if no, proceeds to the next step;
[0096] S35 determines the weight of different logical judgment code blocks through the coupling evaluation results of the logical judgment code blocks in the to-be-processed code, and determines the coupling evaluation result of the to-be-processed code according to the sum of the weights of the coupling judgment code blocks in the to-be-processed code, the coupling compensation amount of the complex coupling code blocks in the to-be-processed code, and the initial coupling evaluation result of the to-be-processed code.
[0097] S4 determines the evaluation result of the to-be-processed code according to the coupling evaluation result and the code amount of the to-be-processed code, and when the evaluation result does not meet the requirements, abstracts the reference business data of the logical judgment code blocks to obtain a parent class interface, and decouples the logical judgment code blocks of the to-be-processed code according to the parent class interface.
[0098] In one possible embodiment, the method for determining the evaluation result of the to-be-processed code in step S4 is:
[0099] when the coupling evaluation result of the to-be-processed code is greater than the preset code coupling degree:
[0100] determining that the evaluation result of the to-be-processed code does not meet the requirement, and determining the evaluation result of the to-be-processed code through the coupling evaluation result of the to-be-processed code;
[0101] when the coupling evaluation result of the to-be-processed code is not greater than the preset code coupling degree:
[0102] obtaining the code amount of the to-be-processed code, and determining that the code amount of the to-be-processed code meets the requirement according to the code amount of the to-be-processed code and the preset maximum limited code amount:
[0103] determining that the evaluation result of the to-be-processed code does not meet the requirement, and determining the evaluation result of the to-be-processed code through the code amount of the to-be-processed code;
[0104] when the code amount of the to-be-processed code does not meet the requirement according to the code amount of the to-be-processed code and the preset maximum limited code amount:
[0105] determining the evaluation result correction amount through the ratio of the code amount of the to-be-processed code to the preset maximum limited code amount, and determining the evaluation result of the to-be-processed code according to the evaluation result correction amount and the coupling evaluation result of the to-be-processed code.
[0106] It should be noted that the decoupling processing of the logic judgment code block of the to-be-processed code according to the parent interface includes:
[0107] abstracting each reference business data of the logic judgment code block to obtain a parent interface, and setting a separate execution algorithm program for each parent interface to obtain processing data of the parent interface;
[0108] referencing the processing data of the parent interface according to the business processing demand of the logic judgment code block, and generating an execution program for the business processing demand.
[0109] In this embodiment, the determination of the evaluation result of the to-be-processed code according to the coupling evaluation result and the code amount of the to-be-processed code realizes the determination of the evaluation result from the complexity and coupling of the code amount, and lays a foundation for further realizing the differentiated decoupling processing of the to-be-processed code.
[0110] The following gives a specific embodiment in combination with a specific business scenario:
[0111] In the pre-loan activity, in order to make an appointment with the customer before the customer's loan is settled, and automatically renew a loan on the day of the repayment day after the loan is settled, lock the customer's loan demand, improve the customer's daily loan rate, and promote the balance growth; At the same time, through the automatic loan function, the high demand customer can operate independently, release manpower, so that manpower can focus more on low loan willingness customers. In the code construction process, it is found that there are a large number of coupling codes, which make the test complexity and code maintenance difficult.
[0112] Therefore, in order to solve the above problems, the following scheme is adopted:
[0113] First, the preset keywords such as if-else, if are used to realize the identification of the logic statements of the to-be-processed code, and the identification results of the brackets of the code blocks where the logic statements are located are used to realize the determination of the logic judgment code blocks composed of different logic statements, and the initial coupling evaluation result of the to-be-processed code is determined based on the logic statements and the logic judgment code blocks. The initial coupling evaluation result can be determined by the number of logic judgment code blocks with more than 3 logic statements.
[0114] Then, the coupling evaluation results under the use quantity of the reference business data source, the reference business data and the logic statement are determined by the proportion of the use quantity of the reference business data source, the reference business data and the logic statement to the set quantity respectively, and the coupling evaluation results of the logic judgment code blocks are determined according to the weights of the three. For example, as shown in Figure 6 , it is an example diagram of coupling identification in the present application;
[0115] Finally, the coupling evaluation result of the to-be-processed code is determined by the maximum value of the normalized evaluation result of the coupling evaluation result of the different logic judgment code blocks of the to-be-processed code and the initial coupling evaluation result of the to-be-processed code.
[0116] When the evaluation result of the to-be-processed code is determined according to the coupling evaluation result of the to-be-processed code and the code amount, and when the evaluation result does not meet the requirements, the reference business data of the logic judgment code block is abstracted to obtain a parent class interface, and the logic judgment code block of the to-be-processed code is decoupled according to the parent class interface. For example, as shown in Figure 7 , it is an example diagram of code decoupling processing using the present application.
[0117] On the other hand, as shown in Figure 8 , the present application provides a computer readable storage medium having a computer program stored thereon, which, when executed in a computer, causes the computer to execute the above-mentioned method for reducing code coupling based on an abstract strategy.
[0118] The code coupling reduction method based on the abstract strategy comprises the following steps.
[0119] The logic statement of the to-be-processed code and the logic judgment code block composed of different logic statements are determined according to the matching result of the preset keyword and the to-be-processed code, and the initial coupling evaluation result of the to-be-processed code is determined based on the logic statement of the to-be-processed code and the logic judgment code block, and when the initial coupling evaluation result meets the requirement, the next step is entered.
[0120] The reference business data source and the reference business data of the logic judgment code block are determined based on the recognition result of the keyword of the logic judgment code block of the to-be-processed code, and the coupling evaluation result of the logic judgment code block is determined in combination with the use data of the logic statement of the logic judgment code block.
[0121] The complex coupling code block in the to-be-processed code whose coupling evaluation result is greater than a preset coupling degree is determined through the coupling evaluation results of different logic judgment code blocks of the to-be-processed code.
[0122] When the to-be-processed code contains the complex coupling code block:
[0123] The coupling compensation amount of the complex coupling code block in the to-be-processed code is determined through the proportion of the number of the complex coupling code block in the to-be-processed code to the logic judgment code block in the to-be-processed code, the proportion of the code amount of the complex coupling code block in the to-be-processed code to the to-be-processed code, and the proportion of the number of the logic statement of the complex coupling code block in the to-be-processed code to the to-be-processed code.
[0124] When the coupling compensation amount of the complex coupling code block in the to-be-processed code and the coupling evaluation result of the complex coupling code block both meet the requirement:
[0125] The coupling evaluation result of the to-be-processed code is determined through the coupling compensation amount of the complex coupling code block in the to-be-processed code and the initial coupling evaluation result of the to-be-processed code.
[0126] When either the coupling compensation amount of the complex coupling code block in the to-be-processed code or the coupling evaluation result of the complex coupling code block does not meet the requirement:
[0127] The coupling evaluation result of the to-be-processed code is determined by the weight determination of different logical judgment code blocks according to the coupling judgment code block weight in the to-be-processed code, the coupling compensation amount of the complex coupling code block in the to-be-processed code, and the initial coupling evaluation result of the to-be-processed code.
[0128] When the to-be-processed code does not contain a complex coupling code block:
[0129] The coupling evaluation result of the to-be-processed code is determined by the initial coupling evaluation result of the to-be-processed code.
[0130] The evaluation result of the to-be-processed code is determined according to the coupling evaluation result of the to-be-processed code and the code amount, and when the evaluation result does not meet the requirement, the reference business data of the logical judgment code block is abstracted to obtain a parent class interface, and the logical judgment code block of the to-be-processed code is decoupled according to the parent class interface.
[0131] Each of the embodiments in the specification is described in a progressive manner, and the same or similar parts of each of the embodiments can be referred to each other. Each of the embodiments focuses on the difference from other embodiments. In particular, for the device, equipment, and non-volatile computer storage medium embodiments, since they are basically similar to the method embodiments, the description is relatively simple, and the relevant parts can be referred to the part of the method embodiment.
[0132] The above describes specific embodiments of the specification. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recited in the claims can be performed in an order different than the order in the embodiments and still achieve the desired result. In addition, the processes depicted in the figures do not necessarily require the particular order shown or sequential order to achieve the desired results. In certain implementations, multitasking and parallel processing can be advantageous or possible.
[0133] The above only describes one or more embodiments of the specification and does not limit the specification. One or more embodiments of the specification can have various modifications and changes for those skilled in the art. Any modification, equivalent replacement, improvement, etc. within the spirit and principles of one or more embodiments of the specification should be included in the scope of the claims of the specification.
Claims
1. A method for reducing code coupling based on abstraction policy, the method comprising: Specifically comprising: Determine the logical statement of the to-be-processed code and the logical judgment code block composed of different logical statements by matching the preset keyword with the to-be-processed code, determine the initial coupling evaluation result of the to-be-processed code based on the logical statement of the to-be-processed code and the logical judgment code block, and when the initial coupling evaluation result meets the requirement, proceed to the next step; Determine the referenced business data source and the referenced business data of the logical judgment code block based on the identification result of the keyword of the logical judgment code block of the to-be-processed code, and determine the coupling evaluation result of the logical judgment code block in combination with the usage data of the logical statement of the logical judgment code block; Obtain the coupling evaluation results of different logical judgment code blocks of the to-be-processed code, and determine the coupling evaluation result of the to-be-processed code in combination with the initial coupling evaluation result of the to-be-processed code; Determine the evaluation result of the to-be-processed code according to the coupling evaluation result of the to-be-processed code and the code amount, and when the evaluation result does not meet the requirement, perform abstract processing on the referenced business data of the logical judgment code block to obtain a parent class interface, and perform decoupling processing on the logical judgment code block of the to-be-processed code according to the parent class interface; The method for determining the coupling evaluation result of the logical judgment code block is: S21 Determine the type of the logical statement of the logical judgment code block and the number of different types of logical statements based on the usage data of the logical statement of the logical judgment code block, and determine the coupling evaluation coefficient of the logical judgment code block in combination with the code amount of the logical judgment code block; S22 Determine the number of the referenced business data source of the logical judgment code block through the referenced business data source of the logical judgment code block of the to-be-processed code, and determine whether the coupling evaluation coefficient of the logical judgment code block is accurate according to the number of the referenced business data source, if yes, proceed to the next step, if not, proceed to step S24; S23 Determine the number of the referenced business data of the logical judgment code block by using the referenced business data of the logical judgment code block of the to-be-processed code, and determine whether the coupling evaluation coefficient of the logical judgment code block is accurate according to the number of the referenced business data, if yes, determine the coupling evaluation result of the to-be-processed code by using the coupling evaluation coefficient of the logical judgment code block, if not, proceed to the next step; S24 Determine the data source uniformity of the referenced business data of the logical judgment code block according to the data source of the referenced business data of the logical judgment code block, and determine the data coupling coefficient of the logical judgment code block in combination with the number of the referenced business data of the logical judgment code block and the number of the referenced business data source, and determine the coupling evaluation result of the logical judgment code block by using the data coupling coefficient and the coupling evaluation coefficient of the logical judgment code block; The method for determining the coupling evaluation result of the to-be-processed code is: determining the complex coupling code blocks in the to-be-processed code through the coupling evaluation results of the different logical judgment code blocks in the to-be-processed code; when the to-be-processed code contains complex coupling code blocks: determining the coupling compensation amount of the complex coupling code blocks in the to-be-processed code through the proportion of the number of the complex coupling code blocks in the to-be-processed code to the number of the logical judgment code blocks in the to-be-processed code, the proportion of the code amount of the complex coupling code blocks in the to-be-processed code to the to-be-processed code, and the proportion of the number of the logical statements of the complex coupling code blocks in the to-be-processed code to the to-be-processed code; when both the coupling compensation amount of the complex coupling code blocks in the to-be-processed code and the coupling evaluation results of the complex coupling code blocks meet the requirements: determining the coupling evaluation results of the to-be-processed code through the coupling compensation amount of the complex coupling code blocks in the to-be-processed code and the initial coupling evaluation results of the to-be-processed code; when either the coupling compensation amount of the complex coupling code blocks in the to-be-processed code or the coupling evaluation results of the complex coupling code blocks does not meet the requirements: determining the weights of the different logical judgment code blocks through the coupling evaluation results of the logical judgment code blocks in the to-be-processed code, and determining the coupling evaluation results of the to-be-processed code according to the sum of the weights of the coupling judgment code blocks in the to-be-processed code, the coupling compensation amount of the complex coupling code blocks in the to-be-processed code, and the initial coupling evaluation results of the to-be-processed code; when the to-be-processed code does not contain complex coupling code blocks: determining the coupling evaluation results of the to-be-processed code through the initial coupling evaluation results of the to-be-processed code.
2. The abstract policy based decoupling method of claim 1, wherein, The preset keywords include if-else, if, switch-case, for, and while.
3. The abstract policy based decoupling of code method of claim 1, wherein, The method for determining the initial coupling evaluation results of the to-be-processed code includes: S11, determining the number of the logical statements of the to-be-processed code through the logical statements of the to-be-processed code, and determining whether the number of the logical statements of the to-be-processed code meets the requirements, if yes, proceeding to the next step, and if no, proceeding to step S13; S12, determining the number of the logical judgment code blocks of the to-be-processed code based on the logical judgment code blocks of the to-be-processed code, and determining whether the number of the logical judgment code blocks of the to-be-processed code meets the requirements, if yes, determining the initial coupling evaluation results of the to-be-processed code through the number of the logical judgment code blocks of the to-be-processed code, and if no, proceeding to the next step; S13, determining the code amount of the logical judgment code blocks of the to-be-processed code according to the logical judgment code blocks of the to-be-processed code, and determining the initial coupling evaluation results of the to-be-processed code in combination with the number of the logical judgment code blocks and the number of the logical statements of the to-be-processed code.
4. The abstract policy based decoupling of code method of claim 3, wherein, The method for determining whether the number of the logical judgment code blocks of the to-be-processed code meets the requirements includes: The determination of the code block quantity limit value of the to-be-processed code is performed according to the code quantity of the to-be-processed code, and whether the number of logical judgment code blocks of the to-be-processed code meets the requirement is determined based on the code block quantity limit value of the to-be-processed code and the number of logical judgment code blocks.
5. The abstract policy based decoupling of code method of claim 1 wherein, When the initial coupling evaluation result of the to-be-processed code does not meet the requirement, the decoupling processing is directly performed on the logical judgment code blocks of the to-be-processed code.
6. The abstract policy based decoupling of code method of claim 1, wherein, The determination of the data source uniformity of the reference business data of the logical judgment code blocks is performed according to the data sources of the reference business data of the logical judgment code blocks, and specifically includes: The determination of the number of reference business data of different reference business data sources is performed according to the data sources of the reference business data of the logical judgment code blocks, and the determination of the reference business data benchmark quantity of the reference business data sources is performed according to the number of reference business data of different reference business data sources; The determination of the uniformity of different reference business data sources is performed through the deviation amount of the number of reference business data of different reference business data sources and the reference business data benchmark quantity, and the determination of the data source uniformity of the reference business data of the logical judgment code blocks is performed based on the number of uniformities of different reference business data sources.
7. The abstract policy based decoupling of code method of claim 1, wherein, The preset coupling degree is determined according to the number of reference business data sources of the to-be-processed code, and the more the number of reference business data sources of the to-be-processed code, the smaller the preset coupling degree.
8. The abstract policy based decoupling of code method of claim 1, wherein, The determination method of the evaluation result of the to-be-processed code is: When the coupling evaluation result of the to-be-processed code is greater than the preset code coupling degree: It is determined that the evaluation result of the to-be-processed code does not meet the requirement, and the determination of the evaluation result of the to-be-processed code is performed through the coupling evaluation result of the to-be-processed code; When the coupling evaluation result of the to-be-processed code is not greater than the preset code coupling degree: The code quantity of the to-be-processed code is obtained, and it is determined that the code quantity of the to-be-processed code meets the requirement according to the code quantity of the to-be-processed code and the preset maximum code limit quantity: It is determined that the evaluation result of the to-be-processed code does not meet the requirement, and the determination of the evaluation result of the to-be-processed code is performed through the code quantity of the to-be-processed code; When it is determined that the code quantity of the to-be-processed code does not meet the requirement according to the code quantity of the to-be-processed code and the preset maximum code limit quantity: The determination of the evaluation result correction quantity is performed through the ratio of the code quantity of the to-be-processed code and the preset maximum code limit quantity, and the determination of the evaluation result of the to-be-processed code is performed according to the evaluation result correction quantity and the coupling evaluation result of the to-be-processed code. 9.A computer readable storage medium having stored thereon a computer program which, when executed in a computer, causes the computer to perform the method of any one of claims 1-8.
Citation Information
Patent Citations
Method and device for solving if-else coupling and high redundancy in code
CN114237570A
Code processing method and device, equipment, medium and program product
CN118444973A