Home CPSC 330

Refactoring

Overview

Refactoring is the process of improving code by rewriting some small part of it. Refactoring does not change the behavior of code.


Reasons to Refactor

Even with the best design, a large code base tends to gets messy. As bugs are fixed, and features added, clean code can become ugly. This is sometimes called software rot, decay or entropy.

Messy code can be more difficult to test, debug and maintain. The messier a code base gets, the harder it gets to fix.

The goal of refactoring is to clean & restructure code as you go.


Reasons not to Refactor

The reason that people don't refactor is because it can be scary to change working code.

Having good unit tests mitigates the risk of breaking things.


Refactoring Rules

While refactoring can be done in many ways, there are several common patterns. Each of these addresses a specific problem that develops in code.

The rules can be applied many times, and are each small enough to not cause major problems.


Rename Method


Add Explaining Variable


Encapsulate Field


Extract Method


Extract Class


Push Down


Pull Up

Copyright © 2018 Ian Finlayson | Licensed under a Creative Commons Attribution 4.0 International License.