By Brad Appleton: Ref or Ref Ing Act or Act Ing Ing Act or Act Ing Ref or Ref Ing or Ref Ing Act
By Brad Appleton: Ref or Ref Ing Act or Act Ing Ing Act or Act Ing Ref or Ref Ing or Ref Ing Act
By Brad Appleton: Ref or Ref Ing Act or Act Ing Ing Act or Act Ing Ref or Ref Ing or Ref Ing Act
or Refing
Refingact
actingRef
ingRef or
ingact or
by Brad Appleton
Created July 2009
Refact
oring
Presenter Information
Brad Appleton
Brad Appleton
2
Refactoring, Emergent Design & Evolutionary
Outline
Part I: Refactoring for Agility
Refactoring Overview
How to Refactor
Refactoring to Patterns & Principles
Emergent Design
Technical Debt
Restructuring
Evolutionary Architecture
Incremental Design
Brad Appleton
3
Refactoring, Emergent Design & Evolutionary
Part I
or acting
or Refing
Refingact
actingRef
ingRef or
ingact or
Refact
oring
Refactoring Defined
Refactoring Myths & Misconceptions
The Practice of Refactoring
Why Refactor?
How to Refactor
5
Refactoring, Emergent Design & Evolutionary
Overview of Refactoring
Identifies design-maintenance issues (code smells)
that typically represent violations of known principles
of good design.
Incrementally and iteratively applies a set of design
improvement techniques (refactorings).
The goal is to minimize complexity & duplication in
order to maximize simplicity & ease-of-change.
Encourages the right design details to emerge
just-in-time with minimal guesswork/rework.
Scaling-up includes the use of periodic restructuring,
initial & incremental design (just enough), and
evolutionary architecture.
Brad Appleton
6
Refactoring, Emergent Design & Evolutionary
Refactoring Defined
Refactoring (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.
Refactoring (verb): to restructure software by applying
a series of refactorings without changing its
observable behavior.
Martin Fowler, Refactoring: Improving the Design of Existing Code
Brad Appleton
7
Refactoring, Emergent Design & Evolutionary
Refactoring Distilled
Refactoring is a disciplined technique for the
restructuring of an existing body of code,
altering its internal structure without
changing its external behavior.
Brad Appleton
8
Refactoring, Emergent Design & Evolutionary
Refactoring Myths
Brad Appleton
9
Refactoring, Emergent Design & Evolutionary
Refactoring is NOT
Rework redesigning things that could, and should, have been
designed correctly in the first place.
Design gold-plating work that adds no business value, and
merely serves to stroke the egos of perfectionists who are out of
touch with business reality.
Miscellaneous code tidying the kind that is nice to have, but
should only happen when the team has some slack-time, and is a
luxury we can do without, without any serious consequences.
A license to hack avoiding any and all initial design & analysis
and instead jumping straight to coding with no real design.
Reengineering large-scale restructuring that requires a concerted
effort over the course of several weeks/months to re-write or rearchitect significant parts of the system.
Derived from: Patrick Wilson, Continuous Refactoring and the Cost of Decay
Brad Appleton
10
Refactoring, Emergent Design & Evolutionary
Refactoring IS
A systematic approach to source-code hygiene that minimizes
the chances of introducing bugs
Improving the design of the code after it has been written
A behavior-preserving transformation of source-code structure
The process of simplifying & consolidating a work-product by
making several, small, successive revisions focused on:
preserving correctness,
removing redundancy,
revealing thoughts & intentions, and
improving clarity & conciseness.
A disciplined way of making changes while exposing the project
to significantly less risk.
An effective means to address the economic reality of software
growth/complexity by reducing & amortizing its cost throughout
the daily business of development & maintenance activities.
Brad Appleton
11
Refactoring, Emergent Design & Evolutionary
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.
You will have more confidence to refactor and make other design
changes if you can quickly run automated tests.
12
Refactoring, Emergent Design & Evolutionary
Why Refactor?
Lots of reasons
Brad Appleton
13
Refactoring, Emergent Design & Evolutionary
How to Refactor
The overarching goal of refactoring is to:
Simple Code
Brad Appleton
14
Refactoring, Emergent Design & Evolutionary
Simple Code
Brad Appleton
15
Refactoring, Emergent Design & Evolutionary
Brad Appleton
16
Refactoring, Emergent Design & Evolutionary
Clean Code
Brad Appleton
17
Refactoring, Emergent Design & Evolutionary
Rules for Clean Code come from the book of the same
name by Robert C. Martin (a.k.a. Uncle Bob) et.al.
Clean Code: A Handbook of Agile Software Craftsmanship
18
Refactoring, Emergent Design & Evolutionary
Refactoring
Brad Appleton
19
Refactoring, Emergent Design & Evolutionary
Brad Appleton
20
Refactoring, Emergent Design & Evolutionary
Code Smells
Brad Appleton
21
Refactoring, Emergent Design & Evolutionary
Code Smells
Any symptom in the source code of a computer program
that indicates something may be wrong. It generally
indicates that the code should be refactored or the
overall design should be reexamined.
22
Refactoring, Emergent Design & Evolutionary
23
Refactoring, Emergent Design & Evolutionary
Categories of Refactorings
Small Refactorings
(De)Composing methods
Moving features between objects
Organizing data
Simplifying conditional expressions
Dealing with generalization
Simplifying method calls
Larger Refactorings/Restructurings
24
Refactoring, Emergent Design & Evolutionary
Refactorings
Some refactorings from real projects
Add a Parameter
Extract Method
Inline Method
Inline Class
Extract Interface
Move Field
Move Method
Parameterize Method
Rename Method
Method Parameter to Constructor Parameter
25
Refactoring, Emergent Design & Evolutionary
What to do if ?
I spot a smell that is not already known or
catalogued?
Convince yourself it really is a smell by trying to
identify its ill-effects and/or the design principle it
appears to violate
Brad Appleton
26
Refactoring, Emergent Design & Evolutionary
When to Refactor?
While adding functionality
While fixing a bug
While reviewing code
After coding the same/similar thing for the
third time (to factor out the duplication)
Brad Appleton
27
Refactoring, Emergent Design & Evolutionary
Refactoring Continually
It's best to refactor continuously (as a normal
part of each coding activity) rather than in
phases or increments.
When you come across code that is in need of improvement,
improve it!
On the other hand, if you must finish a feature before a demo
scheduled for tomorrow, finish the feature and refactor later.
28
Refactoring, Emergent Design & Evolutionary
Brad Appleton
29
Refactoring, Emergent Design & Evolutionary
Refactorings
Design Patterns
Design Principles
Brad Appleton
30
Refactoring, Emergent Design & Evolutionary
Brad Appleton
31
Refactoring, Emergent Design & Evolutionary
Brad Appleton
32
Refactoring, Emergent Design & Evolutionary
Design Principles
Brad Appleton
33
Refactoring, Emergent Design & Evolutionary
34
Refactoring, Emergent Design & Evolutionary
35
Refactoring, Emergent Design & Evolutionary
Design Patterns
Brad Appleton
36
Refactoring, Emergent Design & Evolutionary
Design Patterns
A Design Pattern is
Design patterns
37
Refactoring, Emergent Design & Evolutionary
38
Refactoring, Emergent Design & Evolutionary
& modifiability
Good automated testing is a
prerequisite
Refactoring is not rewriting, rework
or restructuring
With refactoring, we continuously
invest nominal effort to reduce the
Brad Appleton
39
Refactoring, Emergent Design & Evolutionary
Brad Appleton
40
Refactoring, Emergent Design & Evolutionary
Brad Appleton
41
Refactoring, Emergent Design & Evolutionary
http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf and
http://www.oodesign.com/
Brad Appleton
42
Refactoring, Emergent Design & Evolutionary
Books:
Design Patterns, by the Gang of Four (the book that started it all)
POSA Series of books on Pattern-Oriented Software Architecture
Head First Design Patterns, from the OReilly Head First Series
Refactoring to Patterns; by Joshua Kerievsky
Agile Software Development, Principles, Patterns, and Practices
by Robert C. Martin
Implementation Patterns; by Kent Beck
Design Patterns Explained (2ed); by Alan Shalloway, James Trott
Brad Appleton
43
Refactoring, Emergent Design & Evolutionary
44
Refactoring, Emergent Design & Evolutionary
Refactoring @ Scale
Part II
or acting
or Refing
Refingact
actingRef
ingRef or
ingact or
Refact
oring
Restructuring
Evolutionary Architecture
Incremental Design
JEDI (Just Enough Design Initially)
Supple Design & Domain-Driven Design
46
Refactoring, Emergent Design & Evolutionary
Scaling-Up
Brad Appleton
Scaling-Up
To scale refactoring for larger projects, additional
techniques & concepts must be added to the mix.
Note that this is in addition to (not instead of)
Refactoring
In-the-Small
Refactoring @ Scale
Deferred Refactoring
Code Smells
Design Principles &
Patterns
Simple/Clean Code
Brad Appleton
Supple/Domain-Driven Design
Emergent Design
Brad Appleton
49
Refactoring, Emergent Design & Evolutionary
Emergent Design
Emergent Design is a fancy name for the resulting
design that emerges from the synergy of
combining Refactoring together with TDD,
Continuous Integration and Automated Testing.
As a result, youll find the balance of work changes.
50
Refactoring, Emergent Design & Evolutionary
Technical Debt
Brad Appleton
51
Refactoring, Emergent Design & Evolutionary
Brad Appleton
52
Refactoring, Emergent Design & Evolutionary
53
Refactoring, Emergent Design & Evolutionary
Brad Appleton
54
Refactoring, Emergent Design & Evolutionary
55
Refactoring, Emergent Design & Evolutionary
Restructuring
Brad Appleton
56
Refactoring, Emergent Design & Evolutionary
Overview of Restructuring
Identifies higher-level issues (architecture smells)
that typically represent violations of known principles
of good software architecture & design.
Periodically applies larger-scale refactorings and/or
many small refactorings that were previously deferred.
The goal is to pay down technical debt in order to
limit the increasing costs of accumulated complexity.
Typically requires a concerted effort that must be
separately planned.
Uses not only design patterns/principles, but also
architectural patterns/principles, as well as software
modifiability tactics.
Brad Appleton
57
Refactoring, Emergent Design & Evolutionary
Brad Appleton
58
Refactoring, Emergent Design & Evolutionary
Restructure Periodically
Restructuring is often associated with absent or
neglectful refactoring and/or design.
But Any large software project spanning multiple
teams eventually needs restructuring.
Even in the presence expert-level architecture, design &
continuous refactoring
This is just a reality of software evolution/entropy
Brad Appleton
59
Refactoring, Emergent Design & Evolutionary
Architecture Smells
Brad Appleton
60
Refactoring, Emergent Design & Evolutionary
Architecture Smells
Smells in Dependency Graphs
Obsolete Classes
Tree-like, Static Cycles
Visibility of Dependency Graphs
Smells in Inheritance
Hierarchies
Smells in Packages
Unused Packages
Cyclic Package Dependencies
Packages Too Small / Large
Package Hierarchies Unbalanced
Package Hierarchies Not Clearly
Named
Smells in Subsystems
No Subsystems
Cycles between Subsystems
Subsystem Too Large / Small
Subsystem-API Bypassed
Subsystem-API Too Large
Overgeneralization
Smells in Layers
Source: Refactoring in Large Software Projects, by Martin Lippert & Stefan Roock
Brad Appleton
61
Refactoring, Emergent Design & Evolutionary
Brad Appleton
62
Refactoring, Emergent Design & Evolutionary
Hide Information
Maintain Existing Interfaces
Restrict Communication Paths
Use an Intermediary
Run-time Registration
Configuration Files
Polymorphism/Delegation
Component Replacement
Adhere to Defined Protocols
Brad Appleton
63
Refactoring, Emergent Design & Evolutionary
During Retrospectives
Iteration retrospectives
Inter-team (joint) retrospectives [Scrum-of-Scrums level]
After Reviews
64
Refactoring, Emergent Design & Evolutionary
Evolutionary Architecture
Brad Appleton
65
Refactoring, Emergent Design & Evolutionary
Evolutionary Architecture
Software Architecture concerns infrastructure elements
that must exist before you can begin execution.
Architecture is about things that are hard to change later, it is
difficult to allow an architecture to emerge.
Incremental Design
Design evolves incrementally, iteration by iteration,
based on current business priorities and discovered
technical limitations.
Invest in the design only what is needed to comfortably support the current
iteration. Kent Beck & Cynthia Andres
Incremental Design
Specifying too much detail too soon causes more rework later.
But doing less now and saving the rest for later should not require
significantly more work later than it would today.
We must do Just Enough Design Initially to attain the right balance
of anticipation and adaptation.
Brad Appleton
67
Refactoring, Emergent Design & Evolutionary
Brad Appleton
68
Refactoring, Emergent Design & Evolutionary
Brad Appleton
69
Refactoring, Emergent Design & Evolutionary
Supple Design
Brad Appleton
70
Refactoring, Emergent Design & Evolutionary
The basic idea is the design should directly reflect the core business
domain and domain-logic of the problem to solve
This helps understanding the problem as well as the implementation
and increases maintainability of the software.
Intention-Revealing Interfaces, Ubiquitous Language, Side-EffectFree Functions, Assertions, Conceptual Contours, Standalone
Classes, Closure of Operations, Declarative Style
Brad Appleton
71
Refactoring, Emergent Design & Evolutionary
Brad Appleton
73
Refactoring, Emergent Design & Evolutionary
Resources: Restructuring
InfoQ: Michael Stal on Architecture Refactoring
Refactoring in Large Software Projects: Performing Compl
ex Restructurings Successfully
, by Martin Lippert, Stephen Roock (also an
earlier version online)
The Mikado Method and
Refactoring Large Software Systems
Object-oriented Reengineering Patterns, by S. Demeyer, S.
Ducasse & O. Nierstrasz (freely downloadable online)
Brad Appleton
74
Refactoring, Emergent Design & Evolutionary
75
Refactoring, Emergent Design & Evolutionary
http://www.sei.cmu.edu/news-at-sei/columns/the_architect/2007/08/architect-2007-08.htm
Brad Appleton
76
Refactoring, Emergent Design & Evolutionary
Brad Appleton
77
Refactoring, Emergent Design & Evolutionary
Part III
or acting
or Refing
Refingact
actingRef
ingRef or
ingact or
Refact
oring
Brad Appleton
79
Refactoring, Emergent Design & Evolutionary
Refactoring Databases
See the following:
Brad Appleton
80
Refactoring, Emergent Design & Evolutionary
Brad Appleton
81
Refactoring, Emergent Design & Evolutionary
Inline Resource:
Move contents of an external resource into fixture setup logic of the test.
Create an external resource within the fixture setup logic of the test rather
than using a predefined resource.
Source: www.xunitpatterns.com
Brad Appleton
82
Refactoring, Emergent Design & Evolutionary
Online Resources
The Refactoring homepage - www.refactoring.com
Refactoring to Patterns Catalog
Refactoring: Small Steps Guaranteed to Help You Clean Up Your Code,
by C. Keith Ray.
Introductory Refactoring Articles
Podcasts, Refactoring: Part 1 and Part 2, a talk with Martin Lippert.
Refactoring Checklist, by Michael Nielsen.
Michael Stal on Architecture Refactoring, by Niclas Nilsson.
Introduction to Refactoring to Patterns, by Joshua Kerievsky.
Code Refactoring Guidelines, by Federico Cargnelutti.
Smells to Refactorings table, from wiki.java.net
Refactoring with Code Metrics, Andrew Glover
Refactoring Thumbnails, from www.refactoring.be
Continual Refactoring, by Paul Duvall
Refactoring blogs, books, forums, sample chapters, videos, tools and
Brad Appleton
83
Refactoring, Emergent Design & Evolutionary
Books
Refactoring: Improving The Design of Existing Code; by
Martin Fowler
Refactoring to Patterns; by Joshua Kerievsky
Emergent Design: The Evolutionary Nature of Professional
Software Development, by Scott L. Bain
Clean Code: A Handbook of Agile Software Craftsmanship;
by Robert C. Martin
Working Effectively with Legacy Code; by Michael Feathers
xUnit Test Patterns: Refactoring Test Code, by Gerard
Meszaros
Domain-Driven Design: Tackling Complexity in the Heart of
Software; by Eric Evans
Implementation Patterns; by Kent Beck
Brad Appleton
84
Refactoring, Emergent Design & Evolutionary
Books
Brad Appleton
85
Refactoring, Emergent Design & Evolutionary
Backup Slides
[for reference material]
With a), we incur design debt, which can be paid down later by
refactoring [or more significant restructuring].
With b), we analyze what must change to best accommodate the
new feature and then make the necessary changes.
Brad Appleton
87
Refactoring, Emergent Design & Evolutionary
Brad Appleton
88
Refactoring, Emergent Design & Evolutionary
Brad Appleton
89
Refactoring, Emergent Design & Evolutionary
Test-Driven Refactoring
Testing also plays an altogether different role in
refactoring;
Brad Appleton
90
Refactoring, Emergent Design & Evolutionary
Brad Appleton
91
Refactoring, Emergent Design & Evolutionary
92
Refactoring, Emergent Design & Evolutionary
Lower
management
complexity
- Small scale
- Informal
- Single stakeholder
- Products
Telecom
Switch
Commercial
Embedded
Compiler
Automotive
Software
CASE Tool
Small Scientific
Simulation
IS Application
Distributed Objects
(Order Entry)
Defense
Weapon System
National Air Traffic
Control System
Large-Scale
Organization/Entity
Simulation
Enterprise IS
(Family of IS
Applications)
Defense
MIS System
IS Application
GUI/RDB
(Order Entry)
Higher
management
complexity
- Large scale
- Contractual
- Many stake holders
- Projects
Business
Spreadsheet
93
Refactoring, Emergent Design & Evolutionary
Brad Appleton
94
Refactoring, Emergent Design & Evolutionary
Accidental Complexity
Three things tend to spawn accidental complexity.
Irreversibility affects both architecture and design, although its effects are
both more common and more damaging at the architectural level. Try to
avoid decisions impossible or cumbersome to reverse.
Source: Neal Ford, Evolutionary Architecture and Emergent Design
Brad Appleton
95
Refactoring, Emergent Design & Evolutionary
96
Refactoring, Emergent Design & Evolutionary
97
Refactoring, Emergent Design & Evolutionary
98
Refactoring, Emergent Design & Evolutionary
If you make compromises when you dig a ditch, you just get
uneven width or unequal depth. Today's flawed ditch doesn't
prevent you from digging a good ditch tomorrow.
But the software you build today is the foundation for what you
build tomorrow. Compromises made now for the sake of
expediency cause entropy to build up in your software.
99
Refactoring, Emergent Design & Evolutionary
100
Refactoring, Emergent Design & Evolutionary
You can think of the inherent complexity as the principal, and the extra
effort imposed by previous expedient shortcuts as the interest.
Source: Neal Ford, Evolutionary Architecture and Emergent Design
Brad Appleton
101
Refactoring, Emergent Design & Evolutionary