Automated refactoring of embedded software code

The method addresses the complexity of embedded software in cyberphysical systems by using machine learning to automatically refactor code, enhancing its quality and safety, and enabling efficient maintenance.

JP2025059109APending Publication Date: 2025-04-09ROBERT BOSCH GMBH
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
JP2024168807
Authority / Receiving Office
JP · JP
Patent Type
Applications
Current Assignee / Owner
Priority Date
2023-09-28
Filing Date
2024-09-27
Publication Date
2025-04-09

AI Technical Summary

Technical Problem

The high complexity of embedded software in cyberphysical systems, such as vehicles and robots, makes it difficult for software engineers to maintain and refactor code efficiently throughout the software lifecycle, particularly in ensuring reliability and safety.

Method used

A computer-implemented method using machine learning models to automatically refactor software code by evaluating and improving its structure, readability, and maintainability, while ensuring functional equivalence and adherence to safety standards like ISO26262.

Benefits of technology

The method enables efficient and reliable refactoring of software code, improving its quality, maintainability, and safety, allowing for frequent updates without significant effort, and ensuring the technical systems remain functional and safe.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure 2025059109000001_ABST
    Figure 2025059109000001_ABST
Patent Text Reader

Abstract

To provide a computer-implemented method, a computer system, and a program for automatically refactoring software codes.SOLUTION: A method 100 includes: receiving a code of software (110) or receiving a prompt (120); refactoring the code (140) via a machine learning model based on the code and the prompt at least; evaluating the refactored code (150); maintaining the refactored code based on an evaluation result according to a criterion determined in advance (160); outputting the evaluation result (170); and executing the refactored code (180).SELECTED DRAWING: Figure 1a
Need to check novelty before this filing date? Find Prior Art

Description

[Technical field]

[0001] Prior Art Embedded software is used to develop and operate technological systems, especially cyber-physical systems, e.g. They are used for open-loop control, closed-loop control and / or monitoring of computational units of vehicles and / or robots and usually have a high level of complexity that makes maintaining visibility into the software and its changes, especially during the entire software lifecycle (development, testing, production and maintenance), difficult for individual software engineers and even for entire software development departments. [Background technology]

[0002] Code refactoring means in software development the improvement of the code structure while preserving the observable program behavior, i.e. functionality. In this case, it is sought to improve, for example, readability, understandability, maintainability and / or extensibility, with the goal of reducing the respective effort for error analysis and / or functional extension. Typical refactorings are, for example, renaming variables to more understandable names and / or extracting parts of code with their own methods. Refactoring increases the quality of the code and thus the quality of the software. However, first of all, refactoring possibilities must be identified and then the corresponding refactoring must be performed without errors. Furthermore, due to the high complexity of software that may change frequently, and precisely when several different software engineers work on the code, it would be desirable to be able to refactor the code with as little effort as possible and whenever necessary.

[0003] Since the functionality and safety of a technical system must always be guaranteed, the software code must be correspondingly validated and / or verified (V&V). In particular, code refactorings must always be reliable. Summary of the Invention [Problem to be solved by the invention]

[0004] Therefore, the problem underlying this disclosure is to automatically, yet reliably, refactor software code in order to specifically improve the software. [Means for solving the problem]

[0005] Disclosure of the Invention A first general aspect of the present disclosure relates to a computer-implemented method for automatically refactoring code of software, the method including refactoring the code of the software through a machine learning model based on at least the code and a prompt, where the code is refactored, the method further including evaluating the refactored code, resulting in an evaluation result.

[0006] The software may be configured for open-loop control, closed-loop control and / or monitoring of at least one computing unit of a technical system, in particular a cyber-physical system, in particular a vehicle. In particular, the software may be embedded software. The method may include executing the (possibly compiled) refactored code and thus the software. The method may be implemented in an electronic programming environment.

[0007] A second general aspect of the present disclosure relates to a computer-implemented method for further training a machine learning model and / or a further machine learning model, where the machine learning model is configured to refactor code of a software based on at least the code and a prompt, and where the further machine learning model is configured to generate at least one refactoring suggestion for refactoring the code based on at least the code and the further prompt, the method comprising: adjusting the machine learning model and / or the further machine learning model based at least on the at least one refactored code and at least one evaluation result, where the at least one evaluation result is subsequently obtained by evaluating the at least one refactored code. The method according to the second general aspect (or embodiment) may, but need not necessarily, be performed according to the method according to the first general aspect (or embodiment thereof).

[0008] A third general aspect of the present disclosure relates to a computer system configured to perform a computer-implemented method for automatically refactoring code of a software according to the first general aspect (or an embodiment thereof) and / or a computer-implemented method for further training a machine learning model and / or a further machine learning model according to the second general aspect (or an embodiment thereof).

[0009] A fourth general aspect of the present disclosure relates to a computer program configured to perform a computer-implemented method for automatically refactoring code of a software according to the first general aspect (or an embodiment thereof) and / or a computer-implemented method for further training a machine learning model and / or a further machine learning model according to the second general aspect (or an embodiment thereof).

[0010] A fifth general aspect of the present disclosure relates to a computer-readable medium or signal storing and / or including a computer program according to the fourth general aspect (or an embodiment thereof).

[0011] The method according to the first aspect (or an embodiment thereof) proposed in the present disclosure aims at automatically refactoring the code of software. The method proposed herein allows to increase the quality of the code and thus the quality of the software. This is achieved on the one hand by a machine learning model with a sufficiently large machine language comprehension power, and on the other hand by automatically evaluating the refactored code according to established V&V methods. This allows to take advantage of the mechanical creativity of the machine learning model and at the same time ensure the quality of the refactored code. In other words, errors that may occasionally be generated entirely by the machine learning model, in particular refactored code containing errors, are recognized reliably or with a sufficiently high probability by the V&V methods before the refactored code is used for open-loop control, closed-loop control and / or monitoring of a technical system. This allows to improve the functionality and safety of the software, for example the functionality and safety of technical systems, such as vehicles or robots, that are open-loop controlled, closed-loop controlled and / or monitored by the software. Moreover, a high degree of automation allows refactoring to be carried out without great effort as often as desired during the software lifecycle, i.e. whenever necessary, thus also improving the software and thus the technical system.

[0012] During refactoring, further target requirements for the refactoring of the code can be set, for example by prompts and / or by further input of the machine learning model. For example, in addition to the functional equivalence of the code and the refactored code, the quality of the code, in particular the maintainability, can be improved as desired. Alternatively or additionally, it can be set in advance that the refactored code fulfills one or more specifications. In addition to the specifications of the software itself, it may be required that higher-level specifications, such as ISO26262, are fulfilled. In this case, for example, recursion in the code must be replaced by other loops, since according to ISO26262 recursion is not allowed. Alternatively or additionally, the further target requirements may include one or more requirements that have no effect on functional equivalence but are functionally important. Thus, for example, in addition to functional equivalence, it may be required that memory consumption is reduced and / or that stricter execution time requirements are respected. Furthermore, one can envision a goal requirement that code not only should be refactored, but also translated into other programming languages ​​(e.g., from C to Rust).

[0013] Improving the maintainability, understandability and / or modifiability of the code allows the software to be further developed more efficiently and with less errors, which also reduces the time effort and costs of software development, at least without compromising the quality of the software. The automation of quality improvement is also advantageous, especially since unfortunately software engineers often lack the time to also consider the maintainability of the code. Nevertheless, poor maintainability should be avoided - thanks to the method proposed as in this document - because it leads to a significant increase in maintenance costs and an increase in error vulnerability with subsequent changes.

[0014] A further advantage is that the resulting refactoring suggestions and / or the generated refactored code can be used to train a refactoring assistant for each domain, as may be implemented, for example, in the method 200 according to the second general aspect (or an embodiment thereof). This allows supervised fine-tuning and / or unsupervised (reinforcement) learning to be performed based on the evaluation results, thus improving the method according to the first general aspect (or an embodiment thereof). This allows (future) code to be refactored even better. [Brief description of the drawings]

[0015] [Figure 1a] FIG. 1 illustrates generally an exemplary embodiment of a computer-implemented method for automatically refactoring software code. [Figure 1b] FIG. 1 is a schematic diagram of an exemplary embodiment of a computer-implemented method for automatically refactoring software code, where at least one refactoring suggestion is implemented. [Figure 2a] FIG. 1 illustrates a schematic diagram of an example machine learning model that maps at least code and prompts to refactored code. [Figure 2b] FIG. 1 is a schematic diagram illustrating an example chaining of two machine learning models, where at least code and prompts are first mapped to at least one refactoring suggestion and then to refactored code. [Diagram 3] FIG. 2 is a schematic diagram illustrating evaluating refactored code. DETAILED DESCRIPTION OF THE PREFERRED EMBODIMENTS

[0016] Detailed Description The method 100 proposed in the present disclosure aims at automatically refactoring the code of a software. The software may be configured for open-loop control, closed-loop control and / or monitoring of at least one computing unit of a technical system, in particular a cyber-physical system, in particular a vehicle. In particular, the software may be an embedded software configured to run on an embedded (i.e. for example task-specific) system.

[0017] To this end, firstly, a computer-implemented method 100 for automatically refactoring software code is disclosed and is shown generally in Figures 1a-1b. The method 100 comprises refactoring 140 software code 10 via a machine learning model 30 based on at least the code 10 and a prompt 20, whereby the code 10 is refactored. The method further comprises evaluating 150 the refactored code 40, resulting in an evaluation result.

[0018] The code 10 of the software may be a source code of the software. The code 10 may be written in one or more programming languages. For example, the code 10 may be written in the programming language C. Alternatively or additionally, the code 10 may be written in, for example, the programming language Rust. Thanks to the sufficiently large language understanding of the machine learning model, the programming language is not specified. In other words, the method 100 is applicable to the respective code regardless of the programming language or languages.

[0019] Refactoring 140 the code 10 may include or may be modifying the code 10. The refactored code 40 may itself be software code, particularly software source code.

[0020] The machine learning model 30 may include or be a foundation model. The foundation model may be a large-scale machine learning model trained (often by self-supervised or semi-supervised learning) on ​​a large amount of data across a wide range of data so that it can be tailored to a rich and wide range of downstream tasks. In particular, the machine learning model may include or be a large-scale language model (LLM). The large-scale language model may be a language model characterized by its size. In particular, the large-scale language model may be a chatbot and / or have a chatbot function.

[0021] As a large-scale language model, for example, LLaMA, an AI from Meta, can be used. Such a large-scale language model may be advantageous because such a large-scale language model, in particular the weights and / or distortions of such a large-scale language model, can be adjusted, for example, by the method 200. Alternatively or additionally, for example, BERT from Google can be used. Alternatively or additionally, for example, ChatGPT from OpenAI (for example, version 24 May 2023) can be used as a large-scale language model. Alternatively or additionally, for example, Bloom from Hugging Face can be used as a large-scale language model.

[0022] Alternatively or additionally, the machine learning model may include or be a multi-domain model, for example, GPT-4 from OpenAI (e.g., version 14 March 2023).

[0023] An exemplary embodiment of a machine learning model 30 is illustrated generally in FIGS. 2a-b.

[0024] The prompt 20 may be natural language text. The prompt 20 may include or be a natural language instruction to a machine learning model 30. The prompt 20 may include a voice instruction to a large language model (LLM) for the purpose of refactoring the software's code 10, such as the prompt for the machine learning model 30 of FIG. 2a.

[0025] Alternatively or additionally, the prompt 20 may include spoken instructions to a large language model (LLM) aimed at implementing at least one refactoring suggestion 50 such that the code 10 of the software is refactored, such as, for example, a prompt for the machine learning model 30 of FIG. 2b.

[0026] Alternatively or additionally, the prompts 20 may include spoken instructions to a large language model (LLM) for the purpose of extending, improving, and / or modifying the pre-determined refactoring suggestions.

[0027] The evaluation result may comprise or be a natural language text. Alternatively or additionally, the evaluation result may comprise or be a data structure written in a pre-determined syntax (e.g. in a programming language). The data structure may comprise, for example, a natural language text. The evaluation result may comprise one or more numerical values, in particular one or more confidence values. This allows encoding the quality of the refactored code 40 and thus of the possibly at least one refactoring suggestion 50, which may be taken into account when validating and verifying the refactored code and thus the software. Evaluating 150 the refactored code 40 may be regarded as validation and / or verification (V&V).

[0028] Even in case of failure to generate refactored code, the evaluation result may contain information about the unsuccessful generation of refactored code. This can happen, for example, if the code already contains inconsistencies and / or is broken. Such information is also valuable for the development of the technical system. In such cases, it is possible and necessary to adjust, especially improve, the code. On the other hand, if the code has already been refactored (satisfactorily), no further refactoring will be necessary. In this case, there is no need for processing.

[0029] 3, the generated refactored code may be evaluated, where the evaluation result includes, for example, an OK or nOK (short for "not OK") outcome, respectively. In the case of an nOK outcome, the refactored code may be repaired ("fixed"), and the refactored code generated in a previous iteration of method 100 may be adjusted, improved, and / or modified, for example, in a subsequent iteration of method 100.

[0030] The method 100 may include executing 180 the (possibly compiled) refactored code 40, and thus the software, such that by executing the (compiled) refactored code, a technical system can be open-loop controlled, closed-loop controlled and / or monitored.

[0031] The method 100 may be implemented in an electronic programming environment. The electronic programming environment may be, but need not be, an execution environment. The electronic programming environment may be superior to the execution environment. For example, the execution environment may be initialized and subsequently controlled by the electronic programming environment. Furthermore, for example, one or more execution results may be returned from the execution environment to the electronic programming environment. The electronic programming environment allows programming and / or interaction with multiple iterations of the method 100. In particular, an electronic programming environment has been found to be advantageous when initializing and / or debugging during an iteration. For example, the prompt 20 and / or the further prompt 21 may be adjusted within the electronic programming environment.

[0032] As for example shown diagrammatically in Fig. 1b, refactoring 140 the code 10 of the software may comprise implementing 141 at least one refactoring suggestion 50 via the machine learning model 30. By implementing 141 the at least one refactoring suggestion 50 the code 10 may be refactored, i.e. in particular modified. On the other hand, as for example shown diagrammatically in Fig. 1a, implementing 141 the at least one refactoring suggestion 50 may be omitted, for example if the refactored code is already the output of the machine learning model 30, as for example shown diagrammatically in Fig. 2a.

[0033] For example, as shown generally in Figures 1b and 2b, the method 100 may include generating 130 at least one refactoring suggestion 50 for refactoring the code 10 based on at least the code 10 and a further prompt 21 via a further machine learning model 31.

[0034] The step 130 of generating the at least one refactoring suggestion 50 may be performed before implementing 141 the at least one refactoring suggestion 50. Alternatively or additionally, the at least one pre-determined refactoring suggestion can be set as a further input of a further machine learning model 31 or via a further prompt 21.

[0035] Furthermore, the one or more pre-determined refactoring suggestions may be fixedly pre-set or may be received from a user, for example via user input of the electronic programming environment. In other words, the one or more pre-determined refactoring suggestions do not have to be generated 130 via a further machine learning model 31, but may be identified in advance by other techniques (for example via Bad Smell Detection).

[0036] The further machine learning model 31 may include or be a foundation model. In particular, the further machine learning model may include or be a large-scale language model (LLM). Alternatively or additionally, the further machine learning model may include or be a multi-domain model. The further machine learning model 31 (for generating 130 at least one refactoring suggestion 50 for refactoring the code 10) may, but does not necessarily have to be a machine learning model 30 (for refactoring 140 the code, and possibly implementing 141 the at least one refactoring suggestion 50). The further prompt 21 may, but does not necessarily have to be a prompt 20.

[0037] The further prompt 21 may be natural language text. The further prompt 21 may include or be a natural language instruction to the machine learning model 31. The further prompt may include a voice instruction to a large language model (LLM) for generating at least one refactoring suggestion 50 based on at least the code 10.

[0038] The method 100 may include receiving 110 the code 10. Alternatively or additionally, the method 100 may include receiving 120 the prompt 20 and / or the further prompt 21. Alternatively or additionally, the method 100 may include receiving at least one refactoring suggestion 50. Alternatively or additionally, the method 100 may include receiving the machine learning model 30 and / or the further machine learning model 31.

[0039] The at least one refactoring suggestion 50 may include one or more instructions on how the code 10 of the software may be modified such that the functionality of the software remains unchanged and at least one target requirement for the code 10 is satisfied. The at least one target requirement may call for improving the quality of at least the code 10.

[0040] The improvement of the quality of the code 10 may be aimed at the goal of reducing the respective effort for error analysis and / or functional extension. The improvement of the quality of the code 10 may include, for example, an improvement in the readability of the code 10. Alternatively or additionally, the improvement of the quality of the code 10 may include, for example, an improvement in the understandability of the code 10. Alternatively or additionally, the improvement of the quality of the code 10 may include, for example, an improvement in the maintainability of the code 10. Alternatively or additionally, the improvement of the quality of the code 10 may include, for example, an improvement in the extensibility of the code 10 (or the software). The prompt 20 and / or the further prompt 21 may include such settings. Alternatively, the prompt 20 and / or the further prompt 21 may be considered as further inputs to the machine learning model 30 and / or the further machine learning model 31, respectively.

[0041] The instructions may be in a natural language. Alternatively or additionally, the instructions may include a data structure in which the proposed modification of the code 10 is contained, in particular a data structure that encodes the proposed modification of the code 10.

[0042] The at least one target requirement or further target requirements for the modification of the code 10 may comprise, for example, a requirement for a memory demand at the time of execution of the software. Such a requirement may for example be based on an (absolute) memory capacity. Here, it may be set, for example, that the memory demand at the time of execution of the software should not exceed a maximum memory capacity. Alternatively or additionally, the requirement may for example be based on a wish to change the (relative) memory demand. Here, it may be set, for example, that the memory demand should be changed to some fraction (for example 90%) of the current memory demand.

[0043] Alternatively or additionally, the at least one target requirement or further target requirements for the modification of the code 10 may comprise, for example, a requirement on the execution time of the software at run-time. Such a requirement may for example be based on an (absolute) execution time. Here, it may be set, for example, that the execution time of the software at run-time should not exceed a maximum execution time. Alternatively or additionally, the requirement may for example be based on a (relative) execution time change wish. Here, it may be set, for example, that the execution time should be changed to some fraction (for example 80%) of the current execution time.

[0044] Alternatively or additionally, the at least one goal requirement or further goal requirements regarding the modification of the code 10 may include, for example, a requirement that the refactored code should be written in another programming language.

[0045] Particular factors on which refactoring and / or generating at least one refactoring suggestion may be based are disclosed below.

[0046] Refactoring 140 the software code via the machine learning model 30 can be based on at least a natural language and / or formal specification of the software, which may be taken into account as further input for the machine learning model 30 during refactoring 140, for example. Alternatively, the natural language and / or formal specification of the software can be introduced into the refactoring 140 via the prompt 20.

[0047] Alternatively or additionally, generating 130 the at least one refactoring suggestion 50 via the further machine learning model 31 may be based on at least a natural language and / or formal specification of the software. The natural language and / or formal specification of the software may for example be taken into account as a further input of the further machine learning model 31 when generating 130 the at least one refactoring suggestion 50. Alternatively, the natural language and / or formal specification of the software may be incorporated into generating 130 the at least one refactoring suggestion 50 via the further prompt 21.

[0048] The natural language and / or formal specification of the software may include, for example, one or more valid ranges for parameters of the code 10 (or the software). Alternatively or additionally, the natural language and / or formal specification of the software may include, for example, a description of the (desired) functional manner of the software. Alternatively or additionally, the natural language and / or formal specification of the software may include, for example, an objective to be achieved by the software.

[0049] Alternatively or additionally, refactoring 140 the code of the software via the machine learning model 30 may be based at least on at least one pre-determined refactoring suggestion 51. The at least one pre-determined refactoring suggestion 51 may be taken into account as further input of the machine learning model 30 when refactoring 140, e.g. as shown diagrammatically in Fig. 2a. Alternatively, the at least one pre-determined refactoring suggestion 51 may be incorporated into the refactoring 140 via a prompt 20.

[0050] Alternatively or additionally, generating 130 the at least one refactoring suggestion 50 via the further machine learning model 31 may be based on at least a pre-determined refactoring suggestion 51. The pre-determined refactoring suggestion 51 may be taken into account as a further input of the further machine learning model 31 in generating 130 the at least one refactoring suggestion 50, as for example illustrated diagrammatically in Fig. 2b. Alternatively, the pre-determined refactoring suggestion 51 may be incorporated into generating 130 the at least one refactoring suggestion 50 via a further prompt 21.

[0051] The pre-determined refactoring suggestions 51 may include general suggestions on how to refactor the code 10. For example, a general suggestion may be set that recursion in the code 10 should be replaced by other loops (see ISO 26262). Alternatively or additionally, a general suggestion may be set that, for example, dynamic memory allocation should not be performed in the code 10. Alternatively or additionally, the pre-determined refactoring suggestions 51 may include suggestions on how to modify an exemplary portion of the code 10. In another example, the pre-determined refactoring suggestions 51 may have been generated in a previous iteration of the method 100, and this pre-determined refactoring suggestions 51 should now be extended, improved, and / or modified in the current iteration of the method 100. To this end, the prompts 20 may possibly be adjusted for the current iteration.

[0052] Refactoring 140 the code of the software or implementing 141 at least one refactoring suggestion 50 via the machine learning model 30 may comprise implementing 142 a plurality of refactoring suggestions via the machine learning model 30. The method 100 may comprise implementing 142 a plurality of refactoring suggestions via the machine learning model 30 based on at least the prompt 20 and / or the further prompt 21. One or more of the plurality of refactoring suggestions may be implemented, for example, based on at least the prompt 20 and / or the further prompt 21, i.e. in a (single) execution of the method 100. Alternatively or additionally, one or more of the plurality of refactoring suggestions may be implemented by multiple executions of the method 100. Advantageously, the prompt 20 and / or the further prompt 21 may be modified in multiple executions of the method 100. On the other hand, prompt 20 and / or further prompt 21 may remain unchanged across multiple performances of method 100 .

[0053] The implementing 141, 142 of the at least one refactoring suggestion 50 may be performed automatically, i.e. without input from a user of the electronic programming environment. Alternatively, the implementing 141, 142 of the at least one refactoring suggestion 50 may depend, at least in part, on input from a user of the electronic programming environment.

[0054] Generating 130 at least one refactoring suggestion 50 via the further machine learning model 31 and / or, for example as shown diagrammatically in FIG. 1b, the method 100 may include generating 131 a plurality of refactoring suggestions for refactoring the code 10 based on the code 10 and the further prompts 21 via the further machine learning model 31.

[0055] The method 100 may include generating a plurality of refactoring proposals (i.e. at least two refactoring proposals). One or more of the plurality of refactoring proposals may be generated, for example based on at least the code 10 and the further prompt 21, i.e. in a (single) execution of the method 100. Alternatively or additionally, one or more of the plurality of refactoring proposals may be generated by multiple executions of the method 100. Advantageously, the further prompt 21 may be modified in multiple executions of the method 100. On the other hand, the further prompt 21 may not be modified in multiple executions of the method 100. Alternatively or additionally, one or more of the plurality of refactoring proposals may be pre-determined. In view of a high code complexity, a large number of refactoring proposals may be required to refactor 140 the code.

[0056] The method 100 may include keeping 160 the refactored code 40 based on the evaluation result according to the pre-determined criteria, e.g. as shown diagrammatically by "OK" in Fig. 3. The method 100 may (but does not necessarily) include discarding 161 the refactored code 40 if this is not the case (i.e., if keeping 160 is not performed), e.g. as shown diagrammatically in Figs. 1a-1b. Instead of discarding, the refactored code may for example be repaired (see e.g. Fig. 3). If the refactored code 40 is not kept 160, the refactored code 40 and / or knowledge derived from the refactored code 40 may be considered in a new iteration of the method 100.

[0057] Evaluating 150 the refactored code 40 may include checking whether the code 10 and the refactored code 40 match in view of their execution in at least one pre-determined test case. In particular, evaluating 150 the refactored code 40 may include checking whether the code 10 and the refactored code 40 match in view of their execution in a plurality of pre-determined test cases. For this purpose, an execution environment (also called a test harness), for example, may be automatically constructed, in particular by the method 100, for executing the code 10 and the refactored code 40 in view of the at least one pre-determined test case or the plurality of pre-determined test cases. The at least one pre-determined test case or the plurality of pre-determined test cases may be automatically generated. Alternatively or additionally, the at least one pre-determined test case or the plurality of pre-determined test cases may be based on fuzzing. Alternatively or additionally, the at least one pre-determined test case or the plurality of pre-determined test cases can be based on derivation from at least one contract. A contract can, for example, include one or more comments in the code that describe properties to be satisfied by the code.

[0058] Alternatively or additionally, the at least one pre-determined test case or the plurality of pre-determined test cases may have been generated by a third machine learning model. The third machine learning model may be the machine learning model 30 (for refactoring 140 the code, possibly implementing 141 at least one refactoring suggestion). Alternatively or additionally, the third machine learning model may be the further machine learning model 31 (for generating 130 at least one refactoring suggestion for refactoring the code 10). On the other hand, the third machine learning model does not necessarily have to be the machine learning model 30 (for refactoring 140 the code, possibly implementing 141 at least one refactoring suggestion) or the further machine learning model 31 (for generating 130 at least one refactoring suggestion for refactoring the code 10). The at least one pre-determined test case or at least some of the plurality of pre-determined test cases may have been found to be discriminatory in a previous iteration of the method 100. A test case may be made discriminatory in a previous iteration of method 100 if the code that is deterministic in the previous iteration of method 100 and the refactored code that is deterministic in the previous iteration of method 100 do not match in light of their execution in the test case. Discriminatory tests and passing discriminatory tests are particularly compelling.

[0059] If code 10 and refactored code 40 do not match in light of their execution in at least one test case, then there is a lack of functional equivalence, in which case refactored code 40 can (and should) be discarded.

[0060] Method 100 may include labeling the at least one pre-determined test case as distinctive if the inspection reveals that code 10 and refactored code 40 are inconsistent in light of their execution in the at least one pre-determined test case. As previously explained, for example, the at least one pre-determined test case labeled as distinctive may be utilized, possibly together with tailored prompt 20 and / or tailored further prompt 21, to generate one or more further refactoring suggestions in further iterations of method 100.

[0061] Alternatively or additionally, evaluating 150 the refactored code 40 may include checking whether the refactored code 40 satisfies at least one pre-determined contract. This may be performed in a static check. This may be performed, for example, via Bounded Model Checking. Alternatively or additionally, this may be performed, for example, via Abstract Interpretation. Evaluating 150 the refactored code 40 may include checking whether the refactored code 40 satisfies a plurality of pre-determined contracts.

[0062] Alternatively or additionally, evaluating 150 the refactored code 40 may include checking whether the code 10 and the refactored code 40 are functionally equivalent, which may also be performed via bounded model checking, for example.

[0063] Alternatively or additionally, evaluating 150 the refactored code 40 may include checking whether the refactored code 40 satisfies at least one target requirement. Checking whether the refactored code 40 satisfies at least one target requirement may include applying code metrics and / or test metrics to the refactored code 40. In this case, in particular a maintainability index may be calculated. For example, test coverage may be measured. Alternatively or additionally, further properties of the (refactored) code may be checked in a static or dynamic analysis. Alternatively or additionally, the code quality may also be based on a weighting of multiple code metrics and / or test metrics.

[0064] For at least one or each of the multiple refactoring proposals, refactored code can be generated and evaluated 150. The refactored code can be kept or discarded depending on the respective evaluation results, and in the case of discarding, the last kept refactored code is kept. Here, for example, in each step, one or more refactoring proposals can be generated according to the method 100, and the code is sequentially refactored based on the one or more refactoring proposals. This makes it possible to efficiently generate code in which all refactoring proposals are implemented.

[0065] The method 100 may include outputting 170 the evaluation results, which may be performed, for example, via an electronic programming environment. This may be advantageous because the sequence of the method 100 may be adjusted based on the evaluation results. Thus, for example, refactoring suggestions and / or refactored code may be manually selected or culled based on the evaluation results.

[0066] Alternatively or additionally, the method 100 may include outputting 171 the refactored code 40. This may also be performed via an electronic programming environment.

[0067] Alternatively or additionally, the method 100 may include outputting 172 at least one refactoring suggestion 50. This may also be performed via, for example, an electronic programming environment.

[0068] Method 100 may be based on at least one input of a user of an interface of an electronic programming environment. For example, method 100 and / or multiple executions of method 100 may be (interactively) controlled via the electronic programming environment. This may be useful, for example, when setting up a programming environment and / or an execution environment before method 100 is automatically executed. Alternatively or additionally, interactive control may be useful when debugging an automatic execution of method 100.

[0069] Alternatively or additionally, code refactoring can continue after interactively selecting a desired refactoring suggestion, for example via input after multiple refactoring suggestions have been generated.

[0070] Method 100 may be performed iteratively, where the machine learning model and / or the prompts, and possibly further inputs, may be changed. Alternatively or additionally, possibly further machine learning models and / or further prompts, and possibly further inputs may be changed.

[0071] The different refactoring suggestions can be generated by a single execution of the method 100 or by multiple executions. Already with a single execution of the method 100, there is a temperature due to the inherent variability of the machine learning model. If, for example, 101 different refactoring suggestions are to be generated based on (further) prompts, then due to the inherent variability, 101 different refactoring suggestions can indeed be generated. Furthermore, with multiple executions of the method 100, there is a variation due to the wide variety of (further) prompts possible. Alternatively or additionally, there is a variation due to different combinations of different input artifacts (e.g., code only, with specification requirements, etc.). However, when statically verifiable refactoring suggestions that occur multiple times are generated (e.g., by multiple independent executions), the refactoring suggestions can be filtered.

[0072] Also disclosed is a computer-implemented method 200 for further training the machine learning model 30 and / or the further machine learning model 31, where the machine learning model 30 is configured to refactor 140 a code 10 of the software based on at least the code 10 and a prompt 20, and where optionally the further machine learning model 31 is configured to generate 130 at least one refactoring suggestion 50 for refactoring the code 10 based on at least the code 10 and the further prompt 21. The method 200 includes at least adjusting the machine learning model 30 and / or the further machine learning model 31 based on the at least one refactored code 40 and the at least one evaluation result, where the at least one evaluation result is obtained by evaluating 150 the at least one refactored code 40.

[0073] In particular, a computer-implemented method 200 for further training a machine learning model 30 is disclosed, where the machine learning model 30 is configured to refactor 140 code 10 of the software based on at least the code 10 and a prompt 20. The method 200 includes at least adjusting the machine learning model 30 based on the at least one refactored code 40 and at least one evaluation result, where the at least one evaluation result is obtained by evaluating 150 the at least one refactored code 40.

[0074] Alternatively or additionally, a computer-implemented method 200 is disclosed for further training the further machine learning model 31, the further machine learning model 31 being configured to generate 130 at least one refactoring suggestion 50 for refactoring the code 10 based on at least the code 10 and the further prompt 21. The method 200 comprises at least tuning the further machine learning model 31 based on the at least one refactored code 40 and the at least one evaluation result, the at least one evaluation result being obtained by evaluating 150 the at least one refactored code 40.

[0075] Alternatively or additionally, a computer-implemented method 200 for further training the machine learning model 30 and the further machine learning model 31 is disclosed, where the machine learning model 30 is configured to refactor 140 a code 10 of the software based on at least the code 10 and a prompt 20, and the further machine learning model 31 is configured to generate 130 at least one refactoring suggestion 50 for refactoring the code 10 based on at least the code 10 and the further prompt 21. The method 200 comprises at least adjusting the machine learning model 30 and / or the further machine learning model 31 based on the at least one refactored code 40 and the at least one evaluation result, where the at least one evaluation result is obtained by evaluating 150 the at least one refactored code 40.

[0076] Evaluating 150 may, but need not be, part of method 200. At least one refactored code 40 may have been generated 130 and evaluated 150 according to a method 100 for automatically refactoring software code 10. Method 200 may, but need not be, a continuation of method 100.

[0077] Alternatively or additionally, method 100 can be performed anew via the adjusted machine learning model and / or the adjusted further machine learning model. In particular, machine learning model 30 and / or further machine learning model 31 can be adjusted according to method 200 between multiple performances of method 100.

[0078] Adjusting the machine learning model 30 and / or the further machine learning model 31 may be based at least on the at least one code 10 to be refactored and at least the prompt 20 and / or the further prompt 21.

[0079] The machine learning model 30 and / or the further machine learning model 31 can be tuned via supervised learning. Such tuning of the machine learning model 30 and / or the further machine learning model 31 can be considered as supervised finetuning. Thanks to finetuning, a generic machine learning model trained for example with general machine language understanding can be tuned for the respective application, i.e. here in view of generating refactoring suggestions and / or refactored code.

[0080] Fine-tuning of the machine learning model 30 and / or the further machine learning model 31 may, but does not necessarily have to, precede further tuning of the machine learning model 30 and / or the further machine learning model 31 by unsupervised (reinforcement) learning.

[0081] Alternatively or additionally, adjusting the machine learning model 30 and / or the further machine learning model 31 based at least on the at least one refactored code 40 and the at least one evaluation result may comprise calculating at least one reward based at least on the at least one evaluation result and adjusting the machine learning model 30 and / or the further machine learning model 31 based at least on the at least one refactored code 40 and the at least one reward. Such adjustment of the machine learning model 30 and / or the further machine learning model 31 can be considered as unsupervised (reinforcement) learning. Thereby, for example, a generic machine learning model trained with general machine language comprehension and / or such a model after fine-tuning can be (further) adjusted for each application, i.e. here (further) adjusted in light of generating refactoring suggestions and / or refactored code. This allows the machine learning model 30 and / or the further machine learning model 31 to be even better adjusted to the application case of code refactoring.

[0082] A reward may be a parameter, particularly a numerical parameter, that is comparable to other parameters that are also rewards, for example, the reward may be greater than, equal to, or less than the other rewards.

[0083] If the at least one evaluation result is relatively good, the at least one reward may be higher, and if the at least one evaluation result is relatively bad, the at least one reward may be lower.

[0084] The evaluation result may be relatively bad, particularly bad, if the tests on which the evaluation result is based are already negative, i.e. for example failed. Alternatively or additionally, the evaluation result may be relatively good, particularly good, if all tests are positive, i.e. for example passed.

[0085] The machine learning model 30 and / or the further machine learning model 31 can be adjusted based on at least a plurality of refactored codes and a corresponding plurality of rewards. The method can then include calculating a reward for each refactored code based on at least a plurality of evaluation results. In other words, a reward for a refactored code does not have to depend only on the evaluation result of this refactored code, but can also be based on one or more evaluation results for other refactored codes. The reward or rewards (i.e. the reward height) can for example depend on the number of evaluation results. Alternatively or additionally, the reward or rewards can depend on the number of relatively good evaluation results and the number of relatively bad evaluation results, in particular on the imbalance between the relatively good and relatively bad evaluation results. Alternatively or additionally, the reward can be calculated for each evaluation result first and then adjusted and / or calculated based on other evaluation results.

[0086] Tuning the machine learning model 30 and / or the further machine learning model 31 may be based on a reinforcement learning algorithm, such as, for example, Proximal Policy Optimization (PPO).

[0087] Parts of the machine learning model 30 and / or parts of the further machine learning model 31 may be (intentionally) not adjusted, i.e. fixed, in which case only other parts of the machine learning model 30 and / or other parts of the further machine learning model 31 may be adjusted.

[0088] For example, certain parameters, such as weights and / or distortions, in particular weights and / or distortions, may not be adjusted for earlier layers of the machine learning model 30 and / or the further machine learning model 31, i.e. may be fixed. For example, adjustments may be limited to later layers of the machine learning model 30 and / or the further machine learning model 31 where refactoring suggestions are processed and / or where refactorings are performed. This can ensure that the machine learning model 30 and / or the further machine learning model 31 are not excessively deteriorated in terms of machine language comprehension. On the other hand, a deterioration in machine language comprehension due to adjustments may be required insofar as it is not important for refactoring the code. For example, machine language comprehension of Shakespeare English is not (usually) necessary for refactoring the code.

[0089] The method 200 may be controlled via an electronic programming environment. In particular, the machine learning model 30 and / or the further machine learning model 31 may be further adjusted via the electronic programming environment.

[0090] Further disclosed is a computer system configured to perform a computer-implemented method 100 for automatically refactoring the software's code 10. Alternatively or additionally, the computer system may be configured to perform a computer-implemented method 200 for further training the machine learning model 30 and / or the further machine learning model 31. In particular, the computer system may be configured to perform the computer-implemented method 100 for automatically refactoring the software's code 10 and (e.g. subsequently) the computer-implemented method 200 for further training the machine learning model 30 and / or the further machine learning model 31. The computer system may include a processor and / or a main memory.

[0091] Further disclosed is a computer program configured for performing a computer-implemented method 100 for automatically refactoring the software's code 10. Alternatively or additionally, the computer program may be configured for performing a computer-implemented method 200 for further training the machine learning model 30 and / or the further machine learning model 31. In particular, the computer program may be configured for performing the computer-implemented method 100 for automatically refactoring the software's code 10 and (for example subsequently) the computer-implemented method 200 for further training the machine learning model 30 and / or the further machine learning model 31. The computer program may for example be present in an interpretable or compiled form. The computer program may for example be loaded (even partially) into the computer's RAM to be executed as a bit or byte sequence.

[0092] Further disclosed is a computer readable medium or signal having stored thereon and / or including the computer program. The medium may include, for example, one of a RAM, a ROM, an EPROM, a HDD, an SSD, etc., on / within which the signal is stored.

Claims

1. A computer-implemented method (100) for automatically refactoring software code, comprising: The method (100) comprises: - refactoring (140) the code (10) of the software via a machine learning model (30) based on at least the code (10) and a prompt (20), whereby the code (10) is refactored (140); Evaluating (150) the refactored code (40), resulting in an evaluation result; and A method (100).

2. Refactoring (140) the code (10) of the software comprises: implementing (141) at least one refactoring suggestion (50) via the machine learning model (30); The method (100) comprises: generating (130) the at least one refactoring suggestion (50) for refactoring the code (10) based on at least the code (10) and a further prompt (21) via a further machine learning model (31); The method of claim 1 , comprising:

3. the software being configured for open-loop control, closed-loop control and / or monitoring of at least one computing unit of a technical system, in particular a cyber-physical system, in particular a vehicle, 3. The method (100) of claim 1 or 2.

4. The method (100) comprises: - maintaining (160) the refactored code (40) based on the evaluation results according to pre-determined criteria; If not, optionally discarding (161) the refactored code (40); and The method (100) of any one of claims 1 to 3, comprising:

5. Evaluating (150) the refactored code (40) includes: - checking whether said code (10) and said refactored code (40) are consistent in view of their execution in at least one pre-determined test case; The method (100) of any one of claims 1 to 4, comprising:

6. the at least one pre-determined test case having been found to be discriminatory in a previous iteration of the method (100); 6. The method (100) of claim 5.

7. Evaluating (150) the refactored code (40) includes: Checking whether the refactored code (40) satisfies at least one pre-determined contract, optionally via bounded model checking and / or abstract interpretation. The method (100) of any one of claims 1 to 6, comprising:

8. Evaluating (150) the refactored code (40) includes: Checking, optionally via bounded model checking, whether said code (10) and said refactored code (40) are functionally equivalent. The method (100) of any one of claims 1 to 7, comprising:

9. Evaluating (150) the refactored code (40) includes: Checking whether the refactored code (40) satisfies at least one target requirement The method (100) of any one of claims 1 to 8, comprising:

10. Checking whether the refactored code (40) satisfies at least one target requirement includes: Applying code metrics and / or test metrics to the refactored code (40), in particular calculating a maintainability index.

10. The method of claim 9, comprising:

11. A computer-implemented method (200) for further training a machine learning model (30) and / or a further machine learning model (31), comprising: the machine learning model (30) is configured to refactor (140) code (10) of the software based on at least the code (10) and a prompt (20); the further machine learning model (31) is configured to generate (130) at least one refactoring suggestion (50) for refactoring the code (10) based on at least the code (10) and a further prompt (21); The method (200) comprises: - adjusting the machine learning model (30) and / or the further machine learning model (31) based on at least the at least one refactored code (40) and at least one evaluation result. Including, the at least one evaluation result is obtained by evaluating (150) the at least one refactored code (40); The at least one refactored code (40) has been generated (140) and evaluated (150) according to a method (100) for automatically refactoring a software code (10) according to any one of claims 1 to 10. Method (200).

12. Adjusting the machine learning model (30) and / or the further machine learning model (31) based on at least the at least one refactored code (40) and the at least one evaluation result comprises: - calculating at least one reward based on at least one evaluation result; - adjusting the machine learning model (30) and / or the further machine learning model (31) based on at least the at least one refactored code (40) and the at least one reward; 12. The method of claim 11, comprising:

13. A computer-implemented method (100) for automatically refactoring a piece of software code (10) according to any one of claims 1 to 10, and / or A computer-implemented method (200) for further training a machine learning model (30) according to claim 11 or 12 and / or a further machine learning model (31).

23. A computer system configured to:

14. A computer-implemented method (100) for automatically refactoring a piece of software code (10) according to any one of claims 1 to 10, and / or A computer-implemented method (200) for further training a machine learning model (30) according to claim 11 or 12 and / or a further machine learning model (31). A computer program configured to perform the steps of:

15. A computer readable medium or signal storing and / or including a computer program according to claim 14.

Citation Information

Cited By

  • Information processing method, computer program, and information processing device

    JP7765670B1