Software Engineering
Code refactoring's hidden energy costs vary wildly with workloads, study finds
A large-scale analysis of real-world Java code improvements shows that refactoring can either improve or harm energy efficiency depending on how the software is used.
Refactoring—the process of rewriting code to improve quality and maintainability without changing what the software does—is a cornerstone of modern development practice. Yet researchers have discovered that these improvements carry an often-overlooked cost: their effect on energy consumption varies significantly depending on how the software actually runs in practice.
The scale of the research#
A new study from arXiv represents the first comprehensive investigation into whether refactoring makes software more or less energy-efficient. The team evaluated 68 different refactoring patterns across controlled benchmarks and analyzed 481 real-world code improvements from 430 GitHub projects, all written in Java.
The researchers used repeated, paired energy measurements to isolate the effects of refactoring and account for natural variability in power consumption during execution.
Workload sensitivity reshapes outcomes#
Perhaps the most striking finding concerns workload dependency. In controlled experiments, nearly half of all refactoring instances (45.3%) actually changed their energy classification—from beneficial to harmful or vice versa—simply because the testing conditions shifted. Across the controlled benchmark, 51.8% of refactoring-workload combinations showed statistically significant energy differences, indicating that the same code change can have opposite effects depending on usage patterns.
Real-world commits told a different story: only 7.5% showed significant energy shifts, though two-thirds of these differed by at least 10% in power consumption.
Traditional prediction methods fall short#
The study challenged conventional wisdom about predicting energy outcomes. Refactoring type alone proved insufficient as a predictor—developers can't simply say "this type of refactoring always saves energy." While certain recurring refactoring combinations were associated with energy reductions, broader patterns remained elusive.
The researchers also tested whether existing metrics and large language models could identify energy regressions (cases where refactoring inadvertently wasted power). Neither approach worked reliably, suggesting that current tools cannot yet be trusted to catch energy-harming code changes.