Tech Tip #34: Write the tests before you refactor

Never optimise code you can't test.

Video created by explainapaper.com

The problem

An energy fix that changes behaviour is a bug. Legacy code often has no tests, so an optimisation cannot be checked and nobody dares merge it.

The fix you can use today

  • Pin current behaviour with characterisation tests before changing anything.
  • Run the full suite before and after every optimisation.
  • Keep public interfaces unchanged unless you mean to change them.

GreenCode researchers put one rule first: never break the code to save energy.

Tools that help

  • GreenCode Constitution skill (GreenCode researchers): open-source energy principles and a catalogue of 37 energy anti-patterns that an AI coding agent can follow to find and fix waste like this
  • SonarQube: static analysis for code smells, bugs and security issues, with quality gates in CI

How GreenCode helps

GreenCode helps tackle this by generating the missing unit tests before it changes any code. Every optimisation is then gated on quality issues, security flaws and error rate, which must not rise. More on this.

Tags

#testing#refactoring#legacy

Sources

All GreenCode Tech Tips

  • Tip #34
  • For Developers
  • Category Testing, CI and code quality
  • Format short video
GreenCode

Want this checked on your own system? Talk to the GreenCode team.

Get in touch