Code maintenance method, device, equipment, storage medium and program product
By breaking down the code into code blocks and calculating information density values, the problem that complexity assessment tools in existing technologies cannot accurately reflect the actual maintenance cost of the code is solved, achieving more accurate code complexity assessment and maintenance guidance, and improving code maintainability.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- INDUSTRIAL AND COMMERCIAL BANK OF CHINA
- Filing Date
- 2026-03-17
- Publication Date
- 2026-07-31
AI Technical Summary
Existing complexity assessment tools cannot accurately reflect the actual maintenance cost of code, mainly because they ignore the cognitive load implicit in the code, such as the information density of lines of code, the density of variables and operators, and the working memory capacity limitations of developers when reading the code.
The complexity of the code is quantified by splitting the target code into multiple code blocks and determining the information density value based on the number of variables, operators, method call information density value, and the number of characters exceeding the display threshold in each code block. This is combined with the number of shared variables and executable paths between code blocks.
It improves the accuracy of code complexity assessment, enabling more precise identification of code segments with high maintenance costs, guiding developers to perform targeted optimizations, and improving code maintainability.
Smart Images

Figure CN122489111A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of financial technology or other related fields, and in particular to a code maintenance method, apparatus, device, storage medium and program product. Background Technology
[0002] In software development and maintenance, code maintainability directly impacts development efficiency and system stability. As the software lifecycle progresses, codebases gradually accumulate redundant logic, repetitive structures, and complex control flows, leading to a significant cognitive burden for developers when reading, debugging, and refactoring code. For example, in large enterprise applications or open-source projects, developers frequently need to analyze unfamiliar code modules, and the higher the code complexity, the greater the time and effort required to understand it.
[0003] Existing complexity assessment tools mainly quantify complexity by counting the number of branches or nesting levels in the code. However, these metrics only focus on the structural complexity of the code, which makes it impossible for existing tools to accurately reflect the actual maintenance cost of the code. Summary of the Invention
[0004] This application provides a code maintenance method, apparatus, device, storage medium, and program product to solve the technical problem of low efficiency in code maintenance.
[0005] In a first aspect, this application provides a code maintenance method, comprising: splitting the target code into multiple code blocks according to the code structure of the target code; determining the information density value of the first code block according to the code parameters of the first code block, wherein the code parameters include the number of variables, the number of operators, the information density value of the called methods, and the number of characters exceeding the display threshold, wherein the first code block is any one of the multiple code blocks, and the information density value of the called methods is the information density value of all methods called by the first code block; determining the complexity of the target code according to the information density value of each code block in the multiple code blocks; and determining the corresponding code maintenance operation according to the complexity of the target code.
[0006] In one possible implementation of the first aspect, determining the information density value of the first code block based on its code parameters includes: determining the information density value of each code line in the first code block based on its code parameters, wherein the code parameters of each code line include the number of variables, the number of operators, the information density value of the called method, and the number of characters exceeding the display threshold, and the information density value of the called method is determined based on the number of variables, the number of operators, and the number of characters exceeding the display threshold of the called method; and determining the information density value of the first code block based on the information density value of each code line in the first code block and the number of shared variables between each code line and adjacent code lines.
[0007] In one possible implementation of the first aspect, the information density value of each code line is determined based on the code parameters of each code line in the first code block, including: determining the sum of the number of variables, the number of operators, the method information density value, and the number of characters exceeding the display threshold for each code line as the information density value of each code line, and the method information density value as the sum of the information density values of all methods called in each code line.
[0008] In one possible implementation of the first aspect, determining the information density value of the first code block based on the information density value of each line of code in the first code block and the number of shared variables between each line of code and its adjacent lines of code includes: determining the sum of the information density values of each line of code in the first code block as a first parameter; determining the sum of the number of shared variables between each line of code in the first code block and the next line of code as a second parameter; and determining the value of the first parameter minus the second parameter as the information density value of the first code block.
[0009] In one possible implementation of the first aspect, splitting the target code into multiple code blocks according to the code structure of the target code includes: splitting the target code into multiple code blocks according to the target identifier; and / or, determining at least one conditional statement code in the target code as at least one code block; and / or, determining at least one code block according to the separated code content in nested code.
[0010] In one possible implementation of the first aspect, determining the complexity of the target code based on the information density value of each code block in a plurality of code blocks includes: determining all executable paths of the target code; determining the information density value of each executable path based on the information density value of at least one code block corresponding to each executable path in all executable paths; determining the information density value of the first executable path as the complexity of the target code, wherein the first executable path is the executable path with the largest information density value among all executable paths.
[0011] In one possible implementation of the first aspect, determining the information density value of each executable path based on the information density value of at least one code block corresponding to each executable path includes: determining the weight of each code block based on the code block association relationship between at least one code block corresponding to each executable path; and determining the information density value of each executable path based on the weight of each code block corresponding to each executable path and the information density value of each code block.
[0012] In one possible implementation of the first aspect, the display threshold is determined based on the character display capacity of the code display device.
[0013] Secondly, this application provides a code maintenance apparatus, comprising:
[0014] Module splitting is used to split target code into multiple code blocks based on the target code's code structure.
[0015] The first determining module is used to determine the information density value of the first code block based on the code parameters of the first code block. The code parameters include the number of variables, the number of operators, the information density value of the called method, and the number of characters exceeding the display threshold. The first code block is any one of multiple code blocks, and the information density value of the called method is the information density value of all methods called by the first code block.
[0016] The second determining module is used to determine the complexity of the target code based on the information density value of each code block in multiple code blocks;
[0017] The maintenance module is used to determine the corresponding code maintenance operations based on the complexity of the target code.
[0018] Thirdly, this application provides an electronic device, including: a processor and a memory communicatively connected to the processor;
[0019] The memory stores the instructions that the computer executes;
[0020] The processor executes computer-executable instructions stored in memory to implement any of the methods of the first aspect.
[0021] Fourthly, this application provides a computer-readable storage medium storing computer-executable instructions, which, when executed by a processor, are used to implement the method of any one of the first aspects.
[0022] Fifthly, this application provides a computer program product, including a computer program that, when executed by a processor, implements the method of any one of the first aspects.
[0023] The code maintenance method provided in this application improves the accuracy of code complexity assessment by introducing code block-level information density analysis and recursive integration path complexity, thus providing accurate data support for code maintenance. Through code block-level statistics, high-risk code segments that developers frequently need to switch attention during maintenance can be directly identified, avoiding assessment bias caused by overly coarse statistical granularity. Quantifying information density values ensures that the assessment results are consistent with the actual cognitive load of developers; for example, high-information-density code blocks, even with simple branch structures, can be identified as high-maintenance-cost code segments. By analyzing the total information load under the code execution path, the multi-dimensional information switching costs that developers need to handle during actual maintenance can be reflected; for example, code blocks that frequently switch within loops or conditional branches will be marked as high-risk. Attached Figure Description
[0024] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this application and, together with the description, serve to explain the principles of this application.
[0025] Figure 1 A schematic diagram illustrating a code maintenance method provided in an embodiment of this application;
[0026] Figure 2 A flowchart illustrating a code maintenance method provided in an embodiment of this application;
[0027] Figure 3 This is a schematic diagram of the structure of a code maintenance device provided in an embodiment of this application;
[0028] Figure 4 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application.
[0029] The accompanying drawings illustrate specific embodiments of this application, which will be described in more detail below. These drawings and descriptions are not intended to limit the scope of the concept in any way, but rather to illustrate the concept of this application to those skilled in the art through reference to particular embodiments. Detailed Implementation
[0030] Exemplary embodiments will now be described in detail, examples of which are illustrated in the accompanying drawings. When the following description relates to the drawings, unless otherwise indicated, the same numbers in different drawings denote the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with this application. Rather, they are merely examples of apparatuses and methods consistent with some aspects of this application as detailed in the appended claims.
[0031] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, data stored, data displayed, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties. Furthermore, the collection, storage, use, processing, transmission, provision, disclosure, and application of the relevant data all comply with the relevant laws, regulations, and standards of the relevant countries and regions, have taken necessary confidentiality measures, do not violate public order and good morals, and provide corresponding operation access points for users to choose to authorize or refuse.
[0032] Furthermore, the technical solution involved in this application, which involves big data analysis of user information (including but not limited to personal biometrics, identity data, consumption data, asset data, electronic terminal operation data, etc.) and the use of artificial intelligence technology for automated decision-making, and makes decisions that have a significant impact on personal rights based on the results of automated decision-making, provides users with corresponding operation entry points for users to choose to agree to or reject the results of automated decision-making; if the user chooses to reject, the process will proceed to the expert decision-making process.
[0033] It should be noted that the code maintenance methods, apparatus, equipment, storage media and products provided in this application can be used in the fields of financial technology and related fields, as well as in any field other than financial technology and related fields. The application fields of the code maintenance methods, apparatus, equipment, storage media and products in this application are not limited.
[0034] Figure 1 This is a schematic diagram illustrating a scenario where the code maintenance method of this application is applied. For example... Figure 1 As shown, the customer can interact with the electronic device, which can execute the code maintenance method of this application embodiment to provide interactive services to the user. The electronic device can also interact with the server to obtain relevant data required to execute the code maintenance method of this application embodiment.
[0035] In software development and maintenance, code maintainability directly impacts development efficiency and system stability. As the software lifecycle progresses, codebases gradually accumulate redundant logic, repetitive structures, and complex control flows, leading to a significant cognitive burden for developers when reading, debugging, and refactoring code. For example, in large enterprise applications or open-source projects, developers frequently need to analyze unfamiliar code modules, and the higher the code complexity, the greater the time and effort required to understand it.
[0036] Existing complexity assessment tools (such as cyclomatic complexity and cognitive complexity) primarily quantify complexity by counting the number of branches or nesting levels in the code. However, these metrics only focus on the structural complexity of the code, ignoring the implicit cognitive load, such as the information density of code lines, the density of variables and operators, and the working memory capacity limitations of developers when reading the code. This limitation prevents existing tools from accurately reflecting the actual maintenance cost of the code.
[0037] The code maintenance methods, apparatus, equipment, storage media, and products provided in this application are intended to solve the above-mentioned technical problems of the prior art.
[0038] The technical solution of this application and how the technical solution of this application solves the above-mentioned technical problems are described in detail below with specific embodiments. These specific embodiments can be combined with each other, and the same or similar concepts or processes may not be described again in some embodiments. The embodiments of this application will be described below with reference to the accompanying drawings.
[0039] Figure 2 This is a flowchart illustrating a code maintenance method provided in an embodiment of this application, such as... Figure 2 As shown, the method includes:
[0040] S201. The electronic device splits the target code into multiple code blocks according to the code structure of the target code.
[0041] Here, "code block" refers to an independent logical unit in the target code that is isolated by syntactic structures (such as {}, if / else, etc.). For example, A and B in if (A) { B;}.
[0042] S202. The electronic device determines the information density value of the first code block based on the code parameters of the first code block. The code parameters include the number of variables, the number of operators, the information density value of the called method, and the number of characters exceeding the display threshold. The information density value of the called method is the information density value of all methods called by the first code block. The first code block is any one of multiple code blocks.
[0043] The information density value is an indicator that quantifies the working memory load of a code block on a developer. It can be determined based on parameters such as the number of variables, the number of operators, the information density value of the called methods, and the number of characters exceeding the display threshold. In addition, it can be determined based on other parameters, such as the number of called methods, etc., but specific details are not limited here.
[0044] For example, a line of code containing multiple variable declarations has a higher information density value.
[0045] The number of variables is the total number of variables declared or used within the code block. For example, in `int a = 1, b = 2`, the number of variables is 2.
[0046] The operator count is the total number of operators used in a code block (such as +, -, =, ==, etc.). For example, in a = b + c; the number of operators is 2.
[0047] The method call count is the total number of independent methods called within a code block. For example, in `result = func1(func2(a,b), func3(c, d));`, the method call count is 3.
[0048] The display threshold is the number of characters that can be displayed in the code.
[0049] S203. The electronic device determines the complexity of the target code based on the information density value of each code block in multiple code blocks.
[0050] After determining the information density value of each code block, the electronic device can determine the complexity of the target code based on the information density values of all code blocks.
[0051] S204. The electronic device determines the corresponding code maintenance operation based on the complexity of the target code.
[0052] For example, the complexity can be normalized. Then, for low-complexity code with a complexity below 0.2, normal development can be carried out without maintenance. For low-complexity code with a complexity in the range of [0.2, 0.5], operations such as adding comments, supplementing tests, and focusing on reviews can be performed. For low-complexity code with a complexity in the range of (0.5, 0.8), operations such as splitting methods, prohibiting the addition of new logic, and forcing tests can be performed. For low-complexity code with a complexity in the range of [0.8, 1], operations such as freezing modifications and forcing refactoring / rewriting can be performed.
[0053] For example, an electronic device can first split the target code into multiple code blocks, such as splitting `if (A) { B;}` into two independent code blocks, A and B. Then, an information density value is calculated for each code block. This information density value is determined by statistically analyzing the information density values of variables, operators, and method calls within the code block, as well as the number of characters exceeding a display threshold. For instance, when a line of code contains multiple variable declarations (such as `int a = 1, b = 2, c = 3;`), both the number of variables and the number of characters exceeding the display threshold will affect the information density value. Finally, the complexity of the target code is integrated based on the information density burden values of each code block. This entire process combines code structure analysis and psychological models (such as Miller's Law) to correlate the complexity of code blocks with human cognitive load, forming a complexity assessment result, and determining the corresponding code maintenance operations accordingly.
[0054] This possible implementation addresses the complexity assessment bias in existing technologies caused by a lack of consideration of human working memory characteristics by breaking down code into blocks and calculating information density values. It quantifies the impact of variables, operators, method calls, and display thresholds within code blocks on the cognitive load of developers. For example, when evaluating two code methods with the same number of branches, if one method contains a large number of dense variable declarations or complex expressions (e.g., `int a = 1, b = 2, c = 3, d = 4, e = 5;`), its information density burden value will be significantly higher than that of a structurally simpler method (e.g., `int a = 1;`), thus generating a complexity score that better reflects the actual maintenance costs for developers. By refining the assessment to the code block level and incorporating cognitive load modeling, the complexity assessment results more accurately reflect the maintainability of the code, guiding developers to perform targeted optimizations and significantly improving code maintainability.
[0055] In some embodiments, the electronic device determines the information density value of the first code block based on the code parameters of the first code block, which may specifically include:
[0056] The electronic device determines the information density value of each code line based on the code parameters of each code line in the first code block. The code parameters of each code line include the number of variables, the number of operators, the information density value of the called method, and the number of characters exceeding the display threshold. The information density value of the called method is determined based on the number of variables, the number of operators, and the number of characters exceeding the display threshold of the called method.
[0057] The electronic device determines the information density value of the first code block based on the information density value of each line of code in the first code block and the number of shared variables between each line of code and its adjacent lines of code.
[0058] After splitting the code into blocks, for each line of code within each block, the following information is first calculated: the number of variables, the number of operators, the information density of method calls, and the number of characters exceeding the display threshold. For example, in `int a = 1, b = 2`, the number of variables is 2, the number of operators is 1 (the assignment operator `=`), and the number of method calls is 0. Then, the number of characters exceeding the display threshold is calculated; for example, if a line of code contains 210 characters, the excess is 10 characters. Finally, the above statistical results are summed to obtain the information density value of each line of code. Finally, based on the information density value of each line of code in the first code block and the number of shared variables between each line and adjacent lines, the information density value of the first code block is determined. By quantifying the syntactic element density of code lines and the limitations of code display, a complexity index directly related to human working memory load is formed.
[0059] This possible implementation achieves quantitative modeling of the density of syntax elements and code display constraints within a line of code by refining the calculation steps for information density values. For example, when evaluating a line of code containing numerous variable declarations or complex operators (such as `a = b + c * d / e;`), its information density burden value will be significantly higher than that of a concise line of code (such as `a = b;`). By statistically analyzing syntax elements and code overflow factors step by step, the complexity assessment results more accurately reflect the impact of lines of code on the cognitive load of developers, thereby improving the accuracy of code maintenance cost prediction.
[0060] In some embodiments, the electronic device determines the information density value of each code line based on the code parameters of each code line in the first code block, which may specifically include:
[0061] The electronic device determines the information density value of each line of code by summing the number of variables, operators, method information density value, and the number of characters exceeding the display threshold. The method information density value is the sum of the information density values of all methods called in each line of code.
[0062] When calculating the information density value of each line of code, electronic devices can determine the information density value of each line of code by summing the number of variables, the number of operators, the information density value of methods, and the number of characters exceeding the display threshold.
[0063] For example, the line of code `result = torch.relu(x); x++` is statistically analyzed as having an information density of 50 for 4 variables (result, x, x, 1), 3 operators (=, ., ++), and 1 method call (torch.relu(x)). The display threshold is 200 characters; if the line length does not exceed 200 characters, the excess characters are not counted; if it does, the excess portion is counted. Finally, the above statistical results are combined into an information density value of 4 + 3 + 50 = 53 for the line of code.
[0064] For example, the formula for calculating the information density value b for each line of code in an electronic device can be as follows:
[0065] ;
[0066] Among them, C v It is the number of variables in the current line of code; C o It is the number of operators in the current line of code; C mi It is the information density value of the i-th method in the current line of code; o is the number of characters exceeding the display threshold, and M is the number of variables in the reusable context.
[0067] The information density value of the i-th method is calculated in the same way as the information density burden value b of the code line, and can be treated as a single line of code for calculation. For example, the information density value of a method call is the sum of the number of variables, the number of operators, and the number of characters exceeding the display threshold in the called method.
[0068] For example, the line of code: String name = customer.getContactInfo().getPrimaryEmail().toLowerCase();
[0069] Among them, the number of variables C v : name, customer = 2; number of operators C o :,.,,.,.,()×3=7; In the three method calls, getContactInfo(): b=3; getPrimaryEmail(): b=3; toLowerCase(): b=2; C mi The total is 8, M=1. If the line exceeds 200 characters, say 5 more symbols, then o=5. Substituting into the formula: b=max(0,2+7−1+8)+5×6=max(0,16)+30=46.
[0070] This possible implementation accurately quantifies the information density of a line of code by statistically analyzing parameters such as the information density values of variables, operators, and methods, combined with the number of characters exceeding a display threshold. For example, a line of code containing 10 variables, 5 operators, and 3 method calls will have a significantly higher information density burden than a line of code containing only 3 variables, 2 operators, and 1 method call. This solves the problem of existing technologies failing to quantify the multi-dimensional information density of lines of code, thus providing a more comprehensive reflection of the cognitive load on developers when maintaining code.
[0071] In some embodiments, the electronic device determines the information density value of the first code block based on the information density value of each line of code in the first code block and the number of shared variables between each line of code and its adjacent lines of code. Specifically, this may include:
[0072] The electronic device determines the sum of the information density values of each line of code in the first code block as a first parameter; then the electronic device determines the sum of the number of shared variables between each line of code in the first code block and the next line of code as a second parameter; finally, the electronic device determines the value of the first parameter minus the second parameter as the information density value of the first code block.
[0073] The number of shared variables between adjacent lines of code refers to the total number of variables that are reused in adjacent lines of code. For example, in the lines x = 1; y = x + 1; the variable x is shared in both lines, so the number of shared variables in that line of code is 1.
[0074] Electronic devices can integrate the information density burden values of code lines within the first code block. For example, the code block `if(A) B;` is split into two code blocks (A and B), and their information density burden values are calculated separately. Subsequently, the information density value of the first code block is determined by combining the number of variables shared by adjacent code lines (such as `x` being shared in both lines).
[0075] For example, the formula for calculating the information density value of the first code block by an electronic device can be as follows:
[0076]
[0077] Where B is the information density value of the first code block, n is the total number of lines in the current code block, and b n b is the information density value of the nth row. i s is the information density value of the i-th row. i It is the number of variables used in both the i-th row and the (i+1)-th row.
[0078] For example, the first code block includes:
[0079] int a = getValue(); / / Line 1
[0080] int b = a * 2 + 10; / / Line 2
[0081] int c = process(b, a); / / Line 3
[0082] printResult(c); / / Line 4.
[0083] In this dataset, the information density value b1 in the first row is 4, in the second row it is 6, in the third row it is 5, and in the fourth row it is 3. The first and second rows include the common variable 'a'; the second and third rows include both 'a' and 'b'; and the third and fourth rows include the common variable 'c'. The calculated value is B = 14.
[0084] In this possible implementation, the information density value is adjusted by splitting the code into blocks and considering the number of variables shared by adjacent lines of code. For example, a code block containing multiple loops and conditional branches (such as `for(i=0; i<10; i++) {if(A) { B;}}`) can be split into multiple independent code blocks, and their information density values can be calculated separately. This solves the evaluation bias problem caused by excessively coarse statistical granularity in existing technologies, thereby significantly improving code maintenance efficiency.
[0085] In some embodiments, the electronic device splits the target code into multiple code blocks according to the code structure of the target code, specifically including:
[0086] The electronic device can split target code into multiple code blocks based on a target identifier; and / or, the electronic device can determine that at least one conditional statement code in the target code is at least one code block; and / or, the electronic device can define at least one code block based on the separated code content in nested code. Other splitting methods may also be included, which are not limited here.
[0087] In some embodiments, where the syntax allows, code after omitting {} is still considered a code block; that is, electronic devices will also split code after omitting {} into code blocks. For example, in `if (A) B;`, B is an independent code block.
[0088] In other words, during the code block splitting process, electronic devices can perform multi-dimensional partitioning based on the code's syntactic structure. For example, in `if (A) { B;}`, A and B are divided into two separate code blocks; in `if (A) B;`, B is considered an independent code block because the syntax allows omitting `{}`; and in nested code blocks (such as `{C; if (A) { B;} D;}`), C, B, and D are divided into independent code blocks. Through syntax parsing technology, it is ensured that the code block partitioning conforms to the syntax rules of the programming language, thereby providing structured processing objects for subsequent information density calculations.
[0089] For example, electronic devices can treat code enclosed in curly braces {} as a code block; they can also treat the conditional part of conditional statements such as if / else if / catch / for as a code block (e.g., if(A){B}, where A is an independent code block); they can also treat the omitted curly braces as a code block in scenarios where the syntax allows them to be omitted (e.g., if(A){B}, where if B is only one line and the curly braces can be omitted, simply writing if(A)B, B is an independent code block); and they can treat nested code blocks as independent code blocks (e.g., {C; if(A){B;}D;}, where A, B, C, and D are all considered independent code blocks).
[0090] At least one conditional statement in the target code can be the code that evaluates a conditional statement (such as if, else if, catch, for). For example, in if (A) { B;}, A is the conditional code.
[0091] This possible implementation achieves a fine-grained breakdown of the code structure through multi-dimensional code block partitioning rules. For example, when evaluating nested logic (such as if (A) { if (B) { C;} D;}), traditional methods may only count the number of branches of the entire method, while this method can identify A, B, C, and D as independent code blocks, thus more accurately quantifying the cognitive load of each code block. By combining syntax parsing with code structure analysis, the complexity evaluation results are made more closely aligned with the actual cognitive experience of developers when reading code, significantly improving the practicality of the complexity evaluation results.
[0092] In some embodiments, the electronic device determines the complexity of the target code based on the information density value of each code block among a plurality of code blocks, which may specifically include:
[0093] The electronic device can first determine all executable paths of the target code; then, based on the information density value of at least one code block corresponding to each executable path, the electronic device determines the information density value of each executable path; finally, the electronic device determines the information density value of the first executable path as the complexity of the target code, and the first executable path is the executable path with the largest information density value among all executable paths.
[0094] For example, an electronic device can determine all executable paths of the target code based on the control flow graph of the target code. This control flow graph represents the graph structure of all possible execution paths of the target code. For instance, the control flow graph of `if (A) { B;} else {C;}` contains two paths: A→B and A→C.
[0095] In addition, the executable path of the target code can be determined in other ways, such as by using code coverage tools to extract high-frequency execution paths based on actual runtime data.
[0096] In some embodiments, when an electronic device determines the executable path of the target code, it may perform a fixed execution once for each loop step.
[0097] The executable path refers to the combination of logical branches that the target code may traverse during runtime. For example, the execution path of `if (A){ B;} else { C;}` is either A→B or A→C.
[0098] For example, an electronic device can list all possible execution paths based on the control flow graph of the target code. For instance, the paths for `if(A) { B;} else { C;}` are A→B and A→C. Then, the electronic device calculates the sum of the information density values of the code blocks traversed by each path; for example, the sum for the A→B path is bA + bB. Finally, the path with the largest sum (e.g., the sum for A→C is bA + bC and greater than the sum for A→B, bA + bB) is selected as the complexity evaluation result.
[0099] In this possible implementation, path analysis technology covers the diverse execution paths of the code, enabling a quantitative assessment of the "worst-case" complexity. For example, when evaluating methods containing multi-branch logic, traditional methods may only count the number of branches, while this application can identify the execution path under the "worst-case" scenario (such as A→C) and calculate its corresponding total complexity. By combining path analysis with information density calculation, the complexity assessment results are made closer to the high-load scenarios that developers are most likely to encounter in actual maintenance, thereby improving the practicality of the complexity assessment results.
[0100] In some embodiments, the electronic device determines the information density value of each executable path based on the information density value of at least one code block corresponding to each executable path in all executable paths, which may specifically include:
[0101] The electronic device can first determine the weight of each code block based on the code block association relationship between at least one code block corresponding to each executable path; then, the electronic device determines the information density value of each executable path based on the weight of each code block corresponding to each executable path and the information density value of each code block.
[0102] For example, the electronic device can analyze the dependencies between code blocks (such as variable sharing and method calls), for instance, in `int a = 1; B(a);`, where `a` is a shared variable. Then, the electronic device adjusts the weight of each code block based on the code block relationships (e.g., the higher the frequency of shared variables, the greater the weight), and multiplies it by the information density value of the code block to obtain a weighted information density value for each code block. Finally, the weighted information density values of each code block are summed to obtain the information density value of each executable path.
[0103] The code block association refers to the interaction between code blocks, such as variable sharing and method calls. For example, in `int a = 1; B(a);`, `a` is a shared variable, and `B(a)` is a method call.
[0104] Electronic devices can dynamically adjust the weights of information density values for code blocks based on dependency strength. For example, increasing the weight of code blocks with frequently shared variables.
[0105] In this possible implementation, code block dependency analysis enables quantitative modeling of the complexity of code block interactions. For example, when evaluating a high-information-density code block that is frequently referenced by other code blocks, its weighted burden value will be significantly higher than that of an isolated code block. By adjusting the weights, the complexity assessment results more accurately reflect the cumulative impact of interactions between code blocks on the cognitive load of developers, thereby improving the reasonableness of the assessment results.
[0106] In some embodiments, the display threshold is determined based on the character display capacity of the code display device.
[0107] For example, the display threshold can be determined based on the screen capacity of the code display device. For instance, if the screen display capacity of the code display device is 200 characters per line (including spaces and special characters), then when a line of code contains 210 characters, the excess part is 10 characters. That is, the number of variables, methods, and operators that exceed the screen (calculated based on 200 characters per line on the screen (including spaces and other special characters), and the content exceeding 200 characters is considered to be beyond the screen, and is reset when a newline character is encountered (the content beyond the screen is counted starting from a new line).
[0108] In addition, the display threshold can be determined based on other information, such as the average number of characters of code that developers remember. If the average number of characters of code that developers remember is 100, then the display threshold is 100 characters. The specific value is not limited here.
[0109] In this embodiment, the accuracy of code complexity assessment is significantly improved by introducing code block-level information density analysis and recursive integration path complexity. Compared to existing tools that only focus on the number of branches or nesting levels, this embodiment quantifies the density of variables, operators, and method calls in code lines and models cognitive load by combining human working memory capacity. Through code block-level statistics, high-risk code segments that developers need to frequently switch attention during maintenance can be directly identified, avoiding assessment bias caused by overly coarse statistical granularity. Quantifying information density values ensures that the assessment results are consistent with the actual cognitive load of developers; for example, high-information-density code blocks, even with simple branch structures, can be identified as high-maintenance-cost code segments. By analyzing the sum of information loads under the code execution path, the multi-dimensional information switching costs that developers need to handle during actual maintenance can be reflected; for example, code blocks that frequently switch within loops or conditional branches will be marked as high-risk.
[0110] Figure 3 This is a schematic diagram of the structure of a code maintenance device provided in an embodiment of this application, as shown below. Figure 3 As shown, the code maintenance device 300 provided in this embodiment includes:
[0111] Splitting module 301 is used to split the target code into multiple code blocks according to the code structure of the target code;
[0112] The first determining module 302 is used to determine the information density value of the first code block based on the code parameters of the first code block. The code parameters include the number of variables, the number of operators, the information density value of the called method, and the number of characters exceeding the display threshold. The first code block is any one of multiple code blocks, and the information density value of the called method is the information density value of all methods called by the first code block.
[0113] The second determining module 303 is used to determine the complexity of the target code based on the information density value of each code block in the multiple code blocks;
[0114] Maintenance module 304 is used to determine the corresponding code maintenance operation based on the complexity of the target code.
[0115] In one possible implementation, the first determining module 302 is specifically used to: determine the information density value of each code line based on the code parameters of each code line in the first code block, wherein the code parameters of each code line include the number of variables, the number of operators, the information density value of the called method, and the number of characters exceeding the display threshold, and the information density value of the called method is determined based on the number of variables, the number of operators, and the number of characters exceeding the display threshold of the called method; and determine the information density value of the first code block based on the information density value of each code line in the first code block and the number of shared variables between each code line and adjacent code lines.
[0116] In one possible implementation, the first determining module 302 is specifically used to: determine the sum of the number of variables, the number of operators, the method information density value, and the number of characters exceeding the display threshold for each code line as the information density value of each code line, and the method information density value as the sum of the information density values of all methods called in each code line.
[0117] In one possible implementation, the first determining module 302 is specifically used to: determine the sum of the information density values of each code line in the first code block as a first parameter; determine the sum of the number of shared variables between each code line in the first code block and the next code line as a second parameter; and determine the value of the first parameter minus the second parameter as the information density value of the first code block.
[0118] In one possible implementation, the splitting module 301 is specifically used to: split the target code into multiple code blocks according to the target identifier; and / or, determine at least one conditional statement code in the target code as at least one code block; and / or, determine at least one code block according to the separated code content in nested code.
[0119] In one possible implementation, the second determining module 303 is specifically used to: determine all executable paths of the target code; determine the information density value of each executable path based on the information density value of at least one code block corresponding to each executable path; determine the information density value of the first executable path as the complexity of the target code, wherein the first executable path is the executable path with the largest information density value among all executable paths.
[0120] In one possible implementation, the second determining module 303 is specifically used to: determine the weight of each code block based on the code block association relationship between at least one code block corresponding to each executable path; and determine the information density value of each executable path based on the weight of each code block corresponding to each executable path and the information density value of each code block.
[0121] In one possible implementation, the display threshold is determined based on the character display capacity of the code display device.
[0122] The code maintenance device provided in this embodiment can execute the method provided in the above method embodiment. Its implementation principle and technical effect are similar, and will not be described in detail here.
[0123] Figure 4 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Figure 4As shown, the electronic device 400 may include a memory 401 and a processor 402. Optionally, the electronic device may also include a transceiver 403, wherein the memory 401 and the processor 402 communicate with each other; for example, the memory 401, the processor 402 and the transceiver 403 may communicate via a communication bus 404, the memory 401 is used to store a computer program, and the processor 402 executes the computer program to implement the method of the above embodiments.
[0124] Optionally, the aforementioned processor can be a Central Processing Unit (CPU), or other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), etc. The general-purpose processor can be a microprocessor or any conventional processor. The steps in the method embodiments disclosed in this application can be directly implemented by a hardware processor, or implemented by a combination of hardware and software modules within the processor.
[0125] This application also provides a computer-readable storage medium storing computer-executable instructions, which, when executed by a processor, implement the methods in any of the above method embodiments.
[0126] This application also provides a computer program product, including a computer program that, when executed by a processor, implements the methods in any of the above method embodiments.
[0127] All or part of the steps in the above method embodiments can be implemented by hardware related to program instructions. The aforementioned program can be stored in a readable memory. When the program is executed, it performs the steps of the above method embodiments; and the aforementioned memory (storage medium) includes: read-only memory (ROM), RAM, flash memory, hard disk, solid-state drive, magnetic tape, floppy disk, optical disk, and any combination thereof.
[0128] This application describes embodiments with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of this application. It should be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processing unit of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processing unit of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart illustrations. Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.
[0129] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.
[0130] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.
[0131] Obviously, those skilled in the art can make various modifications and variations to the embodiments of this application without departing from the spirit and scope of this application. Therefore, if these modifications and variations to the embodiments of this application fall within the scope of the claims of this application and their equivalents, this application also intends to include these modifications and variations.
[0132] In this application, the term "comprising" and its variations can refer to non-limiting inclusion; the term "or" and its variations can refer to "and / or". The terms "first", "second", etc., in this application are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. In this application, "multiple" refers to two or more. "And / or" describes the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent: A existing alone, A and B existing simultaneously, and B existing alone. The character " / " generally indicates that the preceding and following related objects have an "or" relationship.
[0133] It should be noted that, for the sake of simplicity, the foregoing method embodiments are all described as a series of actions. However, those skilled in the art should understand that this application is not limited to the described order of actions, as some steps may be performed in other orders or simultaneously according to this application. Furthermore, those skilled in the art should also understand that the embodiments described in the specification are all optional embodiments, and the actions and modules involved are not necessarily essential to this application.
[0134] It should be further noted that although the steps in the flowchart are shown sequentially according to the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless explicitly stated herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least some steps in the flowchart may include multiple sub-steps or multiple stages. These sub-steps or stages are not necessarily completed at the same time, but can be executed at different times. The execution order of these sub-steps or stages is not necessarily sequential, but can be performed alternately or in turn with other steps or at least some of the sub-steps or stages of other steps.
[0135] It should be understood that the above-described device embodiments are merely illustrative, and the device of this application can also be implemented in other ways. For example, the division of units / modules in the above embodiments is only a logical functional division, and there may be other division methods in actual implementation. For example, multiple units, modules, or components may be combined, or integrated into another system, or some features may be ignored or not executed.
[0136] Furthermore, unless otherwise specified, the functional units / modules in the various embodiments of this application can be integrated into one unit / module, or each unit / module can exist physically separately, or two or more units / modules can be integrated together. The integrated units / modules described above can be implemented in hardware or as software program modules.
[0137] When integrated units / modules are implemented in hardware, the hardware can be digital circuits, analog circuits, etc. The physical implementation of the hardware structure includes, but is not limited to, transistors, memristors, etc. Unless otherwise specified, the processor can be any suitable hardware processor, such as a CPU, GPU, FPGA, DSP, and ASIC, etc. Unless otherwise specified, the storage unit can be any suitable magnetic or magneto-optical storage medium, such as Resistive Random Access Memory (RRAM), Dynamic Random Access Memory (DRAM), Static Random Access Memory (SRAM), Enhanced Dynamic Random Access Memory (EDRAM), High-Bandwidth Memory (HBM), Hybrid Memory Cube (HMC), etc.
[0138] If the integrated unit / module is implemented as a software program module and sold or used as an independent product, it can be stored in a computer-readable storage device (CMD). Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a memory and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods of the various embodiments of this application. The aforementioned memory includes various media capable of storing program code, such as a USB flash drive, read-only memory (ROM), random access memory (RAM), portable hard drive, magnetic disk, or optical disk.
[0139] In the above embodiments, the descriptions of each embodiment have their own emphasis. For parts not described in detail in a certain embodiment, please refer to the relevant descriptions of other embodiments. The technical features of the above embodiments can be combined arbitrarily. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as the combination of these technical features does not contradict each other, it should be considered within the scope of this specification.
[0140] Other embodiments of this application will readily occur to those skilled in the art upon consideration of the specification and practice of the invention disclosed herein. This application is intended to cover any variations, uses, or adaptations of this application that follow the general principles of this application and include common knowledge or customary techniques in the art not disclosed herein. The specification and examples are to be considered exemplary only, and the true scope and spirit of this application are indicated by the following claims.
[0141] It should be understood that this application is not limited to the precise structure described above and shown in the accompanying drawings, and various modifications and changes can be made without departing from its scope. The scope of this application is limited only by the appended claims.
Claims
1. A code maintenance method, characterized in that, The method includes: The target code is divided into multiple code blocks according to its code structure. The information density value of the first code block is determined based on the code parameters of the first code block. The code parameters include the number of variables, the number of operators, the information density value of the called method, and the number of characters exceeding the display threshold. The first code block is any one of the plurality of code blocks. The information density value of the called method is the information density value of all methods called by the first code block. The complexity of the target code is determined based on the information density value of each code block among the plurality of code blocks; The corresponding code maintenance operations are determined based on the complexity of the target code.
2. The method according to claim 1, characterized in that, Determining the information density value of the first code block based on its code parameters includes: The information density value of each code line is determined based on the code parameters of each code line in the first code block. The code parameters of each code line include the number of variables, the number of operators, the information density value of the called method, and the number of characters exceeding the display threshold. The information density value of the called method is determined based on the number of variables, the number of operators, and the number of characters exceeding the display threshold of the called method. The information density value of the first code block is determined based on the information density value of each line of code in the first code block and the number of shared variables between each line of code and its adjacent lines of code.
3. The method according to claim 2, characterized in that, The step of determining the information density value of each code line based on the code parameters of each code line in the first code block includes: The information density value of each code line is determined by the sum of the number of variables, the number of operators, the method information density value, and the number of characters exceeding the display threshold. The method information density value is the sum of the information density values of all methods called in each code line.
4. The method according to claim 2, characterized in that, The step of determining the information density value of the first code block based on the information density value of each line of code in the first code block and the number of shared variables between each line of code and its adjacent lines includes: The sum of the information density values of each line of code in the first code block is determined as the first parameter; The second parameter is determined by the sum of the number of shared variables between each line of code in the first code block and the next line of code. The value obtained by subtracting the second parameter from the first parameter is determined to be the information density value of the first code block.
5. The method according to claim 1, characterized in that, The step of splitting the target code into multiple code blocks according to the code structure of the target code includes: The target code is split into multiple code blocks based on the target identifier; and / or, Determine that at least one conditional statement in the target code is at least one code block; and / or, The nested code is divided into at least one code block.
6. The method according to claim 1, characterized in that, Determining the complexity of the target code based on the information density value of each code block among the plurality of code blocks includes: Identify all executable paths of the target code; The information density value of each executable path is determined based on the information density value of at least one code block corresponding to each executable path in all executable paths. The information density value of the first executable path is determined to be the complexity of the target code. The first executable path is the executable path with the largest information density value among all executable paths.
7. The method according to claim 6, characterized in that, The information density value of each executable path is determined based on the information density value of at least one code block corresponding to each executable path, including: The weight of each code block is determined based on the code block association relationship between at least one code block corresponding to each executable path; The information density value of each executable path is determined based on the weight of each code block corresponding to each executable path and the information density value of each code block.
8. The method according to any one of claims 1-7, characterized in that, The display threshold is determined based on the character display capacity of the code display device.
9. A code maintenance device, characterized in that, The device includes: The splitting module is used to split the target code into multiple code blocks according to the code structure of the target code; The first determining module is used to determine the information density value of the first code block based on the code parameters of the first code block. The code parameters include the number of variables, the number of operators, the information density value of the called method, and the number of characters exceeding the display threshold. The first code block is any one of the plurality of code blocks, and the information density value of the called method is the information density value of all methods called by the first code block. The second determining module is used to determine the complexity of the target code based on the information density value of each code block among the plurality of code blocks; The maintenance module is used to determine the corresponding code maintenance operations based on the complexity of the target code.
10. An electronic device, characterized in that, include: A processor, and a memory communicatively connected to the processor; The memory stores computer-executed instructions; The processor executes computer execution instructions stored in the memory to implement the method as described in any one of claims 1 to 8.
11. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer-executable instructions, which, when executed by a processor, are used to implement the method as described in any one of claims 1 to 8.
12. A computer program product, characterized in that, Includes a computer program that, when executed by a processor, implements the method of any one of claims 1 to 8.