OOPortal OOPortal


Model Refinement   «Prev  Next»
Lesson 2 Refactoring
Objective Define the goal and process of refactoring.

Refactoring Patterns

Fundamental Approach Software Development

What is Refactoring?

Refactoring is the process of changing a software system in such a way that it does not alter the external behavior of the code yet improves its internal structure. It is a disciplined way to clean up code that minimizes the chances of introducing bugs. In essence when you refactor you are improving the design of the code after it has been written. "Improving the design after it has been written." That's an odd turn of phrase. In our current understanding of software development we believe that we design and then we code. A good design comes first, and the coding comes second. Over time the code will be modified, and the integrity of the system, its structure according to that design, gradually fades. The code slowly sinks from engineering to hacking. Refactoring is the opposite of this practice. With refactoring you can take a bad design, chaos even, and rework it into well-designed code. Each step is simple, even simplistic. You move a field from one class to another, pull some code out of a method to make into its own method, and push some code up or down a hierarchy. Yet the cumulative effect of these small changes can radically improve the design. It is the exact reverse of the normal notion of software decay.
With refactoring you find the balance of work changes. You find that design, rather than occurring all up front, occurs continuously during development. You learn from building the system how to improve the design. The resulting interaction leads to a program with a design that stays good as development continues.



Refactoring and patterns

The term refactoring describes a process for (1) evaluating a model or process in terms of the problems that it causes, and (2) applying common solutions to mitigate those problems. The solutions are revisions to the models or processes themselves.
Most of the problems that we encounter in software development have been experienced throughout the industry for many years. Consequently, the refactored solutions are typically available within the development community as a type of pattern that is, a recurring solution that includes common elements even though the specific implementation may vary.
Refactoring really just means rethinking the problem and the solution. The need for refactoring comes from the observed problems created by the current solutions. However, refactoring has to have a clearly defined goal to arrive at a better solution than the original. The goals that we have used in this course to direct us toward the best solutions include:

Encapsulation
Cohesion
Coupling
Responsibility
Consistent and meaningful communication

In the following lessons, I will reemphasize the purpose and value of each goal as it applies to the iterative process of improving your model.