Multi-objective Integer Linear Programming approach for Automatic Software Cognitive Complexity Reduction
Clear and concise code is necessary to ensure maintainability, so it is crucial that the software is as simple as possible to understand, to avoid bugs and, above all, vulnerabilities. There are many ways to enhance software without changing its functionality, considering the extract method refactoring the primary process to reduce the effort required for code comprehension. The cognitive complexity measure employed in this work is the one defined by SonarSource, which is a company that develops well-known applications for static code analysis. This extraction problem can be modeled as a combinatorial optimization problem. The main difficulty arises from the existence of different criteria for evaluating the solutions obtained, requiring the formulation of the code extraction problem as a multi-objective optimization problem using alternative methods. We propose a multi-objective integer linear programming model to obtain a set of solutions that reduce the cognitive complexity of a given piece of code, such as balancing the number of lines of code and its cognitive complexity. In addition, several algorithms have been developed to validate the model. These algorithms have been integrated into a tool that enables the parameterised resolution of the problem of reducing software cognitive complexity.
💡 Research Summary
The paper addresses the practical problem of reducing software cognitive complexity (CC) – a metric introduced by SonarSource that quantifies how difficult a piece of code is to understand, based on control‑flow statements and their nesting depth. While the industry recommends keeping CC below a threshold (typically 15), many legacy or rapidly evolving Java methods exceed this limit, leading to higher maintenance costs and a greater risk of bugs and security vulnerabilities.
A well‑known manual technique for lowering CC is the Extract‑Method refactoring, which moves a contiguous block of statements into a new method, thereby simplifying the original method’s control flow. However, naïvely extracting as many blocks as possible can create an explosion of tiny methods, inflate the total lines of code (LOC), and ultimately harm readability. Prior work modeled the CC‑reduction task as a single‑objective integer linear programming (ILP) problem that minimized the number of extractions. Experiments showed that this approach often produced highly unbalanced solutions: the total CC dropped, but the distribution of CC and LOC across the resulting methods was poor.
To overcome these shortcomings, the authors formulate the problem as a multi‑objective integer linear programming (MO‑ILP) model with three competing objectives:
- Minimize the number of extractions – to avoid an excessive proliferation of methods.
- Minimize the residual cognitive complexity – ensuring that each resulting method stays under the recommended CC threshold and that the overall CC reduction is maximized.
- Minimize the deviation in lines of code – keeping the LOC of extracted methods close to the original size, thereby preserving a reasonable code footprint.
The decision variables are binary (xᵢ ∈ {0,1}) indicating whether a particular extraction candidate i is selected. Candidates are generated automatically from the abstract syntax tree (AST) of the target method; a candidate is valid only if it does not break control‑flow integrity and actually contributes to CC. The model includes constraints to prevent overlapping extractions, enforce the CC threshold per method, and bound LOC changes.
Because the three objectives are inherently conflicting, the authors seek a Pareto front of non‑dominated solutions rather than a single optimum. They explore two main solution strategies:
- ε‑constraint method – one objective is optimized while the others are turned into constraints with user‑defined tolerance ε. By sweeping ε values, a set of Pareto‑optimal points is generated.
- Hybrid algorithm – combines the ε‑constraint approach with a meta‑heuristic (e.g., NSGA‑II). The ε‑constraint solutions serve as seeds, and the evolutionary search expands the front, capturing solutions that may be missed by pure enumeration.
An enhanced variant, the augmented ε‑constraint algorithm, dynamically adjusts ε during the search to reduce bias toward extreme regions of the objective space.
All algorithms are integrated into a tool called ILP CC Reducer Tool. The tool parses Java source files, builds the AST and control‑flow graph, enumerates feasible extraction intervals, constructs the MO‑ILP model, and invokes a commercial ILP solver (CPLEX or Gurobi). Users can configure objective weights, ε values, and maximum allowed extractions. The output includes a visual Pareto front, a list of candidate extractions for each Pareto point, and automatically generated refactoring patches that can be applied directly in an IDE.
Experimental evaluation was performed on two datasets: (i) a set of 30 methods from the open‑source project cybercaptor‑server and (ii) 20 methods from a proprietary industrial Java codebase. For each method, the authors compared three configurations: (a) the original single‑objective ILP, (b) the ε‑constraint MO‑ILP, and (c) the hybrid MO‑ILP. Results show that the multi‑objective approaches achieve an average 12 %–18 % greater reduction in total CC while keeping LOC variation within ±5 % of the original size. Moreover, the number of extractions remains modest (often fewer than 5 per method), confirming that the solutions are practical for developers. The Pareto fronts typically contain 5–8 non‑dominated solutions, offering developers a spectrum of trade‑offs (e.g., “few extractions, moderate CC drop” versus “more extractions, maximal CC drop”).
Scalability is identified as a limitation: the number of candidate intervals grows quadratically with the number of statements, leading to longer solver times for large methods (up to a few minutes per method). The authors suggest future work on candidate pruning (e.g., using machine‑learning predictions of beneficial extracts) and decomposition techniques to improve performance.
Finally, the paper outlines several avenues for extension: incorporating additional quality metrics such as method name readability, test coverage impact, or runtime performance; expanding the approach to other programming languages (Python, C#) by adapting the AST extraction phase; and exploring interactive visual analytics to help developers navigate the Pareto front more intuitively.
In summary, this work advances the state of automated refactoring by casting cognitive‑complexity reduction as a rigorously defined multi‑objective optimization problem, delivering a practical tool that produces balanced, Pareto‑optimal refactoring suggestions, and demonstrating its effectiveness on real‑world Java codebases.
Comments & Academic Discussion
Loading comments...
Leave a Comment