Guiding Principles for Refactoring
Refactoring is essential for the codebase’s health, but some guidelines should be followed to maximize its benefits.
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 changes to a system's under the hood. No tests should break after a change (assuming the functionality had tests). 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.