What is refactoring? Benefits of a code refactoring

September 7th, 2010

“Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior. Its heart is a series of small behavior preserving transformations. Each transformation (called a ‘refactoring’) does little, but a sequence of transformations can produce a significant restructuring. Since each refactoring is small, it’s less likely to go wrong. The system is also kept fully working after each small refactoring, reducing the chances that a system can get seriously broken during the restructuring.”

Code refactoring is the process of changing a computer program’s source code without modifying its external functional behavior, in order to improve some of the nonfunctional attributes of the software. Advantages include improved code readability and reduced complexity to improve the maintainability of the source code, as well as a more expressive internal architecture or object model to improve extensibility.

Refactoring is usually motivated by noting a code’s smell. For example the method at hand may be very long, or it may be a near duplicate of another nearby method. Once recognized, such problems can be addressed by refactoring the source code, or transforming it into a new form that behaves the same as before, but that no longer “smells”. For a long routine, extract one or more smaller subroutines. Or, for duplicate routines, remove the duplication and utilize one shared function in their place. Failure to perform refactoring can result in accumulating technical debt.
There are two general categories of benefits to the activity of refactoring:

Maintainability. It is easier to fix bugs because the source code is easy to read and the intent of its author is easy to grasp. This might be achieved by reducing large monolithic routines into a set of individually concise, well-named, single-purpose methods. It might be achieved by moving a method to a more appropriate class, or by removing misleading comments.

Extensibility. It is easier to extend the capabilities of the application if it uses recognizable design patterns, and it provides some flexibility where none may have existed before.

If we take a look at the refactorings Refactor! Pro provides, we may see that the list is very long. This may make you think that performing a refactoring can be a difficult task. For example, to use built-in Visual Studio refactorings, you need to use a menu, or remember all shortcuts for every refactoring. However, on the other hand, in Refactor! there’s only a single shortcut to perform a refactoring on a block of code. By default, the shortcut is bound to CTRL+` (back tick). This shortcut brings a popup menu with available refactorings at the caret position. There are also a few different ways to perform a code refactoring.

Almost all refactorings have a preview hint which allows you to see the resulting code before a particular refactoring is performed – to learn more, see the “Refactorings preview hinting” topic.