ENES

Refactoring

Reorganizing code to make it clearer, without changing what it does.

A tangled block of code becomes three small named functions, while the same input keeps producing the same output and the tests stay green

Refactoring means changing the internal structure of code without changing its behavior. From the outside nothing is different: same screens, same results, same bugs, even. Inside, the code becomes easier to read and to change. Typical moves are giving a variable a clearer name, splitting a 300-line function into small ones with meaningful names, removing duplicated code, or moving a piece to where it logically belongs.

The idea was popularized by Martin Fowler's book Refactoring, first published in 1999, which catalogued dozens of these small transformations. The key rule is to go in small steps and keep the program working after each one. That is only safe with good automated tests: you run them after every step, and if they still pass, you know you did not break anything.

Teams refactor because code that is easy to understand is cheap to change, and software is changed constantly. It is the main way to pay down technical debt. A common habit is to clean up the area you are about to work on before adding a new feature there, sometimes summed up as "leave the campground cleaner than you found it". What refactoring is not: rewriting everything from scratch, or sneaking new features in at the same time.

Keep in mind

Go deeper

Related terms

More in Software