Guiding Principles for Refactoring

Refactoring is essential for the codebase’s health, but some guidelines should be followed to maximize its benefits.

Luís Soares
CodeX

--

Photo by Mourizal Zativa on Unsplash

Understanding refactoring is beyond day-to-day techniques like extracting code into functions, renaming fields, and inlining variables. It’s essential to recognize the reasons and circumstances for refactoring. The primary goal is to make code maintainable and adaptable to change, but we want to do it cost-effectively. This leads us to a few fundamental principles, which we’ll explore further.

Behavior preserving

A refactoring is a change made to the internal structure of the code that doesn’t modify its observable behavior. (Refactoring)

A refactor solely consists of under-the-hood changes to a system. If you have a proper safety net, no tests should break after a change. Changing the system behavior (as observed from the outside) is a rewrite, not a refactor. By definition, refactoring should be kept separate from functionality changes or bug fixing.

--

--

Luís Soares
CodeX
Writer for

I write about automated testing, Lean, TDD, CI/CD, trunk-based dev., user-centric dev, domain-centric arch, coding good practices,