Design Patterns & Refactoring
Introduction to Refactoring
Oliver Haase
HTWG Konstanz
What is Refactoring?
Definition (of the noun)
A change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior. (Martin Fowler)
Definition (of the verb)
To restructure software by applying a series of refactorings without changing its observable behavior. (Martin Fowler)
Definition – Key Points
make software structure easier: compare with performance optimizations that
don’t change functionality,
but usually make softwareharderto understand!
without changing behavior: user (end user or other software) cannot tell that implementation has changed.
No Refactoring Without Thorough Unit Tests!
Definition – Key Points
make software structure easier: compare with performance optimizations that
don’t change functionality,
but usually make softwareharderto understand!
without changing behavior: user (end user or other software) cannot tell that implementation has changed.
No Refactoring Without Thorough Unit Tests!
Metaphor of the Two Hats
Software development should consist of distinguishable phases of addition of new functionality andrefactoring:
When adding new functionality, don’t refactor existing code.
When refactoring, don’t change existing behavior.
A developer might swap hats very frequently, but should always be aware of which hat she is currently wearing.
Why Should You Refactor?
Without refactoring, code will decay
→ more often than not, code additions lead to redundancy!
→ refactoring as a means to periodically clean up code Refactoring makes the code easier to understand Refactoring helps you find bugs
Refactoring helps you program faster
When Should You Refactor?
Should you schedule two weeks of refactoring every other month of development?
You don’t decide to refactor, you refactor because you want to do something else, and refactoring helps you do the other thing.
When Should You Refactor?
Should you schedule two weeks of refactoring every other month of development?
You don’t decide to refactor, you refactor because you want to do something else, and refactoring helps you do the other thing.
The Rule of Three
Three strikes and you refactor.
1 Just do it.
2 Wince at duplication, duplicate anyway.
3 Refactor.
The Rule of Three
Three strikes and you refactor.
1 Just do it.
2 Wince at duplication, duplicate anyway.
3 Refactor.
What Do I Tell My Manager?
Many managers don’t appreciate refactoring per se
but: refactoring is a necessary part of the development process
⇓ Don’t tell.
What Do I Tell My Manager?
Many managers don’t appreciate refactoring per se
but: refactoring is a necessary part of the development process
⇓ Don’t tell.
Problems with Rectoring
Databases: Data schemas that applications are tightly coupled to are hard to change → Refactoring of databases is an own research area.
Changing Interfaces:
Minimizepublishedinterfaces, i.e. those whose users you cannot change.
Don’t publish interfaces prematurely.
Loosen code ownership so that unpublished interfaces can be changed.